简体   繁体   English

如何解决HTTP Web request 500 Internal Server错误?

[英]How to solve HTTP Web request 500 Internal Server error?

Below is my code: 下面是我的代码:

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

After the last line it is throwing error: 在最后一行之后,它抛出错误:

 500:Internal Server Error

I am quite new in SOAP I would appreciate most if somebody help me to figure this out. 我是SOAP的新手,如果有人帮助我弄清楚这一点,我将不胜感激。 Thank you. 谢谢。

UPDATE: 更新:

Actually there is a vb.net script for the same code is available in another project which is working perfectly. 实际上,在另一个运行良好的项目中,有一个vb.net脚本用于相同的代码。 So, my task was to convert from vb.net to c# but after converting it is not working. 因此,我的任务是将vb.net转换为c#,但转换后无法正常工作。

Here is the original VB.Net code from where I have converted my 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())

Based on the updated information, there has to be some fine error in your conversion. 根据更新的信息,您的转换中肯​​定有一些错误。 Nothing stands out to me so it could just be some minor typo. 对我来说,什么都没有突出,所以可能只是一些小的错字。

My suggestion is to run WireShark and set your filter to "http" to monitor only HTTP traffic. 我的建议是运行WireShark,并将过滤器设置为“ http”以仅监视HTTP通信。 Then, run both programs and compare the data sent to the web service. 然后,运行两个程序并比较发送到Web服务的数据。

You could also try using Fiddler2 instead. 您也可以尝试使用Fiddler2代替。 It's simpler to use and allows more interactive processing with the remote endpoint. 它使用起来更简单,并允许与远程端点进行更多的交互处理。 However, I have found that in certain cases it won't work with the client because the client may not respect the system proxy settings and attempt to go direct. 但是,我发现在某些情况下它不适用于客户端,因为客户端可能不尊重系统代理设置并尝试直接进行操作。

If you're going to be writing code to go to web services, these are two tools that you should have installed for debugging. 如果要编写代码以访问Web服务,则应安装这两个工具以进行调试。 You should also study up on web services and the technologies that you're going to use. 您还应该学习Web服务和将要使用的技术。 These technologies are not difficult and you should be able to get enough knowledge on them fairly quick. 这些技术并不难,您应该能够很快地获得足够的知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM