使用SoapUI打开wsdl文件,点击到request,双击request,右面的内容就是需要通过http发送的内容,其中?为放参数的位置,参数要用<![CDATA[参数]]>包裹起来。具体代码中http的编码根据实际情况修改
其他需要的内容:
点击绿色三角发送后,可以点击Raw查看发送http所需的内容(请求地址、header等)
具体代码:
//wsdl地址为http://127.0.0.1/testwebservice/services/userservice?wsdl,下面的终端地址可以不加wsdl(有时加了会报错)
String endpoint="http://127.0.0.1/testwebservice/services/userservice";
String userId="1234";
String data="<soapenv:xmlns:soapenv="http://schemas.xm.....>"
+"<soapenv:Header/>"
+"<soapenv:Body>"
+" <open:getuser>"
<open:userid><![CDATA["+userId+"]]></open:xmlData>"
+"</open:getuser>"
+"</soapenv:Body>"
+"</soapenv:Envelope>";
//http发送数据
String url=endpoint;
String headers=null;
String arg=data;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(CONNECT_TIMEOUT).setSocketTimeout(SOCKET_TIMEOUT).build();
httpPost.setConfig(requestConfig);
StringEntity entity = new StringEntity(arg, "UTF-8");
httpPost.setEntity(entity);
CloseableHttpResponse httpResponse = null;
//back为服务端返回的原始soap格式的xml数据,并且实际有用数据的“<”被转译成了“<”,需要自行处理
String back=getResult(httpResponse, httpClient, httpPost);
httpResponse.close();
httpClient.close();
需要引入的类
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
getResult方法:
public static String getResult(CloseableHttpResponse httpResponse,
CloseableHttpClient httpClient, HttpRequestBase httpMethod) throws Exception {
httpResponse = httpClient.execute(httpMethod);
// 获取返回结果
String content = "";
Integer responseStatus = null;
if (httpResponse != null && httpResponse.getStatusLine() != null) {
if (httpResponse.getEntity() != null) {
//返回结果
content = EntityUtils.toString(httpResponse.getEntity(), "utf-8");
}
//返回状态
responseStatus = httpResponse.getStatusLine().getStatusCode();
}
return content;
}