[英].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.