java 发送 http请求——HttpClient

使用HttpClient来发送Http请求

引入两个包:[1]org.apache.httpcomponents.httpclient_x.x.x.jar  [2]org.apache.httpcomponents.httpcore_x.x.x.jar

下载链接:Apache HttpComponents - HttpComponents Downloads

参考文档:[1]HttpClient Tutorial  [2]HttpClient Example


 1 package http;
 2
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5
 6 import org.apache.http.Header;
 7 import org.apache.http.HttpEntity;
 8 import org.apache.http.ProtocolVersion;
 9 import org.apache.http.StatusLine;
10 import org.apache.http.client.ClientProtocolException;
11 import org.apache.http.client.config.RequestConfig;
12 import org.apache.http.client.methods.CloseableHttpResponse;
13 import org.apache.http.client.methods.HttpGet;
14 import org.apache.http.impl.client.CloseableHttpClient;
15 import org.apache.http.impl.client.HttpClients;
16
17
18
19 public class HttpConnector {
20     public static void main(String[] args) {
21         CloseableHttpClient httpClient = HttpClients.createDefault();
22         RequestConfig requestConfig = RequestConfig.custom()
23                 .setConnectTimeout(1000)
24                 .setSocketTimeout(1000)
25                 .build();
26         HttpGet get = new HttpGet("http://i.easou.com/s.m?q=电影&wver=t");
27         get.setConfig(requestConfig);
28
29         /*
30             HttpPost post = new HttpPost("http://i.easou.com/s.m");
31             post.setConfig(requestConfig);
32             List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
33             nameValuePairs.add(new BasicNameValuePair("q", "电影"));
34             nameValuePairs.add(new BasicNameValuePair("wver", "t"));
35             UrlEncodedFormEntity urlEntity;
36             urlEntity = new UrlEncodedFormEntity(nameValuePairs,Consts.UTF_8);
37             post.setEntity(urlEntity);
38         */
39         CloseableHttpResponse response = null;
40         try {
41             response = httpClient.execute(get);
42             /*
43                 response = httpClient.execute(post);
44             */
45             StatusLine statusLine = response.getStatusLine();
46             ProtocolVersion protocolVersion = statusLine.getProtocolVersion();
47             String reasonPhrase = statusLine.getReasonPhrase();
48             int statusCode = statusLine.getStatusCode();
49             System.out.println(statusLine + "\n" + protocolVersion + " " + statusCode + " " + reasonPhrase);
50             /*#############################################################################################*/
51             HttpEntity httpEntity = response.getEntity();
52             if(httpEntity != null) {
53                 Header contentType = httpEntity.getContentType();
54                 Header contentEncoding = httpEntity.getContentEncoding();
55                 long contentLength = httpEntity.getContentLength();
56                 boolean isStreaming = httpEntity.isStreaming();
57                 boolean isRepeatable = httpEntity.isRepeatable();
58                 boolean isChunked = httpEntity.isChunked();
59                 System.out.println(contentType + " | " + contentEncoding + " | " + contentLength + " | " + isStreaming + "/" + isRepeatable + "/" + isChunked);
60                 /*#############################################################################################*/
61                 InputStream is = httpEntity.getContent();
62                 byte[] buff = new byte[512];
63                 while(is.read(buff) > 0) {
64                     for(byte b : buff) {
65                         System.out.print((char) b);
66                     }
67                 }
68                 is.close();
69                 /*#############################################################################################*/
70             }
71         } catch (ClientProtocolException e) {
72             e.printStackTrace();
73         } catch (IOException e) {
74             e.printStackTrace();
75         } finally {
76             if(response != null) {
77                 try {
78                     response.close();
79                 } catch (IOException e) {
80                     e.printStackTrace();
81                 }
82             }
83         }
84         try {
85             httpClient.close();
86         } catch (IOException e) {
87             e.printStackTrace();
88         }
89     }
90
91 }
时间: 2024-10-01 04:39:38

java 发送 http请求——HttpClient的相关文章

Java发送HTTPS请求

前言 上篇文章介绍了 java 发送 http 请求,大家都知道发送http是不安全的 .我也是由于对接了其他企业后总结了一套发送 https的工具.大家网上找方法很多的,但是可不是你粘过来就能用啊,我也是踩过坑的,所以我这个工具,只要粘贴到你们自己项目里就可以用.我的工具跟网上没什么区别,唯一的区别是我亲身实战过,把需要注意的细节列出来,不让大家浪费时间.   正文 本文只介绍 发送 post 请求,既然选择了 https 就不会用get,因为get也是不安全的.   读前须知 我会把需要依赖

关于JAVA发送Https请求(HttpsURLConnection和HttpURLConnection)

关于JAVA发送Https请求(HttpsURLConnection和HttpURLConnection) [转] https协议对于开发者而言其实只是多了一步证书验证的过程.这个证书正常情况下被jdk/jre/security/cacerts所管理.里面证书包含两种情况: 1.机构所颁发的被认证的证书,这种证书的网站在浏览器访问时https头显示为绿色如百度 2.个人所设定的证书,这种证书的网站在浏览器里https头显示为红色×,且需要点击信任该网站才能继续访问.而点击信任这一步的操作就是我们

Java发送Http请求

package com.liuyu.test; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public c

java 发送http请求

package cn.go4mi; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public class H

Java发送POST请求,参数为JSON格式,并接收返回JSON数据

原文地址:https://blog.csdn.net/qq_26975307/article/details/82713725 /** * 发送post请求 * @param url 路径 * @param jsonObject 参数(json类型) * @param encoding 编码格式 * @return * @throws ParseException * @throws IOException */ public static String send(String url, JSO

java发送 get请求

package com.java.base; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class javaHTTPHandle { /**     * 向指定URL发送GET方法的请求     * @param url     *            发送请求的URL     *

java发送http请求(提交form表单)

http://hbiao68.iteye.com/blog/1973914 写一个servlet用于测试请求 Java代码   import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import

java 发送 http 请求

POM 依赖 <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</gro

Java发送Post请求

//访问准备 URL url = new URL("http:***"); //post参数 Map<String,Object> params = new LinkedHashMap<>(); params.put("geomInfo", ""); params.put("f", "json"); //开始访问 StringBuilder postData = new String