HttpClient client = new DefaultHttpClient ();
HttpPost post = new HttpPost(url);
List<NameValuePair> data = Lists.newArrayList();
data.add(new BasicNameValuePair("sign","sign+md5"));
try {
post.setEntity(new UrlEncodedFormEntity(data));
client.execute(post);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
------------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------
4.5
CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet("http://localhost/"); CloseableHttpResponse response = httpclient.execute(httpget); try { HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); try { // do something useful } finally { instream.close();//保持连接 } } } finally { response.close(); //直接shutdown这个连接 }
The difference between closing the content stream and closing the response is that the former will attempt to keep the underlying connection alive by consuming the entity content while the latter immediately shuts down and discards the connection.
Response Handler
The simplest and the most convenient way to handle responses is by using the ResponseHandler
interface, which includes the handleResponse(HttpResponse response)
method. This method completely relieves the user from having to worry about connection management.
When using a ResponseHandler
, HttpClient will automatically take care of ensuring release of the connection back to the connection manager regardless whether the request execution succeeds or causes an exception.
CloseableHttpClient httpclient = HttpClients.createDefault(); HttpGet httpget = new HttpGet("http://localhost/json"); ResponseHandler<MyJsonObject> rh = new ResponseHandler<MyJsonObject>() { @Override public JsonObject handleResponse( final HttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException( statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (entity == null) { throw new ClientProtocolException("Response contains no content"); } Gson gson = new GsonBuilder().create(); ContentType contentType = ContentType.getOrDefault(entity); Charset charset = contentType.getCharset(); Reader reader = new InputStreamReader(entity.getContent(), charset); return gson.fromJson(reader, MyJsonObject.class); } }; MyJsonObject myjson = client.execute(httpget, rh);
相关推荐
HttpClient4.0.1官方教程,里面详细介绍了HttpClient的使用
HttpClient 4.0.1所依赖的所有JAR包。包括: commons-logging-1.1.1.jar commons-codec-1.4.jar httpcore-4.0.1.jar<br/> httpclient-4.0.1.jar apache-mime4j-0.6.jar httpmime-4.0.1.jar
HttpClient4.0.1版本 官方 中文教程。
HttpClient-4.0.1 官方教程,包含HttpClient-4.0.1的基本用法,讲解清晰,格式为PDF
HttpClient-4.0.1中文文档下载+官方教程
HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,...本文首先介绍 HTTPClient,然后根据作者实际工作经验给出了一些常见问题的解决方法。
httpclient-4.0.1.jar11111111111111111111111111111111111111
httpclient 4.0.3 源代码
httpclient4中文教程 httpclient4
最新的java httpclient库 httpclient库4.0.1
HttpClient-4.0.1 官方教程,是官方的权威开发指南对于需要这方面的人具有很好的帮助作用
httpclient 开发所需要的jar。希望帮到大家
常用的Commsjar包:commons-codec-1.3.jar\commons-logging-1.1.1.jar\httpclient-4.0.1.jar\httpcore-4.0.1.jar\GJson.jar
httpclient框架 org.apache.commons.httpclient 4.0.1
commons-beanutils-1.7.0,apache-mime4j-0.6,dom4j-1.6.1,httpclient-4.0.1等SMS常用JAR包合集。
HttpClient 4.0.1所依赖的所有JAR包。包括: commons-logging-1.1.1.jar commons-codec-1.4.jar httpcore-4.0.1.jar httpclient-4.0.1.jar apache-mime4j-0.6.jar httpmime-4.0.1.jar
httpclient所有依赖包 httpclient-4.0.1.jar commons-codec-1.3.jar commons-logging-1.1.1.jar apache-mime4j-0.6.jar httpcore-4.0.1.jar httpmime-4.0.1.jar
httpclient的Android应用,包括基本的所有jar包httpclient-4.0.1.jar;httpcore-nio-4.0.1.jar;httpmime-4.0.1.jar;
Android升级后旧版本的httpclient4.0 apache-mime4j-0.6 commons-codec-1.4 commons-logging-1.1.1 httpclient-4.0.1 httpcore-4.0.1 httpmime-4.0.1