日期:2014-05-18  浏览次数:21529 次

HttpClient I/O exception

HttpClient client = new HttpClient();
PostMethod postMethod = new PostMethod("http://122.22.22.2:2222/Object/HrTms.do");
postMethod.setRequestHeader("Content-type",
"application/x-www-form-urlencoded; charset=UTF-8");
// 泛型List -> Json
java.lang.reflect.Type type = new com.google.gson.reflect.TypeToken<List<Tab_User>>() {
}.getType();

String json = gson.toJson(pageList, type);
_log.debug(serviceUrl + "发送 Hr Json:" + json);
postMethod.setRequestBody(new NameValuePair[] {
new NameValuePair("Type", "Hr"),
new NameValuePair("data", json)
 });
int status = 0;



在发送JSON比较大时,经常会出现一下异常:
[INFO ] [15:52:01] org.apache.commons.httpclient.HttpMethodDirector - I/O exception (org.apache.commons.httpclient.NoHttpResponseException) caught when processing request: The server 122.22.22.2:2222 failed to respond
[INFO ] [15:52:01] org.apache.commons.httpclient.HttpMethodDirector - Retrying request
[INFO ] [15:52:10] org.apache.commons.httpclient.HttpMethodDirector - I/O exception (java.net.ConnectException) caught when processing request: Connection timed out: connect
[INFO ] [15:52:10] org.apache.commons.httpclient.HttpMethodDirector - Retrying request

这就是丢失包?有没有什么办法解决或者尽量减少这种问题?

------解决方案--------------------
把链接的timeout时间设长一点

    /* 连接超时 */
  HttpConnectionParams.setConnectionTimeout(params, 2000);
  /* 读取超时 */
  HttpConnectionParams.setSoTimeout(params, 4000);