[英].NET SOAP request returns “400 Bad Request”
我正在嘗試創建對Web服務的簡單XML SOAP請求。
該服務本身被證明是可以的,我使用WSDL生成的類訪問了它,沒有任何問題。 但是,如果我嘗試使用常規的WebRequest訪問它-它總是返回400 Bad Request。
這是我的代碼:
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;
}
}
XML是由SoapUI生成的,並且在其中可以完美地工作。 這是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)
我究竟做錯了什么?
UPD:通過WSDL將服務從我的服務更改為第三方服務: http ://wsf.cdyne.com/WeatherWS/Weather.asmx-可以使用。 SoapUI返回數據。 從我的代碼-內部錯誤500。
嘗試從肥皂消息.Replace("\\r\\n", "");
刪除回車.Replace("\\r\\n", "");
如果不起作用,請驗證隨提琴手發送的http請求,對我來說非常有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.