通过URL调用WebService里面的方法,报“因URL意外地以***结束,请求格式无法识别。”解决方案...

因 URL 意外地以“/GetUserInfo”结束,请求格式无法识别。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。

异常详细信息: System.InvalidOperationException: 因 URL 意外地以“/GetUserInfo”结束,请求格式无法识别。

源错误:

执行当前 Web 请求期间生成了未处理的异常。可以使用下面的异常堆栈跟踪信息确定有关异常原因和发生位置的信息。
已解决,要在webservice的 <system.web> 节点下加入

<webServices> 
  <protocols> 
      <add   name= "HttpPost "   /> 
      <add   name= "HttpGet "   /> 
  </protocols> 
</webServices> 

直接调用WebService里面的方法是:

http://localhost:3094/Service1.asmx/GetUserInfo?id=1

其中GetUserInfo为WebService里面的方法名,而id为形参(参数名)1为实参(参数的值)

GetUserInfo的签名为:

[WebMethod]
public bool GetUserInfo(int id);

所以URL中的参数名也必须是id,这个id不区分大小写,但是不能写成别的名字。否则就会出错。如下:

System.InvalidOperationException: 缺少参数: Id。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 IntelliJ IDEA 中调用 WebService 的 XML 请求格式可以参考以下步骤: 1. 新建一个 Java 项目,右键点击项目,选择 New -> Module。 2. 在弹出的对话框中选择 WebServices client,然后点击 Next。 3. 输入 WebService 的 WSDL 地址,选择生成代码的输出目录和包名,然后点击 Next。 4. 在弹出的对话框中选择需要调用WebService 方法,然后点击 Finish。 5. 在生成的代码中找到调用方法的代码,可以看到请求的 XML 格式。例如: ``` Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName(new QName(namespace, operationName)); call.setUseSOAPAction(true); call.setSOAPActionURI(namespace + operationName); call.addParameter(parameterName, XMLType.XSD_STRING, ParameterMode.IN); call.setReturnType(XMLType.XSD_STRING); String result = (String) call.invoke(new Object[] { parameterValue }); ``` 6. 修改请求的 XML 格式,根据需要添加或修改请求参数,例如: ``` String xmlRequest = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://example.com/HelloWorld\"><soapenv:Header/><soapenv:Body><ser:sayHello><arg0>World</arg0></ser:sayHello></soapenv:Body></soapenv:Envelope>"; ``` 7. 发送请求并获取响应,例如: ``` URL url = new URL(endpoint); HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection(); httpConnection.setRequestMethod("POST"); httpConnection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); httpConnection.setRequestProperty("SOAPAction", namespace + operationName); httpConnection.setDoOutput(true); OutputStream outputStream = httpConnection.getOutputStream(); outputStream.write(xmlRequest.getBytes()); outputStream.flush(); InputStream inputStream = httpConnection.getInputStream(); byte[] buffer = new byte[1024]; int length; StringBuilder responseBuilder = new StringBuilder(); while ((length = inputStream.read(buffer)) != -1) { responseBuilder.append(new String(buffer, 0, length)); } String xmlResponse = responseBuilder.toString(); ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值