关闭

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

186人阅读 评论(0) 收藏 举报
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();
  }

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:23927次
    • 积分:959
    • 等级:
    • 排名:千里之外
    • 原创:68篇
    • 转载:34篇
    • 译文:0篇
    • 评论:8条
    最新评论