[Java语言] Java网络请求工具类

Java网络请求工具类(依赖:org.apache.http;注:HttpClient 4.4,HttpCore 4.4) 
到此处可以去下载依赖包:http://hc.apache.org/downloads.cgi 
  1. import java.util.List;
  2. import org.apache.http.HttpStatus;
  3. import org.apache.http.NameValuePair;
  4. import org.apache.http.client.config.RequestConfig;
  5. import org.apache.http.client.entity.UrlEncodedFormEntity;
  6. import org.apache.http.client.methods.CloseableHttpResponse;
  7. import org.apache.http.client.methods.HttpGet;
  8. import org.apache.http.client.methods.HttpPost;
  9. import org.apache.http.impl.client.CloseableHttpClient;
  10. import org.apache.http.impl.client.HttpClients;
  11. import org.apache.http.util.EntityUtils;
  12. http://www.nvzi91.cn/LEEP/30054.html
  13. /**
  14. * HttpServletUtil
  15. *
  16. * @author ysj
  17. * @Date: 2015-1-30 下午2:07:55
  18. */
  19. public class HttpServletUtil {
  20. private static CloseableHttpClient httpclient;
  21. private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
  22. /**
  23. * Post:访问数据库并返回数据字符串
  24. *http://www.nvzi91.cn/kunmingrenliu/041930055.html
  25. * @param params
  26. * 向服务器端传的参数
  27. * @param url
  28. * @return String 数据字符串
  29. * @throws Exception
  30. */
  31. public static String doPost(List<NameValuePair> params, String url) throws Exception {
  32. String result = null;
  33. httpclient = HttpClients.createDefault();
  34. HttpPost httpPost = new HttpPost(url);
  35. httpPost.setEntity(new UrlEncodedFormEntity(params));
  36. //设置请求和传输超时时间
  37. httpPost.setConfig(requestConfig);
  38. CloseableHttpResponse httpResp = httpclient.execute(httpPost);
  39. try {
  40. int statusCode = httpResp.getStatusLine().getStatusCode();
  41. // 判断是够请求成功
  42. if (statusCode == HttpStatus.SC_OK) {
  43. System.out.println("状态码:" + statusCode);
  44. System.out.println("请求成功!");
  45. // 获取返回的数据
  46. result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
  47. } else {
  48. System.out.println("状态码:"
  49. + httpResp.getStatusLine().getStatusCode());
  50. System.out.println("HttpPost方式请求失败!");
  51. }
  52. } finally {
  53. httpResp.close();
  54. httpclient.close();
  55. }http://www.nvzi91.cn/shengzhizhengxing/30056.html
  56. return result;
  57. }
  58. /**
  59. * Get:访问数据库并返回数据字符串
  60. *
  61. * @param url
  62. * @return String 数据字符串
  63. * @throws Exception
  64. */
  65. public static String doGet(String url) throws Exception{
  66. String result = null;
  67. httpclient = HttpClients.createDefault();
  68. HttpGet httpGet = new HttpGet(url);
  69. //设置请求和传输超时时间
  70. httpGet.setConfig(requestConfig);
  71. CloseableHttpResponse httpResp = httpclient.execute(httpGet);
  72. try {
  73. int statusCode = httpResp.getStatusLine().getStatusCode();
  74. // 判断是够请求成功
  75. if (statusCode == HttpStatus.SC_OK) {
  76. System.out.println("状态码:" + statusCode);
  77. System.out.println("请求成功!");
  78. // 获取返回的数据
  79. result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
  80. } else {http://www.nvzi91.cn/luanchaonanzhong/30057.html
  81. System.out.println("状态码:"
  82. + httpResp.getStatusLine().getStatusCode());
  83. System.out.println("HttpGet方式请求失败!");
  84. }http://www.nvzi91.cn/fujianyan/30058.html
  85. } finally {
  86. httpResp.close();
  87. httpclient.close();
  88. }www.nvzi91.cn
  89. return result;
  90. }
  91. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值