SOAP帖子导致“400错误请求”

时间:2018-05-27 05:17:49

标签: javascript c# wcf soap

我正在尝试通过简单页面上的JS调用提交SOAP post请求。提交时,如果我通过浏览器调试,我会得到:

  

POST http://localhost:63748/DistGamingWebService.svc 400(错误请求)

我有一个Web服务(如上所述),它连接到一个本地运行的非常基本的“数据库”类型服务器。两者之间的连接工作正常 - 当我运行WCF测试客户端时,我可以调用方法并按预期获得返回值。

我已根据测试WCF客户端的XML请求设置了SOAP消息,但仍然收到400错误请求。 req.status的值始终为1.

我在下面列出了我的JS调用和POST消息格式。

function GetNumBossesTest() {
  req = null;
  if (window.XMLHttpRequest != undefined)
    req = new XMLHttpRequest();
  else {
    req = new ActiveXObject("Micosoft.XMLHTTP");
  }

  req.open("POST", "http://localhost:63748/DistGamingWebService.svc", true);

  req.setRequestHeader("Content-Type", "text/xml");
  req.setRequestHeader("SOAPAction", "http://tempuri.org/IDistGamingWebService/GetNumBosses");

  var sMsg = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header><Action soap:>http://tempuri.org/IDistGamingWebService/GetNumBosses</Action></soap:Header><soap:Body><GetNumBosses xmlns="http://tempuri.org/" /></soap:Body></s:Envelope>';

  req.send(sMsg);
  if (req.status == 200)
    alert(req.responseText);

} 

在上面的例子中,我通过将消息完全放在一行来简化消息。当我通过Fiddler和Chrome调试检查请求时,消息的格式与JS插入它的方式完全相同。

就我的网站svc上的界面而言,我有以下内容:

[ServiceContract]
public interface IDistGamingWebService
{    
    [OperationContract]
    int GetNumBosses();
}

此接口的实现只是在其他类似数据库的服务器上进行远程调用。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

我认为SOAP消息在命名空间方面存在一些问题。此外,我会完全删除邮件中的操作,因为它已经存在于标题中。

试试这个:

var sMsg = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Header/><soap:Body><GetNumBosses xmlns="http://tempuri.org/" /></soap:Body></soap:Envelope>';
相关问题