httpclient.get|post处理(连接、响应超时)

 

HttpClient httpclient = new DefaultHttpClient();
  String responseBody = null;

  try
  {
   HttpGet httpGet = new HttpGet(url);
   HttpConnectionParams.setConnectionTimeout(httpGet.getParams(), timeout * 1000);
   HttpConnectionParams.setSoTimeout(httpGet.getParams(), timeout * 1000);

   ResponseHandler responseHandler = new BasicResponseHandler();
   responseBody = httpclient.execute(httpGet, responseHandler);
  }
  finally
  {
   httpclient.getConnectionManager().shutdown();
  }

  return responseBody;

 

 

HttpClient httpclient = new DefaultHttpClient();
  ArrayList params = new ArrayList();
  String responseBody = null;

  try
  {
   String[] nameAndValues = body.split("&");

   for (String nameAndValue : nameAndValues)
   {
    String[] nameValue = nameAndValue.split("=");
    params.add(new BasicNameValuePair(nameValue[0], nameValue[1]));
   }

   HttpPost httpPost = new HttpPost(url);
   HttpConnectionParams.setConnectionTimeout(httpPost.getParams(), timeout * 1000);
   HttpConnectionParams.setSoTimeout(httpPost.getParams(), timeout * 1000);
   httpPost.setEntity(new UrlEncodedFormEntity(params));

   ResponseHandler responseHandler = new BasicResponseHandler();
   responseBody = httpclient.execute(httpPost, responseHandler);

  }
  finally
  {
   httpclient.getConnectionManager().shutdown();
  }

  return responseBody;

 

你可能感兴趣的:(httpclient)