【commons-httpclient】Java中HttpClient工具訪問Web請求


  注意jar包是:

 

 

 

HttpClient工具使用

  HttpClient 是 Apache Jakarta Common 下的子項目,可以用來提供高效的、最新的、功能豐富的支持 HTTP 協議的客戶端編程工具包,並且它支持 HTTP 協議最新的版本和建議。

  

  為什么要使用HttpClient工具:

原生態的Socket基於傳輸層,現在我們要訪問的WebService是基於HTTP的屬於應用層,所以我們的Socket通信要借助HttpClient發HTTP請求,這樣格式才能匹配

HttpClient使用步驟如下:

  1. 創建 HttpClient 的實例
  2. 創建某種連接方法的實例,在這里是 GetMethod。在 GetMethod 的構造函數中傳入待連接的地址
  3. 配置要傳輸的參數,和消息頭信息
  4. 調用第一步中創建好的實例的 execute 方法來執行第二步中創建好的 method 實例
  5. 通過response讀取字符串
  6. 釋放連接。無論執行方法是否成功,都必須釋放連接

jar包:

1。第一種使用方式:

Get方式:

public static void getMethod() throws Exception {
        // 創建get對象,類似get請求
        GetMethod getMethod = new GetMethod(        "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=18373551982&userID=");
        // 發送get請求
        int code = http.executeMethod(getMethod);
        System.out.println("返回的消息碼為:" + code);
        System.out.println("返回的消息為:" + getMethod.getResponseBodyAsString());
        getMethod.releaseConnection();
    }

 

 

POST方式:

public static void postMethod() throws Exception {
        // 創建post請求,類似Post請求
        PostMethod postMethod = new PostMethod( "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
        // 設置請求的正文內容
        postMethod.setRequestBody("mobileCode=18373551982&userID=");
        // 設置傳送信息的格式
        postMethod.setRequestHeader("Content-Type",
                "application/x-www-form-urlencoded");
        // 發送post請求
        int code = http.executeMethod(postMethod);
        System.out.println("返回消息碼為:" + code);
        System.out.println("返回的消息為:" + postMethod.getResponseBodyAsString());
        postMethod.releaseConnection();
    }

 

 

2.第二種使用方式

        /**HttpClient訪問網絡的實現步驟:
         *  1. 准備一個請求客戶端:瀏覽器
         *  2. 准備請求方式: GET 、POST
         *  3. 設置要傳遞的參數
         *  4.執行請求
         *  5. 獲取結果
         */

 

get方式:(不用設置參數)

package ws_a;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.junit.Test;

public class HttpClientTest {

    @Test
    public void testGet() throws HttpException, Exception{
        HttpClient client = new HttpClient();
        GetMethod getMethod = new GetMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode="+"13110410513"+
                "&userID="+"");
        //4.執行請求 ,結果碼
        int code=client.executeMethod(getMethod);
        //5. 獲取結果
        String result=getMethod.getResponseBodyAsString();
        System.out.println("get請求的結果:"+result);
    }
}

 

 

get請求的結果:<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">13110410513:陝西 西安 陝西聯通GSM卡</string>

 

Post方法:

    @Test
    public void post() throws Exception{
        HttpClient client=new HttpClient();
        PostMethod postMethod=new PostMethod("http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo");
        //3.設置請求參數
        postMethod.setParameter("mobileCode", "13834786998");
        postMethod.setParameter("userID", "");
        //4.執行請求 ,結果碼
        int code=client.executeMethod(postMethod);
        //5. 獲取結果
        String result=postMethod.getResponseBodyAsString();
        System.out.println("Post請求的結果:"+result);
    }

 

Post請求的結果:<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://WebXml.com.cn/">13834786998:山西 長治 山西移動全球通卡</string>

 

如果返回的中文亂碼,我們可以設置編碼:

        // 防止中文亂碼
        postMethod.getParams().setContentCharset("utf-8");

 

 

 

 

maven地址:

        <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
        </dependency>

 


免責聲明!

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



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