远程httpclient 请求

工具类 HttpClientUtil

 

public class HttpClientUtil {
  
    public static String sendRequest(URL url) throws IOException {
        String result = "";
        try {
            PostMethod method = new PostMethod(url.toString());
            method.addRequestHeader("Cache-Control",
                    RequestConstants.REQUEST_CACHE_CONTROL);
            method.addRequestHeader("SOAPAction",
                    RequestConstants.REQUEST_SOAP_ACTION);
            method.addRequestHeader("Cookie", RequestConstants.REQUEST_COOKIE);
            method.addRequestHeader("Host", url.getHost());
            method.addRequestHeader("Accept", RequestConstants.REQUEST_ACCEPT);
            method.setRequestEntity(null);
            HttpClient httpClient = new HttpClient();
            int status = httpClient.executeMethod(method);

            if (method.getResponseContentLength() < 0) {
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                byte[] data = new byte[8192];
                int rsize = 0;
                int totalrsize = 0;
                InputStream in = method.getResponseBodyAsStream();
                do {
                    rsize = in.read(data);
                    if (rsize > 0) {
                        totalrsize += rsize;
                        bout.write(data, 0, rsize);
                    }
                } while (rsize > 0);
                result = new String(bout.toByteArray());
            } else {
                byte data[] = method.getResponseBody();
                result = new String(data, "utf-8");
            }
            if (status != 200) {
                log.info("请求错误:[url:" + url.toString() + ",responseCode:"
                        + status + "]");
                return null;
            }
        } catch (Exception e) {
            log.info("http请求异常!");
        }
        return result;
    }

 

public interface RequestConstants {
 
    public static final String REQUEST_CACHE_CONTROL = "no-cache";
  
    public static final String REQUEST_SOAP_ACTION = "\"\"";
 
    public static final String REQUEST_COOKIE = "JSESSIONID=A09JHGHKHU68624309UTY84932;";
 
    public static final String REQUEST_ACCEPT = "application/soap+xml, application/dime, multipart/related, text/*";
   
}

 

action 类

    public String listTicketsByCondition() throws IOException {
        String url = "http://192.168.3.205:8080/GenLotWBOS/queryTickInf.action";
        String messageid = this.getRequest().getParameter("messageid");
        String cpserial = this.getRequest().getParameter("cpserial");
        String tacketid = this.getRequest().getParameter("tacketid");
        PostHelper p = new PostHelper(url);
        if(StringUtils.hasLength(messageid)){
            p.addParameter("messageid", messageid);
        }
        if(StringUtils.hasLength(cpserial)){
            p.addParameter("cpserial", cpserial);
        }
        if(StringUtils.hasLength(tacketid)){
            p.addParameter("orderno", tacketid);
        }
        String jsonMsg = p.post(url);
    
   
        this.getResponse().setCharacterEncoding("UTF-8");
        this.getResponse().getWriter().write(jsonMsg);
        this.getResponse().getWriter().flush();

        return null;
    }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值