webservice客户端asmx[通俗易懂]

webservice客户端asmx[通俗易懂]记录一次webservice接口访问服务端一般会给个以http://xxx/services.asmx。以前都是wsdl做服务端,采用idea自带的工具生成客户端或者用wsdl2java工具生成。从网上找了好多方法,最后终于成功了。服务端的URL:asmx的请求与响应代码:<!–请求–>POST/webService/services/webServiceImplService.asmxHTTP/1.1Host:172.16.1.20Content-Ty

大家好,又见面了,我是你们的朋友全栈君。

记录一次webservice接口访问服务端一般会给个以http://xxx/services.asmx。

以前都是wsdl做服务端,采用idea自带的工具生成客户端或者用wsdl2java工具生成。

从网上找了好多方法,最后终于成功了。

服务端的URL:

webservice客户端asmx[通俗易懂]

asmx的请求与响应代码:

<!--请求-->
POST /webService/services/webServiceImplService.asmx HTTP/1.1
Host: 172.16.1.20
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://webService/services/webServiceImplService/SendInfo"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SendInfo xmlns="http://webService/services/webServiceImplService">
      <Data>string</Data>
    </SendInfo>
  </soap:Body>
</soap:Envelope>
<!--响应-->
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <SendInfoResponse xmlns="http://webService/services/webServiceImplService">
      <SendInfoResult>string</SendInfoResult>
    </SendInfoResponse>
  </soap:Body>
</soap:Envelope>

方法一

asmx也可以用wsdl2java工具生成。就在http://xxx/services.asmx后加?wsdl即可。生成方式可百度,有很多。如果这么简单我就不会写这篇文章了/哭

我这个服务端地址里面包含了很多方法。其中有参数是重复的,导致用wsdl2java工具生成时一直报某某字段重复。我从网上找了个asmx文件是可以用wsdl2java生成的。所以这个方法是没法用的。

方法二     

直接使用org.apache.axis.client.ServiceCall。代码如下:

public static void main(String[] args) throws Exception {
    String url = "http://ip:port/webService/services/webServiceImplService.asmx";
    //这里有个坑,一定要注意最后是否有反斜线!!!
    String namespace = "http://webService/services/webServiceImplService";
    //action路径(方法名)
    String actionUri = "SendInfo";
    //方法名
    String op = "SendInfo";

    Service service = new Service();
	Call call = (Call) service.createCall();
	call.setTargetEndpointAddress(new URL(url));
	call.setUseSOAPAction(true);
    // 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值
	call.setSOAPActionURI(namespace +"/"+actionUri);
	call.setReturnType(XMLType.XSD_STRING);
	call.setOperationName(new QName(namespace, op)); // 设置要调用哪个方法
	call.addParameter(new QName(namespace,"Data"), // 设置要传递的参数(形参)
	XMLType.XSD_STRING, ParameterMode.IN);

    String json = "传递的数据";
    Object[] params = new Object[]{json};
    String response = "";
    try {
			response = (String) call.invoke(params);// 调用方法并传递参数
		}catch (Exception e){
			e.printStackTrace();
			//输出SOAP发送的请求报文
			System.out.println("--SOAP Request: " +     call.getMessageContext().getRequestMessage().getSOAPPartAsString());
		}
}

 

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/130385.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号