用apache的http包工具进行通信.(java)--有道笔记整理

DefaultHttpClient httpclient = new DefaultHttpClient();

  HttpPost httppost = new HttpPost(url);
  httppost.setHeader("Content-Type", "application/json");
  StringEntity entity;
  try {
   entity = new StringEntity(JsonUtil.object2json(parameters), "UTF-8");
   httppost.setEntity(entity);
  } catch(UnsupportedEncodingException e) {
   logger.error("", e);
  }

  try {
   HttpResponse httpResponse = httpclient.execute(httppost);
   HttpEntity resEntity = httpResponse.getEntity();

   if(null != resEntity) {
    result = IOUtils.toString(resEntity.getContent(), "utf-8");
   }
  } catch(ClientProtocolException e) {
   logger.error(e);
  } catch(IOException e) {
   logger.error(e);
  } finally {
   httpclient.getConnectionManager().shutdown();
  }

你可能感兴趣的:(用apache的http包工具进行通信.(java)--有道笔记整理)