码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-12-15 18:56:21      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:common   run   ack   except   trace   import   ring   release   inpu   

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请求

标签:common   run   ack   except   trace   import   ring   release   inpu   

原文地址:http://www.cnblogs.com/jxtx92/p/8044360.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!