Java编程之通过HttpClient发送HTTP请求

Posted jxtx92

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java编程之通过HttpClient发送HTTP请求相关的知识,希望对你有一定的参考价值。

HttpClient时著名的开源软件组织Apache基金会下的一个子项目,它对HTTP协议通信的过程进行了封装,提供高效且功能丰富的客户端编程工具包。

package cn.com.cis.claim.car.interf.common.service.facade;

public interface HttpClientService {
    /**
     * HttpServlet+XML交互发送报文
     * chengjie 2017-12-14
     * @param strURL
     * @param requestXML
     * @return String 返回报文
     */
    public String postMethod(String strURL, String requestXML);
}
package cn.com.cis.claim.car.interf.common.service.spring;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;

import cn.com.cis.claim.car.interf.common.service.facade.HttpClientService;

public class HttpClientServiceSpringImpl implements HttpClientService {
    
    public String postMethod(String strURL, String requestXML) {
        StringBuffer buffer = new StringBuffer();
        String strResponse = "";
        PostMethod postMethod = null;
        HttpClient httpClient = new HttpClient();

        HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams();

        int intCICONNECTTIME = Integer.parseInt("120000"); // 连接超时时间(单位毫秒)
        int intSoTimeout = Integer.parseInt("180000"); // 连接读数据超时时间(单位毫秒)
        managerParams.setConnectionTimeout(intCICONNECTTIME); // 设置连接超时时间(单位毫秒)
        managerParams.setSoTimeout(intSoTimeout); // 设置读数据超时时间(单位毫秒)=连接超时时间+1分钟

        System.out.println("********************************");
        System.out.println("访问地址" + strURL);
        System.out.println("********************************");
        BufferedReader reader = null;
        try {
            System.out.println("---------->进入try");
            System.out.println("strURL-------->" + strURL);
            postMethod = new PostMethod(strURL);
            System.out.println("---------->postMethod");
            postMethod.setRequestEntity(new StringRequestEntity(requestXML, "text/xml", "GBK"));
            int statusCode = httpClient.executeMethod(postMethod);
            System.out.println("statusCode-------->" + statusCode);
            if (statusCode != HttpStatus.SC_OK) {
                throw new IllegalStateException("Method failed: " + postMethod.getStatusLine());
            }

            reader = new BufferedReader(new InputStreamReader(postMethod.getResponseBodyAsStream()));
            System.out.println("reader--------->" + reader);
            while ((strResponse = reader.readLine()) != null) {
                buffer.append(strResponse);
            }

        } catch (Exception ex) {
            throw new RuntimeException("发送数据出错,请联系管理员!");
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            postMethod.releaseConnection();

        }
        return buffer.toString();
    }
}

 

以上是关于Java编程之通过HttpClient发送HTTP请求的主要内容,如果未能解决你的问题,请参考以下文章

JAVA通过HttpClient发送HTTP请求的方法

JAVA通过HttpClient发送HTTP请求

Java httpClient 发送http请求

HTTP请求异步编程解决堵塞之痛-Apache HttpClient5.0

Java爬虫技术之HttpClient学习笔记

Android网络编程使用HttpClient访问web站点