java+httpclient—— 一個簡單的post請求


package jkcs;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

public class jiekoumoni 
{

    public static void main(String[] args) throws ClientProtocolException, IOException, Exception 
    {
    
        
        CloseableHttpClient client = HttpClients.createDefault();    //創建一個http客戶端
        
        HttpPost httpPost = new HttpPost("http://www.oschina.net/");// 通過httpPost方式來實現我們的get請求
        
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36");//偽裝成瀏覽器請求
        
        CloseableHttpResponse Response = client.execute(httpPost);  // 通過client調用execute方法,得到我們的執行結果就是一個response,所有的數據都封裝在response里面了
        
        
        //-----------------------------------------------------------------------

        
        System.out.println(Response.getStatusLine());  //HTTP/1.1 200 OK
        
        System.out.println(Response.getProtocolVersion());  //HTTP/1.1
        
        System.out.println(Response.getStatusLine().getStatusCode());  //200
        
        
        //-----------------------------------------------------------------------
        
        
        
        HttpEntity httpEntity = Response.getEntity();         //獲取某個固定的響應頭
        
        System.out.println(httpEntity.getContentType()); //Content-Type: text/html
        
        

        
        
        //-----------------------------------------------------------------------
        
        
        System.out.println(Response.getFirstHeader("Content-Type"));//Content-Type: text/html
        System.out.println(Response.getFirstHeader("Date"));   //Date: Sun, 03 May 2020 06:58:16 GMT
        
        //-----------------------------------------------------------------------
        
        Header[] headers = Response.getAllHeaders();    //獲取所有響應頭
        for (Header header : headers) 
        {
           System.out.println("Key : " + header.getName()+",         ," +" Value : " + header.getValue());
        }
        
        
        
        //-----------------------------------------------------------------------
        
        
        
        Response.close();  // 關閉
        
        
        

    }

}

 

 

執行結果:

 

HTTP/1.1 301 Moved Permanently
HTTP/1.1
301


Content-Type: text/html
Content-Type: text/html
Date: Sun, 03 May 2020 07:47:35 GMT


Key : Date, , Value : Sun, 03 May 2020 07:47:35 GMT
Key : Content-Type, , Value : text/html
Key : Content-Length, , Value : 239
Key : Connection, , Value : keep-alive
Key : Server, , Value : Tengine
Key : Location, , Value : https://www.oschina.net/

 


免責聲明!

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



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