httpclientå¦ç¨ä¸ httpclientä½ä¸ºå®¢æ·ç«¯è°ç¨webservice | http://aperise.iteye.com/blog/2223454 |
httpclientå¦ç¨äº httpclientä¿æä¼è¯ç»å½ | http://aperise.iteye.com/blog/2223470 |
httpclientè¿æ¥æ± | http://aperise.iteye.com/blog/2295153 |
httpclientè¿æ¥æ±
1.è¿æ¥æ± åç(å¾ ç»)
2.httpclientè¿æ¥æ± å¦ä½ä¿è¯è¿æ¥äº¤åè³è¿æ¥æ± 管çå¨
  2.1 æ¹å¼ä¸
ããHttpResponse response = (httpMethod);
ããHttpEntity entity = response.getEntity();
ãã//è¿ä¸¤æ®µä»£ç è¿åçentityæ¯HttpEntityçå®ç°ç±»BasicManagedEntityãæ¤æ¶ä¸æ¬æ¬¡è¯·æ±å
³èçè¿æ¥å°æªå½è¿è³è¿æ¥ç®¡çå¨ãéè¦è°ç¨ä»¥ä¸ä¸¤æ¡è¯å¥ï¼
ããInputStream instream = entity.getContent();//è·å¾ååºå
·ä½å
容
ãã//å¤çååºï¼ä»£ç çç¥
ããinstream.close();//å
³éè¾å
¥æµåæ¶ä¼å°è¿æ¥äº¤åè³è¿æ¥å¤çå¨
   2.2 æ¹å¼äºä½¿ç¨é»è®¤çååºå¤çå¨BasicResponseHandler
ããhttpClient Jarå
ä¸æä¾BasicResponseHandlerãå¦æè¿åçç±»åè½ç¡®å®éè¦è§£ç 为Stringç±»åçè¯ï¼æ¨è使ç¨è¯¥ååºå¤çå¨ã
ãã该å¤çå¨è§£ç httpè¿æ¥ååºåèæµä¸ºStringç±»åï¼å¯¹è¿åç >=300çååºè¿è¡äºå¼å¸¸å°è£
ï¼å¹¶è½å¤ä¿è¯è¿æ¥äº¤è¿ç»è¿æ¥æ± 管çå¨ã
ãã该å¤çå¨å°åè解ç 为å符çè¿ç¨ä¾æ¬¡å¦ä¸ï¼
ãã1)å¦æååºhttpæ¥æHeadé¨åç±æå®çcharsetï¼å使ç¨è¯¥charsetè¿è¡è§£ç ï¼å¦åè¿è¡ä¸ä¸æ¥ãä¾å¦ä½¿ç¨UTF-8解ç 以ä¸ååºï¼DEBUG org.apache.http.headers - $amp;
ãã2)å¦æååºæ¥ææªæ§è¡charsetï¼å使ç¨ä¼ å
¥EntityUntils.toString()æ¶æå®çcharsetè¿è¡è§£ç ãå¦åè¿è¡ä¸ä¸æ¥
ãã3)使ç¨ISO-8859-1è¿è¡è§£ç ã
Â
3.å®ç°è¿æ¥æ± ï¼å¾ ç»ï¼
4.éå°çé®é¢
  4.1 è¿æ¥æ± é»å¡ï¼é¢ç¹çæ¥ä»¥ä¸é误信æ¯
pooling post() method executing IOException:java.net.SocketTimeoutException: Read timed out
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at org.apache.http.impl.io.SessionInputBufferImpl.streamRead(SessionInputBufferImpl.java:136)
at org.apache.http.impl.io.SessionInputBufferImpl.fillBuffer(SessionInputBufferImpl.java:152)
at org.apache.http.impl.io.SessionInputBufferImpl.readLine(SessionInputBufferImpl.java:270)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:140)
at org.apache.http.impl.conn.DefaultHttpResponseParser.parseHead(DefaultHttpResponseParser.java:57)
at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:260)
at org.apache.http.impl.DefaultBHttpClientConnection.receiveResponseHeader(DefaultBHttpClientConnection.java:161)
at sun.reflect.GeneratedMethodAccessor96.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.http.impl.conn.CPoolProxy.invoke(CPoolProxy.java:138)
at com.sun.proxy.$Proxy43.receiveResponseHeader(Unknown Source)
at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:271)
at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:123)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:254)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:195)
at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:85)
at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:108)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:72)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:214)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:160)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:136)
 ä¸è¿°é®é¢ä¸»è¦é®é¢æ¯è¿æ¥æ± ä¸httpclientè¿æ¥å¨éå°å¼å¸¸æ åµä¸æªæ£å¸¸å ³é导è´ï¼åºé®é¢æ¶åè¿æ¥æ± å ³äºåæ¶httpclientè¿æ¥ç代ç å¦ä¸ï¼
private ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
int status = response.getStatusLine().getStatusCode();
if (status >= HttpStatus.SC_OK && status < HttpStatus.SC_MULTIPLE_CHOICES) {
HttpEntity entity = response.getEntity();
Charset charset = ContentType.getOrDefault(entity).getCharset();
if (charset == null) {
charset = Charset.forName("UTF-8");
}
return entity != null ? EntityUtils.toString(entity, charset) : null;
} else {
//注æè¿é没æåè¯è¿æ¥æ± è¦åæ¶è¿æ¥
throw new ClientProtocolException("Unexpected response status: " + status);
}
};
 修æ¹åç代ç å¦ä¸ï¼
private ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException {
final StatusLine statusLine = response.getStatusLine();
final HttpEntity entity = response.getEntity();
if (statusLine.getStatusCode() >= HttpStatus.SC_MULTIPLE_CHOICES||statusLine.getStatusCode()<HttpStatus.SC_OK) {
//主å¨åæ¶httpclientè¿æ¥
EntityUtils.consume(entity);
throw new HttpResponseException(statusLine.getStatusCode(),
statusLine.getReasonPhrase());
}
Charset charset = ContentType.getOrDefault(entity).getCharset();
if (charset == null) {
charset = Charset.forName("UTF-8");
}
return entity != null ? EntityUtils.toString(entity, charset) : null;
}
};
Â