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

本文关键字:请求 内部 服务器 错误 Web HTTP 何解决 解决 | 更新日期: 2023-09-27 18:04:17

我的代码如下:

 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#,但转换后它不工作。

这是原来的VB。Net代码从我已经转换我的c#:

  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())

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

根据更新的信息,在您的转换中必须有一些细微的错误。没有什么能让我注意到的,所以可能只是一些小的打字错误。

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

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

如果你要写代码去web服务,这是你应该安装调试的两个工具。您还应该研究web服务和将要使用的技术。这些技术并不难,你应该能够很快获得足够的知识。