【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>
原文地址:https://www.cnblogs.com/qlqwjy/p/7554535.html