httpclient 3.1和4.1 请求的区别
2014-02-17 11:55
309 查看
Httpclient 3.1和httpclient 4.1 在post请求中的不同。
下面的示例将httpclient请求模拟webservices请求。
3.1 包名:commons-httpclient-3.1.jar
4.1 包名:httpclient-4.1.jar
如有任何疑问可扫码头像关注公众号!
下面的示例将httpclient请求模拟webservices请求。
3.1 包名:commons-httpclient-3.1.jar
public static EnvelopeDocument call(String soapString, String endpoint, int timeout)
throws SOAException
{
EnvelopeDocument resDoc = null;
PostMethod post = null;
HttpClient httpclient = new HttpClient();
try
{
HttpClientParams params = new HttpClientParams();
params.setSoTimeout(timeout * 1000);
httpclient.setParams(params);
post = new PostMethod(endpoint);
org.apache.commons.httpclient.methods.RequestEntity entity = new StringRequestEntity(soapString, "text/xml", "UTF-8");
post.setRequestEntity(entity);
int result = httpclient.executeMethod(post);
String res;
if(result != 200)
{
res = post.getResponseBodyAsString();
throw new SOAException(ErrorCode.CALL_WEB_SERVICE_ERROR, "Web Service Server returns HTTP code " + result + "\n" + res);
}
res = post.getResponseBodyAsString();
try
{
resDoc = org.xmlsoap.schemas.soap.envelope.EnvelopeDocument.Factory.parse(res);
}
catch(XmlException xe)
{
throw new SOAException(ErrorCode.PARSE_RESPONSE_XML_ERROR, "Parsing web service response soap message error", xe);
}
}
catch(SOAException e)
{
e.printStackTrace();
throw e;
}
catch(Exception e)
{
e.printStackTrace();
throw new SOAException(ErrorCode.CALL_WEB_SERVICE_ERROR, "call web service error" + e.getMessage(), e);
}
return resDoc;
}
4.1 包名:httpclient-4.1.jar
public static String requestWebservice(String url, String xmlParam)
throws ClientProtocolException, IOException {
Validate.notNull(url, "请求url");
Validate.notNull(xmlParam, "符合webservice标准的xml字符串");
String responseContent = "";
HttpClient httpClient = new DefaultHttpClient();
byte[] b = xmlParam.getBytes("utf-8");
HttpPost httpPost = new HttpPost(url);
if (url.indexOf("RushRepairService") != -1) {
b = xmlParam.getBytes("GBK");
}
InputStream is = new ByteArrayInputStream(b, 0, b.length);
HttpEntity re = new InputStreamEntity(is, b.length);
httpPost.setEntity(re);
HttpResponse response = httpClient.execute(httpPost);
if (200 != response.getStatusLine().getStatusCode()) {
throw new RuntimeException("http返回状态"
+ response.getStatusLine().getStatusCode());
}
HttpEntity entity = response.getEntity();
String inputLine = "";
InputStreamReader in = new InputStreamReader(entity.getContent());
BufferedReader reader = new BufferedReader(in);
while ((inputLine = reader.readLine()) != null) {
responseContent += inputLine;
}
reader.close();
httpClient.getConnectionManager().shutdown();
return responseContent;
}
如有任何疑问可扫码头像关注公众号!
相关文章推荐
- java 利用httpclient 3.1 和 httpclient4.1.2发送post请求
- httpclient3.1发送https 的post请求,带参
- 跨域请求中JSONP和HttpClient的区别
- java 利用httpclient 3.1 和 httpclient4.1.2发送post请求
- java 利用httpclient 3.1 和 httpclient4.1.2发送post请求
- 我的Android进阶之旅------>Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- Android Asynchronous Http Client-Android异步网络请求客户端接口
- 【Angular】angular-HttpClient 与服务器通讯 Get Post Put Delete Http 拦截器 请求讲解
- HTTPClient跨域访问 ——Get请求
- HttpClient 请求
- httpclient 请求 json 数据
- Java httpclient请求,解决乱码问题
- android_30_使用HttpClient发送Get和Post请求
- OkHttpClient 请求超时问题-------以解决
- Http之Get/Post请求区别
- Java网络连接之HttpURLConnection与HttpClient 区别及联系
- 我的Android进阶之旅------>Android发送GET和POST以及HttpClient发送POST请求给服务器响应
- 网络请求之HttpClient
- Android Asynchronous Http Client-Android异步网络请求客户端接口
- Apache Commons-httpclient-3.1重定向时Cookie丢失缺陷问题(Java)。