HttpClient (POST GET PUT)请求
HttpClient (POST GET PUT)请求
package com.curender.web.server.http;
import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.curender.dal.util.ConstantsDal;
/**
* HttpClient GET POST PUT 请求
* @author huang
* @date 2013-4-10
*/
public class HttpRequest
{
protected static Log log = LogFactory.getLog(HttpRequest.class);
private static HttpRequest httpRequst=null;
private HttpRequest(){}
public static HttpRequest getInstance(){
if(httpRequst==null){
synchronized(HttpRequest.class){
if(httpRequst == null){
httpRequst=new HttpRequest();
}
}
}
return httpRequst;
}
/**
* HttpClient GET请求
* @author huang
* @date 2013-4-9
* @param uri
* @return resStr 请求返回的JSON数据
*/
public String doGet(String uri){
String resStr = null;
HttpClient htpClient = new HttpClient();
GetMethod getMethod = new GetMethod(ConstantsDal.SERVER_URL+uri);
getMethod.getParams().setParameter( HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
try{
int statusCode = htpClient.executeMethod( getMethod );
// log.info(statusCode);
if(statusCode != HttpStatus.SC_OK){
log.error("Method failed: "+getMethod.getStatusLine());
return resStr;
}
byte[] responseBody = getMethod.getResponseBody();
resStr = new String(responseBody,HttpConstants.ENCODED);
} catch (HttpException e) {
log.error("Please check your provided http address!"); //发生致命的异常,可能是协议不对或者返回的内容有问题
} catch (IOException e) {
log.error( "Network anomaly" ); //发生网络异常
}finally{
getMethod.releaseConnection(); //释放连接
}
return resStr;
}
/**
* HttpClient POST请求
* @author huang
* @date 2013-4-9
* @param s_user
* @return resStr 请求返回的JSON数据
*/
@SuppressWarnings( "deprecation" )
public String doPost(String uri,String jsonObj){
String resStr = null;
HttpClient htpClient = new HttpClient();
PostMethod postMethod = new PostMethod(ConstantsDal.SERVER_URL+uri);
postMethod.addRequestHeader( "Content-Type","application/json" );
postMethod.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET, HttpConstants.ENCODED );
postMethod.setRequestBody( jsonObj );
try{
int statusCode = htpClient.executeMethod( postMethod );
// log.info(statusCode);
if(statusCode != HttpStatus.SC_OK){
//post和put不能自动处理转发 301:永久重定向,告诉客户端以后应从新地址访问 302:Moved Temporarily
if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY||statusCode == HttpStatus.SC_MOVED_TEMPORARILY){
Header locationHeader = postMethod.getResponseHeader( "location" );
String location = null;
if(locationHeader!=null){
location = locationHeader.getValue();
log.info("The page was redirected to :"+location);
}else{
log.info("Location field value is null");
}
}else{
log.error("Method failed: "+postMethod.getStatusLine());
}
return resStr;
}
byte[] responseBody = postMethod.getResponseBody();
resStr = new String(responseBody,HttpConstants.ENCODED);
}catch(Exception e){
e.printStackTrace();
}finally{
postMethod.releaseConnection();
}
return resStr;
}
/**
* HttpClient PUT请求
* @author huang
* @date 2013-4-10
* @return
*/
@SuppressWarnings( "deprecation" )
public String doPut(String uri,String jsonObj){
String resStr = null;
HttpClient htpClient = new HttpClient();
PutMethod putMethod = new PutMethod(ConstantsDal.SERVER_URL+uri);
putMethod.addRequestHeader( "Content-Type","application/json" );
putMethod.getParams().setParameter( HttpMethodParams.HTTP_CONTENT_CHARSET, HttpConstants.ENCODED );
putMethod.setRequestBody( jsonObj );
try{
int statusCode = htpClient.executeMethod( putMethod );
// log.info(statusCode);
if(statusCode != HttpStatus.SC_OK){
log.error("Method failed: "+putMethod.getStatusLine());
return null;
}
byte[] responseBody = putMethod.getResponseBody();
resStr = new String(responseBody,HttpConstants.ENCODED);
}catch(Exception e){
e.printStackTrace();
}finally{
putMethod.releaseConnection();
}
return resStr;
}
}
HttpClient (POST GET PUT)请求的更多相关文章
- HttpClient方式模拟http请求设置头
关于HttpClient方式模拟http请求,请求头以及其他参数的设置. 本文就暂时不给栗子了,当作简版参考手册吧. 发送请求是设置请求头:header HttpClient httpClient = ...
- HttpClient发送get post请求和数据解析
最近在跟app对接的时候有个业务是微信登录,在这里记录的不是如何一步步操作第三方的,因为是跟app对接,所以一部分代码不是由我写,我只负责处理数据,但是整个微信第三方的流程大致都差不多,app端说要传 ...
- HttpWebRequest 改为 HttpClient 踩坑记-请求头设置
HttpWebRequest 改为 HttpClient 踩坑记-请求头设置 Intro 这两天改了一个项目,原来的项目是.net framework 项目,里面处理 HTTP 请求使用的是 WebR ...
- 使用HttpClient发送Get/Post请求 你get了吗?
HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议 ...
- org.apache.httpcomponents httpclient 发起HTTP JSON请求
1. pom.xml <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactI ...
- httpclient的几种请求URL的方式
一.httpclient项目有两种使用方式.一种是commons项目,这一个就只更新到3.1版本了.现在挪到了HttpComponents子项目下了,这里重点讲解HttpComponents下面的ht ...
- HttpClient发起Http/Https请求工具类
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpcl ...
- HttpClient方式模拟http请求
方式一:HttpClient import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.http.*; im ...
- Android HttpClient GET或者POST请求基本使用方法(转)
在Android开发中我们经常会用到网络连接功能与服务器进行数据的交互,为此Android的SDK提供了Apache的HttpClient来方便我们使用各种Http服务.这里只介绍如何使用HttpCl ...
随机推荐
- SpringMVC注解汇总(二)-请求映射规则
接上一节SpringMVC注解汇总-定义 讲到Httpy请求信息 URL路径映射 1)普通URL路径映射 @RequestMapping(value={"/test1", &quo ...
- Linux配置JDK1.7和Resin4.0
1.安装JDK1.7 (1)下载 官网下载路径:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-18802 ...
- iOS学习-UITextField设置placeholder的颜色
UITextField *text = [[UITextField alloc] initWithFrame:CGRectMake(, , , )]; text.borderStyle = UITex ...
- java的反射
JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意方法和属性:这种动态获取信息以及动态调用对象方法的功能称为java语言的反射机制. ...
- Uncaught RangeError: Maximum call stack size exceeded 超出最大调用值(个人解释)
写了段jq后,报这个错,度娘未解,灵光一闪,找到原因,上代码: Html 结构: <a href="javascript:;" class="item-pic&qu ...
- 日历插件FullCalendar应用:(二)数据增删改
接上一篇 日历插件FullCalendar应用:(一)数据展现. 这一篇主要讲使用fullcalendar插件如何做数据的增删改,用到了art.dialog web对话框组件,上一篇用到的webFor ...
- 【BZOJ-4698】Sandy的卡片 后缀数组
4698: Sdoi2008 Sandy的卡片 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 140 Solved: 55[Submit][Stat ...
- [Android] Visual Studio Emulator For Android 相关
1.修改设备名 C:\Users\[用户名]\AppData\Local\Microsoft\VisualStudioEmulator\Android\Containers\Local\Devices ...
- Rest webservice 和SOAP webservice
SOAP: 简单对象访问协议(Simple Object Access Protocol,SOAP)是一种基于 XML 的协议,可以和现存的许多因特网协议和格式结合使用,包括超文本传输协议(HTTP) ...
- Android File存储
原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/Android_File_store.html 一 概念 Android系统中提供了一种文件读写的方 ...