目录
一、对方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>