httpClient類比瀏覽器發請求

來源:互聯網
上載者:User

標籤:

一、介紹

 httpClient是Apache公司的一個子項目, 用來提高高效的、最新的、功能豐富的支援http協議的用戶端編程工具包。完成可以類比瀏覽器發起請求行為。

二、簡單使用例子 : 類比瀏覽器發起訪問Google首頁請求

1、pom.xml 配置

    <dependency>        <groupId>commons-httpclient</groupId>        <artifactId>commons-httpclient</artifactId>        <version>3.1</version>    </dependency>

2、樣本

import java.io.IOException;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.HttpException;import org.apache.commons.httpclient.HttpMethod;import org.apache.commons.httpclient.methods.GetMethod;public class HttpClientDemo {    public static void main(String[] args) throws HttpException, IOException {                        HttpClient httpClient = new HttpClient();                String uri = "https://www.google.com";        HttpMethod method = new GetMethod(uri);        httpClient.executeMethod(method );                //伺服器返回狀態        print(method.getStatusLine());        //返回的內容        print(method.getResponseBodyAsString());        //釋放串連        method.releaseConnection();    }        static void print(Object o) {        System.out.println(o);    }    }

列印出來的結果是這樣子的

 

httpClient類比瀏覽器發請求

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.