简体   繁体   中英

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. Thank you.

UPDATE:

Actually there is a vb.net script for the same code is available in another project which is working perfectly. So, my task was to convert from vb.net to c# but after converting it is not working.

Here is the original VB.Net code from where I have converted my 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. Then, run both programs and compare the data sent to the web service.

You could also try using Fiddler2 instead. 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. You should also study up on web services and the technologies that you're going to use. These technologies are not difficult and you should be able to get enough knowledge on them fairly quick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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