HTTPSend.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package qh.lqg.utils.net;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.PrintWriter;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.net.URLEncoder;
  9. import java.util.List;
  10. /**
  11. * Created by XdaTk on 2014/12/21.
  12. * <p/>
  13. * HTTP请求工具类
  14. */
  15. public class HTTPSend {
  16. /**
  17. * 发送get请求
  18. *
  19. * @param url
  20. * 请求地址
  21. * @param list
  22. * 请求参数
  23. *
  24. * @return 请求结果
  25. *
  26. * @throws IOException
  27. */
  28. public static String sendGet(String url, List<HTTPParam> list)
  29. throws IOException {
  30. StringBuffer buffer = new StringBuffer(); // 用来拼接参数
  31. StringBuffer result = new StringBuffer(); // 用来接受返回值
  32. URL httpUrl = null; // HTTP URL类 用这个类来创建连接
  33. URLConnection connection = null; // 创建的http连接
  34. BufferedReader bufferedReader = null; // 接受连接受的参数
  35. // 如果存在参数,我们才需要拼接参数 类似于 localhost/index.html?a=a&b=b
  36. if (list.size() > 0) {
  37. for (int i = 0; i < list.size(); i++) {
  38. buffer.append(list.get(i).getKey())
  39. .append("=")
  40. .append(URLEncoder.encode(list.get(i).getValue(),
  41. "utf-8"));
  42. // 如果不是最后一个参数,不需要添加&
  43. if ((i + 1) < list.size()) {
  44. buffer.append("&");
  45. }
  46. }
  47. url = url + "?" + buffer.toString();
  48. }
  49. // 创建URL
  50. httpUrl = new URL(url);
  51. // 建立连接
  52. connection = httpUrl.openConnection();
  53. connection
  54. .setRequestProperty("accept",
  55. "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  56. connection.setRequestProperty("connection", "keep-alive");
  57. connection
  58. .setRequestProperty("user-agent",
  59. "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
  60. connection.connect();
  61. // 接受连接返回参数
  62. bufferedReader = new BufferedReader(new InputStreamReader(
  63. connection.getInputStream()));
  64. String line;
  65. while ((line = bufferedReader.readLine()) != null) {
  66. result.append(line);
  67. }
  68. bufferedReader.close();
  69. return result.toString();
  70. }
  71. /**
  72. * 发送Post请求
  73. *
  74. * @param url
  75. * 请求地址
  76. * @param list
  77. * 请求参数
  78. *
  79. * @return 请求结果
  80. *
  81. * @throws IOException
  82. */
  83. public static String sendPost(String url, List<HTTPParam> list)
  84. throws IOException {
  85. StringBuffer buffer = new StringBuffer(); // 用来拼接参数
  86. StringBuffer result = new StringBuffer(); // 用来接受返回值
  87. URL httpUrl = null; // HTTP URL类 用这个类来创建连接
  88. URLConnection connection = null; // 创建的http连接
  89. PrintWriter printWriter = null;
  90. BufferedReader bufferedReader; // 接受连接受的参数
  91. // 创建URL
  92. httpUrl = new URL(url);
  93. // 建立连接
  94. connection = httpUrl.openConnection();
  95. connection
  96. .setRequestProperty("accept",
  97. "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  98. connection.setRequestProperty("connection", "keep-alive");
  99. connection
  100. .setRequestProperty("user-agent",
  101. "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
  102. connection.setDoOutput(true);
  103. connection.setDoInput(true);
  104. printWriter = new PrintWriter(connection.getOutputStream());
  105. if (list.size() > 0) {
  106. for (int i = 0; i < list.size(); i++) {
  107. buffer.append(list.get(i).getKey())
  108. .append("=")
  109. .append(URLEncoder.encode(list.get(i).getValue(),
  110. "utf-8"));
  111. // 如果不是最后一个参数,不需要添加&
  112. if ((i + 1) < list.size()) {
  113. buffer.append("&");
  114. }
  115. }
  116. }
  117. printWriter.print(buffer.toString());
  118. printWriter.flush();
  119. connection.connect();
  120. // 接受连接返回参数
  121. bufferedReader = new BufferedReader(new InputStreamReader(
  122. connection.getInputStream()));
  123. String line;
  124. while ((line = bufferedReader.readLine()) != null) {
  125. result.append(line);
  126. }
  127. bufferedReader.close();
  128. return result.toString();
  129. }
  130. }