http发送get、post请求

深藏阁楼爱情的钟 2022-07-14 02:52 387阅读 0赞

发送get、post请求简单举例,代码如下:

  1. import com.google.common.base.Stopwatch;
  2. import com.google.common.base.Strings;
  3. import org.apache.commons.httpclient.*;
  4. import org.apache.commons.httpclient.methods.GetMethod;
  5. import org.apache.commons.httpclient.params.HttpMethodParams;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.client.methods.HttpPost;
  10. import org.apache.http.impl.client.HttpClients;
  11. import org.apache.http.util.EntityUtils;
  12. import org.mortbay.util.UrlEncoded;
  13. import java.io.*;
  14. import java.net.URL;
  15. import java.net.URLConnection;
  16. import java.util.List;
  17. import java.util.Map;
  18. import java.util.concurrent.TimeUnit;
  19. public class HttpClientTest {
  20. //*根据url得到数据
  21. public static void httpClientTest() throws IOException {
  22. final Stopwatch stopwatch = Stopwatch.createUnstarted().start();
  23. String responseMsg = "";
  24. HttpClient httpClient = new HttpClient();
  25. String id = "~IgOMOo15IUf1NfENGQV+Gw==~1~_116476553_6";
  26. String url = "http://xxxxxx/" + UrlEncoded.encodeString(id);
  27. GetMethod getMethod = new GetMethod(url);
  28. getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
  29. //3.执行getMethod,调用http接口
  30. httpClient.executeMethod(getMethod);
  31. //4.读取内容
  32. byte[] responseBody = getMethod.getResponseBody();
  33. //5.处理返回的内容
  34. responseMsg = new String(responseBody);
  35. System.out.println(responseMsg);
  36. System.out.println("query cost 1: " + stopwatch.elapsed(TimeUnit.MILLISECONDS));
  37. }
  38. //根据url得到数据
  39. public static void httpGet(){
  40. final Stopwatch stopwatch = Stopwatch.createUnstarted().start();
  41. try {
  42. String id = "~IgOMOo15IUf1NfENGQV+Gw==~1~_116476553_6";
  43. //String url = "https://uland.taobao.com/coupon/edetail?e=huuWrBr87UIN%2BoQUE6FNzLHOv%2FKoJ4LsM%2FOCBWMCflrRpLchICCDoqC1%2FGlh90mJfFEjLtoHpPCLQAGMzNbHRBpywujSvOp2nUIklpPPqYL9BYNf2%2FonKY57aXDe3JrR2pd4uaXWR8D1g9gleiedVtDvjgqDLA1q&pid=mm_115940806_14998982_61982315&af=1";
  44. String url = "https://shop.m.taobao.com/shop/coupon.htm?sellerId=696902416&activityId=4aee3e6538d94b518e19b53d3b04f30c";
  45. // 创建HttpClient对象
  46. org.apache.http.client.HttpClient client = HttpClients.createDefault();
  47. // 创建GET请求(在构造器中传入URL字符串即可)
  48. HttpGet get = new HttpGet(url);
  49. // 调用HttpClient对象的execute方法获得响应
  50. HttpResponse response = client.execute(get);
  51. response.setHeader("content-type", "application/json");
  52. ///response.setEntity(new StringEntity(query, "utf-8"));
  53. // 调用HttpResponse对象的getEntity方法得到响应实体
  54. HttpEntity httpEntity = response.getEntity();
  55. // 使用EntityUtils工具类得到响应的字符串表示
  56. String result = EntityUtils.toString(httpEntity,"utf-8");
  57. System.out.println(result);
  58. }catch (UnsupportedEncodingException e) {
  59. e.printStackTrace();
  60. }catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. System.out.println("query cost 2:" + stopwatch.elapsed(TimeUnit.MILLISECONDS));
  64. }
  65. //可以根据实际的query得到数据
  66. public static void httpPost(){
  67. final Stopwatch stopwatch = Stopwatch.createUnstarted().start();
  68. try {
  69. String query = "{\"from\":0,\"size\":10,\"query\":{\"bool\":{\"must\":[{\"match\":{\"buyer_nick\":{\"query\":\"11杨柳絮语言\",\"type\":\"phrase\"}}}]}}}";
  70. // String url = "http://xxxxx:9200/index_address/address/_search";
  71. String url = "https://shop.m.taobao.com/shop/coupon.htm?sellerId=696902416&activityId=4aee3e6538d94b518e19b53d3b04f30c";
  72. // 创建HttpClient对象
  73. org.apache.http.client.HttpClient client = HttpClients.createDefault();
  74. // 创建POST请求
  75. HttpPost post = new HttpPost(url);
  76. // 向POST请求中添加消息实体
  77. //post.setHeader("content-type", "application/json");
  78. //post.setEntity(new StringEntity(query, "utf-8"));
  79. // 得到响应并转化成字符串
  80. String responseMsg = "";
  81. HttpResponse response = client.execute(post);
  82. if(response.getStatusLine().getStatusCode() == 302){
  83. String locationUrl = response.getLastHeader("Location").getValue();
  84. System.out.println(locationUrl);
  85. } else {
  86. HttpEntity httpEntity = response.getEntity();
  87. String result = EntityUtils.toString(httpEntity, "UTF-8");
  88. System.out.println(result);
  89. }
  90. }catch (Exception e){
  91. } finally {
  92. }
  93. System.out.println("query cost 3:" + stopwatch.elapsed(TimeUnit.MILLISECONDS));
  94. }
  95. /** * 执行一个HTTP GET请求,返回请求响应的HTML * * @return 返回请求响应的HTML */
  96. public static String doGet() {
  97. String url = "https://shop.m.taobao.com/shop/coupon.htm?sellerId=696902416&activityId=4aee3e6538d94b518e19b53d3b04f30c";
  98. String response = null;
  99. HttpClient client = new HttpClient();
  100. HttpMethod method = new GetMethod(url);
  101. try {
  102. client.executeMethod(method);
  103. if (method.getStatusCode() == HttpStatus.SC_OK) {
  104. response = method.getResponseBodyAsString();
  105. }
  106. } catch (URIException e) {
  107. System.out.println("执行HTTP Get请求时,编码查询字符串");
  108. } catch (IOException e) {
  109. System.out.println("执行HTTP Get请求" + url + "时,发生异常!");
  110. } finally {
  111. method.releaseConnection();
  112. }
  113. return response;
  114. }
  115. }

发表评论

表情:
评论列表 (有 0 条评论,387人围观)

还没有评论,来说两句吧...

相关阅读

    相关 iOS发送http请求

    前言 苹果公司在全球开发者大会(WWDC)的一场安全演示会上,公布了一个最后期限——2017 年 1 月 1 日——即 App Store 当中的所有应用必须在这个日期之