使用Web服务

时间:2013-08-09 12:33:11

标签: c# web-services consuming

    try
    {
        string xml = XMLExporter.OrderToXML(order);

        WebRequest request = WebRequest.Create("http://localhost:60159/intranet/webservice/OrderService.asmx/ReceiveOrder");
        request.Method = "POST";

        byte[] byteArray = Encoding.UTF8.GetBytes(xml);

        request.ContentType = "application/xml";
        request.ContentLength = byteArray.Length;

        Stream dataStream = request.GetRequestStream();
        dataStream.Write(byteArray, 0, byteArray.Length);
        dataStream.Close();

        WebResponse webResponse = request.GetResponse(); // ERROR HERE
    }
    catch // 500 error
    {
        return false;
    }
    return true;

Web.Config中:

  <system.serviceModel>
<bindings>
  <basicHttpBinding>
    <binding name="ReceiveOrder" />
  </basicHttpBinding>
</bindings>
<client>      
  <endpoint address="http://localhost:60159/intranet/webservice/OrderService.asmx" binding="basicHttpBinding" bindingConfiguration="ReceiveOrder" contract="OrderService.ReceiveOrder" name="ReceiveOrder" />
</client>

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

“远程服务器返回错误:(500)内部服务器错误。”

当我直接从代码调用方法时,它工作正常!

任何人都可以了解这个错误吗?

1 个答案:

答案 0 :(得分:1)

如果你得到àhttp错误500,这意味着它是抛出的服务器部分,所以你应该看看OrderService.asmx中的方法ReceiveOrder。

要恢复它,不是呼叫者的问题,而是接收者的问题。