如何解决HTTP Web请求500内部服务器错误?

时间:2014-05-19 16:11:02

标签: c# xml http soap httpwebrequest

以下是我的代码:

 XmlFile = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:auth='http://someurl.com/common/header/auth' xmlns:bal='http://someurl.com/transfer/'><soapenv:Header><auth:authHeader>";
        mssg = XmlFile;
        XmlFile = XmlFile + "<auth:username>user</auth:username>";
        XmlFile = XmlFile + "<auth:password>pw</auth:password>";
        XmlFile = XmlFile + "</auth:authHeader></soapenv:Header><soapenv:Body>";
        XmlFile = XmlFile + "<bal:DebitRequest>";
        XmlFile = XmlFile + "<bal:MSISDN>" + MSISDN + "</bal:MSISDN>";
        XmlFile = XmlFile + "<bal:debitAmount>" + Amount + "</bal:debitAmount>";
        XmlFile = XmlFile + "<bal:reason>FIMOBILE</bal:reason>";
        XmlFile = XmlFile + "</bal:DebitRequest>";
        XmlFile = XmlFile + "</soapenv:Body></soapenv:Envelope>";
        HttpWebRequest myWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://url");
        myWebRequest.Method = WebRequestMethods.Http.Post;
        myWebRequest.ContentType = "text/xml;charset=UTF-8";
        byte[] chargeRequestBytes = System.Text.Encoding.ASCII.GetBytes(XmlFile);
        myWebRequest.ContentLength = chargeRequestBytes.Length;
        myWebRequest.Headers.Add("SOAPAction", "https://api.tunetalk.net/infinet/BalanceManagement");
        myWebRequest.Headers.Add("username", "user");
        myWebRequest.Headers.Add("password", "pw");
        StreamWriter writer = new StreamWriter(myWebRequest.GetRequestStream());
        writer.Write(XmlFile);
        writer.Close();

        // Send the 'WebRequest' and wait for response.
        ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateCertificate);

        HttpWebResponse myWebResponse = (HttpWebResponse)myWebRequest.GetResponse();

在最后一行之后抛出错误:

 500:Internal Server Error

我是SOAP新手,如果有人帮我解决这个问题,我会非常感激。谢谢。

更新:

实际上有一个vb.net脚本可以在另一个完美运行的项目中使用相同的代码。所以,我的任务是从vb.net转换为c#但转换后它不起作用。

以下是我转换C#的​​原始VB.Net代码:

  XmlFile = "<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:auth=""http://someurl/header/auth"" xmlns:bal=""http://someurl""><soapenv:Header><auth:authHeader>"
        XmlFile = XmlFile & "<auth:username>user</auth:username>"
        XmlFile = XmlFile & "<auth:password>pass</auth:password>"
        XmlFile = XmlFile & "</auth:authHeader></soapenv:Header><soapenv:Body>"
        XmlFile = XmlFile & "<bal:DebitRequest>"
        XmlFile = XmlFile & "<bal:MSISDN>" & checkMSISDN & "</bal:MSISDN>"
        XmlFile = XmlFile & "<bal:debitAmount>" & debitAmount & "</bal:debitAmount>"
        XmlFile = XmlFile & "<bal:reason>FIMOBILE</bal:reason>"
        XmlFile = XmlFile & "</bal:DebitRequest>"
        XmlFile = XmlFile & "</soapenv:Body></soapenv:Envelope>"

        Dim myWebRequest As HttpWebRequest = HttpWebRequest.Create("https://someurl")
        myWebRequest.Method = WebRequestMethods.Http.Post
        myWebRequest.ContentType = "text/xml"
        myWebRequest.ContentLength = XmlFile.Length
        myWebRequest.Headers.Add("SOAPAction", "https://api.tunetalk.net/infinet/BalanceManagement")
        myWebRequest.Headers.Add("username", "user")
        myWebRequest.Headers.Add("password", "pass")

        Dim writer As New StreamWriter(myWebRequest.GetRequestStream)
        writer.Write(XmlFile)
        writer.Close()

        ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf ValidateCertificate)
        Dim myWebResponse As HttpWebResponse = myWebRequest.GetResponse()

        Dim reader As New StreamReader(myWebResponse.GetResponseStream())

1 个答案:

答案 0 :(得分:1)

根据更新后的信息,转换时必须存在一些错误信息。没有什么比我更突出,所以它可能只是一些小错误。

我的建议是运行WireShark并将过滤器设置为“http”以仅监控HTTP流量。然后,运行这两个程序并比较发送到Web服务的数据。

您也可以使用Fiddler2 尝试。它使用起来更简单,并允许与远程端点进行更多交互式处理。但是,我发现在某些情况下它不能与客户端一起使用,因为客户端可能不尊重系统代理设置并尝试直接进行。

如果您要编写代码转到Web服务,这些是您应该安装用于调试的两个工具。您还应该研究Web服务以及您将要使用的技术。这些技术并不困难,您应该能够相当快地获得足够的知识。