简体   繁体   English

.NET SOAP请求返回“ 400 Bad Request”

[英].NET SOAP request returns “400 Bad Request”

I am trying to create a simple XML SOAP request to a web service. 我正在尝试创建对Web服务的简单XML SOAP请求。

The service itself is proven to be OK, I accessed it by using WSDL-generated class without any problems. 该服务本身被证明是可以的,我使用WSDL生成的类访问了它,没有任何问题。 But if I try to access it using regular WebRequest - it always returns 400 Bad Request. 但是,如果我尝试使用常规的WebRequest访问它-它总是返回400 Bad Request。

Here's my code: 这是我的代码:

class Program
{

    static void Main(string[] args)
    {

        HttpWebRequest request = CreateWebRequest("http://localhost:8000/ExchangeService", "WhoAmI");
        XmlDocument SoapEnvelopeXml = new XmlDocument();
        SoapEnvelopeXml.LoadXml(
        @"<?xml version=""1.0""?>
        <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope"" 
        xmlns:tem=""http://tempuri.org"">
        <soapenv:Header/>
        <soapenv:Body>
        <tem:WhoAmI/></soapenv:Body></soapenv:Envelope>");

        using (Stream stream = request.GetRequestStream())
        {
            SoapEnvelopeXml.Save(stream);
        }

        using (WebResponse response = request.GetResponse())
        {
            using (StreamReader rd = new StreamReader(response.GetResponseStream()))
            {
                string soapResult = rd.ReadToEnd();
                Console.WriteLine(soapResult);
            }
        }

        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }

    public static HttpWebRequest CreateWebRequest(string url, string soapAction)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
        webRequest.ContentType = "text/xml;charset=UTF-8;action=\"" + soapAction + "\"";
        webRequest.Method = "POST";
        return webRequest;
    }
}

The XML is generated by SoapUI and works there perfectly. XML是由SoapUI生成的,并且在其中可以完美地工作。 Here is the SoapUI's HTTP header: 这是SoapUI的HTTP标头:

POST http://localhost:8000/ExchangeService HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: text/xml;charset=UTF-8
SOAPAction: "http://tempuri.org/IExchangeFunctions/WhoAmI"
Content-Length: 211
Host: localhost:8000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

What am I doing wrong? 我究竟做错了什么?

UPD: changed the service from my to some third-party: http://wsf.cdyne.com/WeatherWS/Weather.asmx Through WSDL - it works. UPD:通过WSDL将服务从我的服务更改为第三方服务: http ://wsf.cdyne.com/WeatherWS/Weather.asmx-可以使用。 SoapUI returns data. SoapUI返回数据。 From my code - Internal Error 500. 从我的代码-内部错误500。

Try remove carriage return from soap message .Replace("\\r\\n", ""); 尝试从肥皂消息.Replace("\\r\\n", "");删除回车.Replace("\\r\\n", ""); If not work verify the http request sended with fiddler, to me it was very helpful. 如果不起作用,请验证随提琴手发送的http请求,对我来说非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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