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();
    }
}

 从Servlet中获取bean

public void init() throws ServletException {
           claimInfoQueryService = (ClaimInfoQueryService)ServiceFactory.getService(this.getServletContext(),"claimInfoQueryService");
     }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM