java中發送http請求


使用HttpClient發送http請求:

 1 public String cawl(String url){
 2         try {
 3             CloseableHttpClient httpClient = HttpClientBuilder.create().build();//初始化
 4             CloseableHttpResponse httpResponse = httpClient.execute(new HttpGet(url));//獲取頁面信息
 5             String result = EntityUtils.toString(httpResponse.getEntity());//將對象轉換成字符串輸出
 6             return result;
 7         } catch (IOException e) {
 8             throw new RuntimeException(e);
 9         }
10  }

使用Url發送http請求:

抽象類URLConnection:所有類的超類,它代表應用程序和 URL 之間的通信鏈接。此類的實例可用於讀取和寫入此 URL 引用的資源

 1  try {
 2             URL url = new URL("http://www.baidu.com");
 3             URLConnection connection = url.openConnection();
 4             for (int i = 1;;i++){
 5                 String header = connection.getHeaderField(i);
 6                 if (header==null)break;
 7                 System.out.println(connection.getHeaderFieldKey(i)+":"+header);
 8             }
 9         } catch (java.io.IOException e) {
10             e.printStackTrace();
11         }

底層方面使用,Socket來發送http請求:

 1 public void testCustom(){
 2         try {
 3             Socket socket = new Socket();
 4             InetSocketAddress addr = new InetSocketAddress("baidu.com",80);
 5             socket.connect(addr);
 6             InputStream in = socket.getInputStream();
 7             new Thread(new GetInfo(in)).start();//開線程來讀取信息
 8             OutputStream out = socket.getOutputStream();
 9             out.write(("GET / HTTP/1.1\r\nHost: baidu.com\r\n" +     //  \r\n代表換行
10                     "Accept-Language: zh-CN,zh;q=0.9\r\n" +
11                     "Accept-Encoding: gzip, deflate, br\r\n" +
12                     "User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) " +
13                     "AppleWebKit/537.36 (KHTML, like Gecko) " +
14                     "Chrome/63.0.3239.108 Safari/537.36\r\n\r\n").getBytes());
15             out.flush();
16             try {
17                 Thread.sleep(2000);
18             } catch (InterruptedException e) {
19                 e.printStackTrace();
20             }
21 
22         } catch (IOException e) {
23             e.printStackTrace();
24         }
25     }

因為baidu.com采用的是https協議,使用了SSL加密,所以控制台輸出以下信息:

 ready to read
HTTP/1.1 302 Moved Temporarily
Server: bfe/1.0.8.18
Date: Thu, 15 Mar 2018 08:26:58 GMT
Content-Type: text/html
Content-Length: 161
Connection: Keep-Alive
Location: https://www.baidu.com/
Expires: Fri, 16 Mar 2018 08:26:58 GMT
Cache-Control: max-age=86400
Cache-Control: privae

當網址換成http協議時,出現了以下的信息:

ready to read
HTTP/1.1 200 OK
Server: nginx
Date: Thu, 15 Mar 2018 08:32:04 GMT
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Cache-Control: max-age=86400
Last-Modified: Thu, 15 Mar 2018 08:32:04 GMT
Expires: Fri, 16 Mar 2018 08:32:04 GMT
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Encoding: gzip

2228


因為網站的文檔編碼是:gzip,需要進行解碼才能讀取,而后直接讀取apache的網址,就出現header+頁面代碼,就不附控制台的輸出結果了。

參考文檔:

http://www.jb51.net/article/114738.htm

http://blog.csdn.net/u010197591/article/details/51441399

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM