1. import java.io.IOException;
  2.  
  3. import javax.ws.rs.core.MediaType;
  4.  
  5. import org.apache.commons.httpclient.HttpClient;
  6. import org.apache.commons.httpclient.HttpException;
  7. import org.apache.commons.httpclient.HttpStatus;
  8. import org.apache.commons.httpclient.methods.GetMethod;
  9. import org.apache.commons.httpclient.methods.PostMethod;
  10. import org.apache.commons.httpclient.methods.PutMethod;
  11. import org.apache.commons.httpclient.methods.RequestEntity;
  12. import org.apache.commons.httpclient.methods.StringRequestEntity;
  13.  
  14. /**
  15. *
  16. * @author Administrator
  17. *
  18. */
  19. public class RestClientUtils {
  20.  
  21. /**
  22. * 发送POST请求
  23. * @param url
  24. * @param object
  25. */
  26. public static String post(String uri, Object content) {
  27.  
  28. String sendContent = content.toString();
  29. String result = null;
  30. PostMethod postMethod = null;
  31. HttpClient httpClient = null;
  32. try {
  33. postMethod = new PostMethod(uri);
  34. httpClient = new HttpClient();
  35. RequestEntity entity = new StringRequestEntity(sendContent,
  36. MediaType.APPLICATION_JSON + ";charset=UTF-8", null);//解决中文乱码问题的方法
  37. postMethod.addRequestHeader("Accept", MediaType.APPLICATION_JSON
  38. + ";charset=UTF-8");
  39. postMethod.setRequestEntity(entity);
  40. int statusCode = httpClient.executeMethod(postMethod);
  41. //如果相应成功
  42. if (statusCode != HttpStatus.SC_OK) {
  43. System.err.println(" Method failed: "
  44. + postMethod.getStatusLine());
  45. return "failed";
  46. }
  47. byte[] responseBody = postMethod.getResponseBody();
  48. result = new String(responseBody, "utf-8");
  49. System.err.println("===================发送POST格式数据请求==================");
  50. System.err.println("返回状态:"+statusCode);
  51. System.err.println("返回结果:");
  52. System.err.println(result);
  53. System.err.println("==============================================");
  54. } catch (HttpException e) {
  55. e.printStackTrace();
  56. } catch (IOException e) {
  57. e.printStackTrace();
  58. } finally {
  59. // 释放连接
  60. if (postMethod != null) {
  61. postMethod.releaseConnection();
  62. }
  63. }
  64. return result;
  65.  
  66. }
  67.  
  68. /**
  69. * 发送GET请求
  70. * @return
  71. */
  72. public static String get(String uri) {
  73. HttpClient httpclient = new HttpClient();
  74. GetMethod getMethod = new GetMethod(uri);
  75. String result = "";
  76. int statusCode = 0;
  77. try {
  78. statusCode = httpclient.executeMethod(getMethod);
  79. result = getMethod.getResponseBodyAsString();
  80. System.err.println("===================发送GET请求==================");
  81. System.err.println("返回状态:"+statusCode);
  82. System.err.println("返回结果:");
  83. System.err.println(result);
  84. System.err.println("==============================================");
  85. } catch (IOException e) {
  86. // TODO Auto-generated catch block
  87. e.printStackTrace();
  88. } finally {
  89. // 释放连接
  90. if (getMethod != null) {
  91. getMethod.releaseConnection();
  92. }
  93. }
  94.  
  95. return result;
  96. }
  97.  
  98. /**
  99. * 发送PUT请求
  100. * @param uri
  101. * @param content
  102. * @return
  103. */
  104. public static String put(String uri, Object content) {
  105. String sendContent = content.toString();
  106. HttpClient httpclient = new HttpClient();
  107. PutMethod putMethod = new PutMethod(uri);
  108. String result = "";
  109. try {
  110.  
  111. RequestEntity entity = new StringRequestEntity(sendContent,
  112. MediaType.APPLICATION_JSON + ";charset=UTF-8", null);//解决中文乱码问题的方法
  113. putMethod.addRequestHeader("Accept", MediaType.APPLICATION_JSON
  114. + ";charset=UTF-8");
  115. putMethod.setRequestEntity(entity);
  116. int statusCode = httpclient.executeMethod(putMethod);
  117. byte[] responseBody = putMethod.getResponseBody();
  118. result = new String(responseBody, "utf-8");
  119. System.err.println("===================发送PUT请求==================");
  120. System.err.println("返回状态:"+statusCode);
  121. System.err.println("返回结果:");
  122. System.err.println(result);
  123. System.err.println("==============================================");
  124. } catch (Exception e) {
  125. e.printStackTrace();
  126. } finally {
  127. // 释放连接
  128. if (null != putMethod) {
  129. putMethod.releaseConnection();
  130. }
  131. }
  132. return result;
  133. }
  134. }

我的HttpClients工具的更多相关文章

  1. HTTP Client工具类

    HTTP Client工具类: import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.ht ...

  2. HttpClient4.5 SSL访问工具类

    要从网上找一个HttpClient SSL访问工具类太难了,原因是HttpClient版本太多了,稍有差别就不能用,最后笔者干脆自己封装了一个访问HTTPS并绕过证书工具类. 主要是基于新版本Http ...

  3. 使用java开源工具httpClient及jsoup抓取解析网页数据

    今天做项目的时候遇到这样一个需求,需要在网页上展示今日黄历信息,数据格式如下 公历时间:2016年04月11日 星期一 农历时间:猴年三月初五 天干地支:丙申年 壬辰月 癸亥日 宜:求子 祈福 开光 ...

  4. HttpClient 工具

    什么是httpclient HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.虽然在 JDK 的 ja ...

  5. Android Studio 插件开发详解二:工具类

    转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/78112856 本文出自[赵彦军的博客] 在插件开发过程中,我们按照开发一个正式的项 ...

  6. http和https工具类 (要注意httpclient版本号和log4j的版本号)

    1 工具类 package dd.com; import java.io.IOException; import java.security.cert.CertificateException; im ...

  7. 高德地图web端笔记;发送http请求的工具类

    1.查询所有电子围栏 package com.skjd.util; import java.io.BufferedReader; import java.io.InputStream; import ...

  8. HttpClientUtil 工具类

    /* * * * FileName: s.java * * Description:TODO(用一句话描述该文件做什么) * * Created: jiangzhanghong 2017年11月14日 ...

  9. 带SSL证书的httpclient 远程接口工具类

    package com.iups.wx.util; import java.io.IOException; import java.io.UnsupportedEncodingException; i ...

随机推荐

  1. gitlab一键安装

    参考 https://about.gitlab.com/downloads/

  2. 查询,创建,扩充表空间&&impdp--------表空间大全

    周六晚上还在办公室导入数据. 按schemas导入成功的关键是:导入的环境和源数据环境里面的表空间大小,表空间名字,需要一模一样 (当然,表空间大小创建到和源数据环境里面ues_size大小就可以了) ...

  3. UITableView beginUpdate和endUpdate使用的前提

    转载地址:http://blog.csdn.net/vieri_ch/article/details/46893023 UITableView有两个方法,用于单元格动画变化的方法,beginUpdat ...

  4. Android(java)学习笔记201:网络图片浏览器的实现(ANR)

    1.我们在Android下,实现使用http协议进行网络通信,请求网络数据.这里是获取网络上的图片信息,让它可以显示在手机上: 但是我们这个手机连接网络是很费时间,如果我们在主线程(UI线程)中写这个 ...

  5. javascript 高级程序设计(三)-数据类型

    ECMAScript 中所有类型的值都有与这两个Boolean值等价的值   数据类型     转换为true的值   转换为false的值 Boolean true   false( String ...

  6. oracle的安装与plsql的环境配置

    1,首先得有oracle的安装包和plsql的安装包,安装包地址可见百度云 http://pan.baidu.com/s/1miTqhmg 2.解压下来进入0817账套,找到set.exe文件,双击安 ...

  7. CakePHP之请求与响应对象

    请求与响应对象 请求与响应对象在 CakePHP 2.0 是新增加的.在之前的版本中,这两个对象是由数组表示的,而相关的方法是分散在RequestHandlerComponent,Router,Dis ...

  8. 委托、 Lambda表达式和事件——事件

    /* * 由SharpDevelop创建. * 用户: David Huang * 日期: 2015/7/31 * 时间: 14:21 */ using System; namespace 事件 { ...

  9. ratingBar抢焦点问题

    ratingBar抢viewpager焦点问题: 1)写一个类继承ratingBar,让onTouchevent或者dispatchTouchEvent返回false 2)设置ratingBar的属性 ...

  10. requirejs+anjularjs+express框架

    1.目录 2.首页login.html如下: <!DOCTYPE html><html> <head> <title>登录界面</title> ...