【代码库】webService接口,携带请求头发送Post请求

目录

一、对方webService接口文档:

二、HTTPClient远程调用方法

 三、引入的依赖


一、对方webService接口文档:

二、HTTPClient远程调用方法

package XXXXX;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.springframework.stereotype.Component;
import java.io.*;



/**
 * introduce: 远程调用webservice工具类
 * author: xingMeiLing
 * DATE: 2020/11/11
 **/
@Component
public class HTTPClientUtil {

    /**
    * 方法描述   post请求 无参数
    *
    * author xingMeiLing
    * param reqHeadKey 请求头key
    * param reqHeadValue 请求头value
    * param timeout  超时时间
    * param wsdl 请求地址url
    * param ns 命名空间名
    * param method 方法名
    */
      public synchronized static String accessService(String reqHeadKey,String reqHeadValue,int timeout,String wsdl, String ns, String method) throws IOException {

        //拼接SOAP
        StringBuffer soapRequestData = new StringBuffer("");
        soapRequestData.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:kld=\""+ns+"\">");
        soapRequestData.append("<soapenv:Header/>");
        soapRequestData.append("<soapenv:Body>");
        soapRequestData.append("<kld:" + method + ">");
        soapRequestData.append("</kld:" + method + ">");
        soapRequestData.append("</soapenv:Body>" + "</soapenv:Envelope>");

        String soapReponseDatea = postRequest(reqHeadKey,reqHeadValue,timeout,wsdl,soapRequestData);
        return soapReponseDatea;
    }

    /**
     * Post 参数为json
     * @param wsdl
     * @param jsonString
     * @return code 接口返回的状态码
     * @throws IOException
     */
    public synchronized static String postJson2(String reqHeadKey,String reqHeadValue,int timeout,String wsdl, String ns, String method,String jsonString) throws IOException{

        //拼接SOAP
        StringBuffer soapRequestData = new StringBuffer("");
        soapRequestData.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:kld=\""+ns+"\">");
        soapRequestData.append("<soapenv:Header/>");
        soapRequestData.append("<soapenv:Body>");
        soapRequestData.append("<kld:" + method + ">");
        soapRequestData.append("<datas>[" + jsonString + "]</datas>");
        // 参数拼接
        soapRequestData.append("</kld:" + method + ">");
        soapRequestData.append("</soapenv:Body>" + "</soapenv:Envelope>");

        String soapReponseDate = postRequest(reqHeadKey,reqHeadValue,timeout,wsdl,soapRequestData);
        return soapReponseDate;
    }

    //  发送post请求
    private static String postRequest(String reqHeadKey,String reqHeadValue,int timeout,String wsdl,StringBuffer soapRequestData) throws IOException {
        PostMethod postMethod = new PostMethod(wsdl);
        //将Soap请求数据添加到postMethod中
        String soapReponseDatea = "";
        byte[] b = null;
        InputStream is = null;
        try{
            b = soapRequestData.toString().getBytes("utf-8");
            is = new ByteArrayInputStream(b, 0, b.length);
            RequestEntity re = new InputStreamRequestEntity(is, b.length, "text/xml; charset=UTF-8");
            postMethod.setRequestEntity(re);
            // 添加请求头
            postMethod.addRequestHeader(reqHeadKey,reqHeadValue);
            HttpClient httpClient = new HttpClient();
            // 设置请求超时时间
            httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(timeout);
            // 设置响应超时时间
            httpClient.getHttpConnectionManager().getParams().setSoTimeout(timeout);

            int status = httpClient.executeMethod(postMethod);
            if(status == E7EnumUntil.REQUEST_SUCCESS.getState()){
                soapReponseDatea = getMesage(postMethod.getResponseBodyAsString());
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(is==null){
                is.close();
            }
        }
          return soapReponseDatea;
    }

    // 处理返回
    private static String getMesage(String soapAttachment) {

        if(soapAttachment!=null&&soapAttachment.length()>0){
            int begin = soapAttachment.indexOf("<return xmlns=\"\">");
            begin = soapAttachment.indexOf(">", begin);
            int end = soapAttachment.indexOf("</return>");
            String str = soapAttachment.substring(begin+1, end);
            return str;
        }else{
            return null;
        }
    }
}

 三、引入的依赖

<!--远程调用webservice接口需要依赖的包-->
<!--httpclient请求-->
<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpcore</artifactId>
    <version>4.4.13</version>
</dependency>

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.7</version>
</dependency>

 

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

邢美玲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值