HttpClient模仿瀏覽器調用服務

1、什么是httpclient

HTTP 協(xié)議可能是現(xiàn)在 Internet 上使用得最多、最重要的協(xié)議了,越來越多的 Java 應用程序需要直接通過 HTTP 協(xié)議來訪問網(wǎng)絡資源。雖然在 JDK 的 java net包中已經(jīng)提供了訪問 HTTP 協(xié)議的基本功能,但是對于大部分應用程序來說,JDK 庫本身提供的功能還不夠豐富和靈活。HttpClient 是 Apache Jakarta Common 下的子項目,用來提供高效的、最新的、功能豐富的支持 HTTP 協(xié)議的客戶端編程工具包,并且它支持 HTTP 協(xié)議最新的版本和建議。
下載地址:http://hc.apache.org/

2、功能介紹

以下列出的是 HttpClient 提供的主要的功能,要知道更多詳細的功能可以參見 HttpClient 的主頁。
(1)實現(xiàn)了所有 HTTP 的方法(GET,POST,PUT,HEAD 等)
(2)支持自動轉向
(3)支持 HTTPS 協(xié)議
(4)支持代理服務器等

3、導入依賴

        <!-- httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.3.5</version>
        </dependency>

4、執(zhí)行GET請求

客戶端

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

public class HttpClientTest {
    @Test
    public void getTest() throws Exception {
       //創(chuàng)建一個httpclient對象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //創(chuàng)建一個uri對象
        URIBuilder uriBuilder = new URIBuilder("http://localhost:8082/httpclient/get.html");
        uriBuilder.addParameter("wd", "花千骨");
        HttpGet get = new HttpGet(uriBuilder.build());

        //執(zhí)行請求
        CloseableHttpResponse response = httpClient.execute(get);

        //取響應的結果
        int statusCode = response.getStatusLine().getStatusCode();
        System.out.println(statusCode);
        HttpEntity entity = response.getEntity();
        String string = EntityUtils.toString(entity, "utf-8");
        System.out.println(string);

        //關閉httpclient
        response.close();
        httpClient.close();
    }
}

服務器端

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.nio.charset.Charset;

@Controller
public class HttpClientController {

    @ResponseBody
    @RequestMapping(value = "/httpclient/get",method = RequestMethod.GET)
    public String getTest(@RequestParam String wd){
        wd = new String(wd.getBytes(), Charset.forName("utf-8"));
        System.out.println(wd);
        return "success";
    }
}

5、執(zhí)行post請求

客戶端

import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class HttpClientTest {

    @Test
    public void postTest() throws Exception{
        //創(chuàng)建一個httpclient對象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //創(chuàng)建一個post對象
        HttpPost post = new HttpPost("http://localhost:8082/httpclient/post.html");

        //創(chuàng)建一個Entity。模擬一個表單
        List<NameValuePair> kvList = new ArrayList<>();
        kvList.add(new BasicNameValuePair("username", "張三"));
        kvList.add(new BasicNameValuePair("password", "123"));

        //包裝成一個Entity對象
        StringEntity entity = new UrlEncodedFormEntity(kvList, "utf-8");

        //設置請求的內容
        post.setEntity(entity);

        //執(zhí)行post請求
        CloseableHttpResponse response = httpClient.execute(post);

        //取響應的結果
        String str = EntityUtils.toString(response.getEntity());
        System.out.println(str);

        //關閉httpclient
        response.close();
        httpClient.close();
    }
}

服務器端

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.nio.charset.Charset;

@Controller
public class HttpClientController {

    @ResponseBody
    @RequestMapping(value = "/httpclient/post",method = RequestMethod.POST)
    public String postTest(@RequestParam String username,@RequestParam String password){
        System.out.println(username+"---"+password);
        return "success";
    }
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容