简体   繁体   中英

SOAP post resulting in “400 bad request”

I am trying to submit a SOAP post request via a JS call on a simple page. When submitting, if I debug via the browser, I get a:

POST http://localhost:63748/DistGamingWebService.svc 400 (Bad Request)

I have a web service (as above) which connects to a very basic "database" type server both running locally. The connections between the two work fine - when I run the WCF test client, I can invoke the methods and get returned values as expected.

I have set up the SOAP message based on the test WCF client's XML request but I still get the 400 bad request. The value of req.status is always 1.

I've included below, my JS call, and the POST message format.

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

} 

In the above example, I've simplified the message by making it entirely on one line. When I inspect the request through both Fiddler and Chrome debugging, the message is formatted exactly how the JS inserts it.

In terms of the interface on my web svc, I have the following:

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

The implementation of this interface is simply remote calls on the other database-like server.

Thank you in advance.

I think the SOAP message has some problems with the namespace. Also, I would remove the action from the message altogether since it is already present in the header.

Try this:

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>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM