【问题标题】:Getting 400 bad request with WCF service called through WebRequest using C#使用 C# 通过 WebRequest 调用 WCF 服务获取 400 个错误请求
【发布时间】: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


【解决方案1】:

我看到了你的另一个帖子,我认为你和我目前正在做同样的事情。动态调用 Web 服务。加载 WSDL,使用动态代理和反射以及所有有趣的东西。

对于您看到的这个错误,我有一个建议。 我使用的其中一项 Web 服务遇到了类似的问题。即使标头中的 Action 被认为是可选的,当我像您一样传入地址 (uri) 时,它确实使我的调用失败。当我传入操作的名称时,它起作用了。所以试着改变这个:

req.Headers.Add("SOAPAction", "http://tempuri.org/GetSimpleType"); 

改为 WSDL 中的方法名称。这是 System.ServiceModel.Description.OperationDescription 的名称。可能是“GetSimpleType”。

req.Headers.Add("SOAPAction", "GetSimpleType"); 

【讨论】:

  • 嗨,Karin 很高兴看到有人在做类似的事情,否则我找不到 WCF/Soap 请求的任何资源。按照您的建议更改代码也没有使它起作用:( 可能我在这里仍然遗漏了一些东西。起初我认为这可能是因为为 WCF 生成 wsdl 的方式(多个 xsd 参考)但后来我生成了单个 wsdl,它又失败了。你能分享你的代码吗?
猜你喜欢
  • 1970-01-01
  • 2013-06-22
  • 2012-04-27
  • 2015-11-24
  • 1970-01-01
  • 1970-01-01
  • 2021-10-20
  • 2011-09-29
  • 2014-01-11
相关资源
最近更新 更多