通过httpClient发送json格式数据请求
2015-06-16 09:55
926 查看
//引入maven
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.0.1</version>
</dependency>
//重写HTTP请求类
import java.net.URI;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
public final static String METHOD_NAME = "GET";
public HttpGetWithEntity() {
super();
}
public HttpGetWithEntity(final URI uri) {
super();
setURI(uri);
}
/**
* @throws IllegalArgumentException
* if the uri is invalid.
*/
public HttpGetWithEntity(final String uri) {
super();
setURI(URI.create(uri));
}
@Override
public String getMethod() {
// TODO Auto-generated method stub
return METHOD_NAME;
}
}
//测试调用
/**
* 发送带json格式数据!get請求方式
*
* @param url
* @param jsonParams {\"from\":\"20150101\","to":"20150111"}
* @return
* @throws ServiceException
*/
public static String processGetWithData(String url, String jsonParams) throws ServiceException {
String result = null;
org.apache.http.client.HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
try {
HttpGetWithEntity e = new HttpGetWithEntity(url);
e.setHeader("Content-Type", "application/json");
if (!StringUtils.isEmpty(jsonParams)) {
StringEntity se = new StringEntity(jsonParams);
e.setEntity(se);
}
HttpResponse response = httpclient.execute(e);
int STATUS_CODE = response.getStatusLine().getStatusCode();
if (STATUS_CODE == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
if (entity != null) {
entity.consumeContent();
}
} else {
throw new ServiceException(HttpExceptionEnums.CODE_INTERNAL_EXCPTION);
}
httpclient.getConnectionManager().shutdown();
} catch (UnsupportedEncodingException e) {
throw new ServiceException(HttpExceptionEnums.UNKNOWN_CHARSET_EXCPTION);
} catch (ClientProtocolException e) {
throw new ServiceException(HttpExceptionEnums.ClientProtocolException_EXCPTION);
} catch (ParseException e) {
throw new ServiceException(HttpExceptionEnums.PARSEException_EXCPTION);
} catch (IOException e) {
throw new ServiceException(HttpExceptionEnums.IOException_EXCPTION);
}
return result;
}
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.0.1</version>
</dependency>
//重写HTTP请求类
import java.net.URI;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
public class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
public final static String METHOD_NAME = "GET";
public HttpGetWithEntity() {
super();
}
public HttpGetWithEntity(final URI uri) {
super();
setURI(uri);
}
/**
* @throws IllegalArgumentException
* if the uri is invalid.
*/
public HttpGetWithEntity(final String uri) {
super();
setURI(URI.create(uri));
}
@Override
public String getMethod() {
// TODO Auto-generated method stub
return METHOD_NAME;
}
}
//测试调用
/**
* 发送带json格式数据!get請求方式
*
* @param url
* @param jsonParams {\"from\":\"20150101\","to":"20150111"}
* @return
* @throws ServiceException
*/
public static String processGetWithData(String url, String jsonParams) throws ServiceException {
String result = null;
org.apache.http.client.HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 60000);
httpclient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 60000);
try {
HttpGetWithEntity e = new HttpGetWithEntity(url);
e.setHeader("Content-Type", "application/json");
if (!StringUtils.isEmpty(jsonParams)) {
StringEntity se = new StringEntity(jsonParams);
e.setEntity(se);
}
HttpResponse response = httpclient.execute(e);
int STATUS_CODE = response.getStatusLine().getStatusCode();
if (STATUS_CODE == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
if (entity != null) {
result = EntityUtils.toString(entity);
}
if (entity != null) {
entity.consumeContent();
}
} else {
throw new ServiceException(HttpExceptionEnums.CODE_INTERNAL_EXCPTION);
}
httpclient.getConnectionManager().shutdown();
} catch (UnsupportedEncodingException e) {
throw new ServiceException(HttpExceptionEnums.UNKNOWN_CHARSET_EXCPTION);
} catch (ClientProtocolException e) {
throw new ServiceException(HttpExceptionEnums.ClientProtocolException_EXCPTION);
} catch (ParseException e) {
throw new ServiceException(HttpExceptionEnums.PARSEException_EXCPTION);
} catch (IOException e) {
throw new ServiceException(HttpExceptionEnums.IOException_EXCPTION);
}
return result;
}
相关文章推荐
- 为自己记----android开发学习记录之网络请求
- ios通过图片的URL地址,从网络上获取图片(UIImage类型)
- Wince 6.0适用 .NET 使用HttpRequest的Post上传文件,服务端的Web API接收Post上传上来的文件 代码
- 机器学习、计算机视觉有关书籍--转自http://www.cnblogs.com/xmphoenix/p/3683870.html
- Delphi多线程和indy10 tcp的使用。
- TCP segment of a reassembled PDU
- 6.15 网络请求,Get和Post,同步和异步
- Cookie的httponly属性设置方法
- ASIHTTPRequest详解
- nginx 和 php-fpm 通信使用unix socket还是TCP,及其配置
- Mac OS X网络诊断命令
- wp8.1使用HttpClient发送post请求数据
- 利用GPU和Caffe训练神经网络
- Spring找不到方法:The method getDispatcherType() is undefined for the type HttpServletRequest
- HTTP报文格式说明
- 西南最大的汽车售后网络运营商
- 西南最大的汽车售后网络运营商
- tcp/ip详解
- TCP连接状态详解
- 接口或者http请求测试工具--chrome plug