【发布时间】:2011-03-29 16:12:13
【问题描述】:
在对代码进行一些更改并通过大量链接之后,我能够编写以下代码来使用 SOAP 消息调用 WCF 服务方法。现在我得到 400 Bad Request 错误,无论我改变什么(尝试 HttpWebRequest)我仍然得到这个错误。不知道我错过了什么:
private string WebServiceCall()
{
WebRequest req = WebRequest.Create("http://klo239fu.mass.win.tf.com/WCFTestService/Service.svc");
req.ContentType = "application/soap+xml; charset=utf-8";
req.Method = "POST";
req.Headers.Add("SOAPAction", "http://tempuri.org/GetSimpleType");
using(Stream reqStream = req.GetRequestStream())
{
var bytes = Encoding.UTF8.GetBytes(txtFormattedSoap.Text);
reqStream.Write(bytes, 0, bytes.Length);
reqStream.Close();
}
var wr = (WebRespose)req.GetResponse();
var srd = new StreamReader(wr.GetResponseStream());
txtResponse.Text = srd.ReadToEnd();
}
SOAP 请求(来自 SOAP 客户端)
POST /WCFTestService/Service.svc HTTP/1.1
Host: klo239fu.mass.win.tf.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "wsf-test-service/IService/GetSimpleType"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<GetSimpleType xmlns="wsf-test-service">
<myvalue>int</myvalue>
</GetSimpleType>
</soap:Body>
</soap:Envelope>
这是我在我的代码中传递给上述肥皂信封的 SOAP 请求:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><GetSimpleType xmlns="wcf-test-service"><myvalue>1234</myvalue></GetSimpleType></soap:Body></soap:Envelope>
之前我有内容类型 test/xml,但抛出了代码“(415)无法处理消息,因为内容类型 'text/xml' 不是预期的类型。”。所以我更改了 content type="application/soap+xml; charset=utf-8" 但它因 (400) Bad request 而失败。
【问题讨论】:
-
撞!撞!同样的问题,这个问题正在等待答案。
标签: wcf soap httpwebrequest webrequest