【问题标题】:Sending SOAP Request to WebService error 500向 WebService 发送 SOAP 请求错误 500
【发布时间】:2012-04-20 05:05:39
【问题描述】:

当我将我的 SOAP 请求发送到我的 Web 服务“远程服务器返回一个错误:(500) 内部服务器错误。”时,我得到了这个异常。 WebService 有 2 个参数(int a、int b)。如果我删除这两个参数,那么我不会得到任何异常。

这是我第一次使用 SOAP+WS。我做错了什么? 感谢您抽出宝贵时间。

编辑: 我的大学刚刚在我的代码中发现了错误。我发送的标题无效。

这是缺少的标题部分:

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

另外,我使用的网址不正确。 错误:

http://localhost:62830/Service1.asmx/Add

正确:

http://localhost:62830/Service1.asmx

这是我的代码 将 SOAP 发送到 WebService 的客户端

private void button2_Click(object sender, EventArgs e)
    {

        var url = UrlTextBox.Text;
        var xml_file_path = FileTextBox.Text;

        XmlDocument xml_doc = new XmlDocument();
        xml_doc.Load(xml_file_path);

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.Method = "POST";
        request.ContentType = "text/xml";
        request.Accept = "text/xml";
        request.Timeout = 30 * 1000;

        Stream request_stream = request.GetRequestStream();
        xml_doc.Save(request_stream);
        request_stream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        Stream r_stream = response.GetResponseStream();

        StreamReader response_stream = new StreamReader(r_stream, System.Text.Encoding.Default);
        string sOutput = response_stream.ReadToEnd();

        ResultTextBox.Text = sOutput;

    }

这里是 XML 文件

        <?xml version="1.0" encoding="utf-8"?>
    <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
      <soap12:Body>
        <add xmlns="http://tempuri.org/" >
        <a>5</a>
        <b>10</b>
        </add>
      </soap12:Body>
    </soap12:Envelope>

这里是WebService中的代码

    [WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{

    [WebMethod]
    public int Add(int a, int b)
    {
        return a + b;
    }
}

【问题讨论】:

  • 也许将它们读取为字符串,然后转换为 int 可能会有所帮助
  • 我稍后的计划是将复杂的项目集合发送到 Web 服务。将它们作为字符串读取将需要大量工作才能将它们转换为复杂的类。

标签: c# asp.net web-services soap


【解决方案1】:

手动构建 SOAP 信封似乎不是一个好主意。您应该依靠 WSDL 来“添加 Web 引用”,然后选择 WCF。

无论如何,这是我在 web.config 中用于 WS 的配置的一部分

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
  <conformanceWarnings>
    <remove name="BasicProfile1_1"/>
  </conformanceWarnings>
</webServices>
</system.web>

另外,您可以通过直接连接到 .asmx(在 localhost 模式下)来检查 WS 是否在 GET 模式下工作

不知道这是否重要,但我会稍微不同地写信封。 我会添加标题“Content-Length”。

    <?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <a xmlns="http://tempuri.org/">5</a>
    <b xmlns="http://tempuri.org/">10</b>
  </soap12:Body>
</soap12:Envelope>

【讨论】:

  • 感谢您的提示。该代码是现有客户端/Web 服务应用程序的一部分。我不确定这是否容易将它们转换为您建议的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-25
  • 1970-01-01
  • 1970-01-01
  • 2014-01-03
  • 2017-10-24
  • 2015-10-25
  • 1970-01-01
相关资源
最近更新 更多