SOAPUI HTTP后台模拟代码

xiaoxiao2021-02-28  91

SoapUI HTTP后台调用模拟程序                        很多情况下我们再调用Werbservice接口的时候都是在本地调用,本地调用的时候直接用windows下面的soapui即可测试。而当我们需要与厂家对接接口时候,一般都是放在linux服务器上去测试,而此时就需要用代码来测试。或者你也可以在linux上安装linux版本的soapui,这里我们主要介绍代码方式实现SoapUI后台Webservice后台调用。 package com.zy.test; import java.io.ByteArrayInputStream; import java.io.InputStream; 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; public class Deme { public static String SoapRequest(String xml, String url) { try { PostMethod postMethod = new PostMethod(url); byte[] b = xml.getBytes("utf-8"); InputStream is = new ByteArrayInputStream(b, 0, b.length); System.out.println("is:" + is); RequestEntity re = new InputStreamRequestEntity(is, b.length, "application/soap+xml; charset=utf-8"); System.out.println("re:" + re); postMethod.setRequestHeader("SOAPAction", "getAllManagedElements");//这个参数对应操作类型,一般方法名。不传报接口未   //注册错误,很重要。 postMethod.setRequestEntity(re); HttpClient httpClient = new HttpClient(); httpClient.executeMethod(postMethod); xml = postMethod.getResponseBodyAsString(); xml = xml.replaceAll(""", "'"); } catch (Exception e) { e.printStackTrace(); xml = "error"; } return xml; } public static void main(String[] args) { String report ="<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"" + "xmlns:v1=\"http://www.tmforum.org/mtop/fmw/xsd/hdr/v1\" " + "xmlns:v11=\"http://www.tmforum.org/mtop/mri/xsd/mer/v1\" " + "xmlns:v12=\"http://www.tmforum.org/mtop/fmw/xsd/nam/v1\">" + "<soapenv:Header>" + " <v1:header> " + "<v1:activityName>getAllManagedElements</v1:activityName>" + "<v1:msgName>getAllManagedElementsRequest</v1:msgName>" + "<v1:msgType>REQUEST</v1:msgType>" + "<v1:senderURI>/MTOSI/InventoryOS</v1:senderURI>" + "<v1:destinationURI>/MTOSI/EmsOS</v1:destinationURI>" + "<v1:security>admin_VARIABLE_admin</v1:security>" + "<v1:communicationPattern>SimpleResponse</v1:communicationPattern>" + "<v1:communicationStyle>RPC</v1:communicationStyle>" + "</v1:header>" + "</soapenv:Header>" + "<soapenv:Body>" + "<v11:getAllManagedElementsRequest>" + "<!--Optional:-->" + "<v11:mdOrMlsnRef>" + "<!--1 or more repetitions:-->" + "<v12:rdn>" + "<v12:type>MD</v12:type>" + "<v12:value>GZKD</v12:value>" + "</v12:rdn>" + "</v11:mdOrMlsnRef>" + "</v11:getAllManagedElementsRequest>" + "</soapenv:Body>" + "</soapenv:Envelope>" ; String url = "webservice地址"; System.out.println("返回的xml报文是:" + SoapRequest(report, url)); } } 代码所使用到的jar包分别有:httpcore-4.3.1.jar,commons-logging-api-1.1.jar,commons-logging-1.1.1.jar,commons-httpclient-3.1.jar,commons-codec-1.6.jar。可在www.java2s.com自行下载。 解压出来放入上面的Demo类打包的jar包配置Main-Class主属性上传至对应服务器即可运行。
转载请注明原文地址: https://www.6miu.com/read-28218.html

最新回复(0)