webservice客户端asmx

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

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

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

服务端的URL:

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());
		}
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值