您的位置:首页 > 理论基础 > 计算机网络

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

2015-11-19 17:46 585 查看
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();
  }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: