【Java】通过 Httpclient 发送 GET and POST请求

悠悠 2023-10-17 09:59 43阅读 0赞

通过 Httpclient 发送 GET and POST请求

  1. /**
  2. * Created by CF on 2017/1/6.
  3. */
  4. import java.io.IOException;
  5. import java.io.UnsupportedEncodingException;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.NameValuePair;
  10. import org.apache.http.client.entity.UrlEncodedFormEntity;
  11. import org.apache.http.client.methods.CloseableHttpResponse;
  12. import org.apache.http.client.methods.HttpGet;
  13. import org.apache.http.client.methods.HttpPost;
  14. import org.apache.http.impl.client.CloseableHttpClient;
  15. import org.apache.http.impl.client.HttpClients;
  16. import org.apache.http.message.BasicNameValuePair;
  17. import org.apache.http.util.EntityUtils;
  18. import org.springframework.beans.factory.annotation.Value;
  19. public class HttpRequestUtils {
  20. //成功
  21. private static boolean TYPE=true;
  22. @Value("${base.web.url}")
  23. private static String serverHost;
  24. /**
  25. * 通过GET方式发起http请求
  26. */
  27. public static boolean requestByGetMethod(String url){
  28. //创建默认的httpClient实例
  29. CloseableHttpClient httpClient = getHttpClient();
  30. try {
  31. //用get方法发送http请求
  32. HttpGet get = new HttpGet(url);
  33. System.out.println("执行get请求:...."+get.getURI());
  34. CloseableHttpResponse httpResponse = null;
  35. //发送get请求
  36. httpResponse = httpClient.execute(get);
  37. try{
  38. //response实体
  39. HttpEntity entity = httpResponse.getEntity();
  40. if (null != entity){
  41. System.out.println("响应状态码:"+ httpResponse.getStatusLine());
  42. System.out.println("-------------------------------------------------");
  43. System.out.println("响应内容:" + EntityUtils.toString(entity));
  44. System.out.println("-------------------------------------------------");
  45. }
  46. }
  47. finally{
  48. httpResponse.close();
  49. }
  50. } catch (Exception e) {
  51. e.printStackTrace();//TODO
  52. return TYPE=false;
  53. }
  54. finally{
  55. try{
  56. closeHttpClient(httpClient);
  57. } catch (IOException e){
  58. e.printStackTrace();
  59. }
  60. }
  61. return TYPE;
  62. }
  63. /**
  64. * POST方式发起http请求
  65. */
  66. public static boolean requestByPostMethod(String url){
  67. CloseableHttpClient httpClient = getHttpClient();
  68. try {
  69. HttpPost post = new HttpPost(url); //这里用上本机的某个工程做测试
  70. //创建参数列表
  71. List<NameValuePair> list = new ArrayList<NameValuePair>();
  72. list.add(new BasicNameValuePair("j_username", "admin"));
  73. list.add(new BasicNameValuePair("j_password", "admin"));
  74. //url格式编码
  75. UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(list,"UTF-8");
  76. post.setEntity(uefEntity);
  77. System.out.println("POST 请求...." + post.getURI());
  78. //执行请求
  79. CloseableHttpResponse httpResponse = httpClient.execute(post);
  80. try{
  81. HttpEntity entity = httpResponse.getEntity();
  82. if (null != entity){
  83. System.out.println("-------------------------------------------------------");
  84. System.out.println(EntityUtils.toString(uefEntity));
  85. System.out.println("-------------------------------------------------------");
  86. }
  87. } finally{
  88. httpResponse.close();
  89. }
  90. } catch( UnsupportedEncodingException e){
  91. e.printStackTrace();
  92. return TYPE=false;//TODO
  93. }
  94. catch (IOException e) {
  95. e.printStackTrace();
  96. }
  97. finally{
  98. try{
  99. closeHttpClient(httpClient);
  100. } catch(Exception e){
  101. e.printStackTrace();
  102. }
  103. }
  104. return TYPE;
  105. }
  106. private static CloseableHttpClient getHttpClient(){
  107. return HttpClients.createDefault();
  108. }
  109. private static void closeHttpClient(CloseableHttpClient client) throws IOException{
  110. if (client != null){
  111. client.close();
  112. }
  113. }
  114. public static void main(String[] args) {
  115. boolean a= requestByGetMethod("http://192.168.2.163:8999/");
  116. System.out.print("a = " + a);
  117. }
  118. }

maven配置:

  1. <dependency>
  2. <groupId>org.apache.httpcomponents</groupId>
  3. <artifactId>httpclient</artifactId>
  4. <version>4.3.5</version>
  5. </dependency>

发表评论

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

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

相关阅读