SoapUI JAVA HTTP方式调用webservice接口

使用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数据,并且实际有用数据的“<”被转译成了“&lt;”,需要自行处理
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;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值