Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Soap call gives 500 (internal server error) in c#

Tags:

c#

soap

I have one affiliate account and I need to make a soap call to get data. I got ready code from one site and I tried to apply it, but I'm getting 500(internal server error). My code is given below.

public void getdata()
{
    var _url = "http://secure.directtrack.com/api/soap_affiliate.php";
    var _action = "http://secure.directtrack.com/api/soap_affiliate.php/dailyStatsInfo";

    XmlDocument soapEnvelopeXml = CreateSoapEnvelope();
    HttpWebRequest webRequest = CreateWebRequest(_url, _action);
    InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

    // begin async call to web request.
    IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

    // suspend this thread until call is complete. You might want to
    // do something usefull here like update your UI.
    asyncResult.AsyncWaitHandle.WaitOne();

    // get the response from the completed web request.
    string soapResult;
    using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
    using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
    {
        soapResult = rd.ReadToEnd();
    }
    Console.Write(soapResult);

}

private static HttpWebRequest CreateWebRequest(string url, string action)
{
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.Headers.Add("SOAPAction", action);
    webRequest.ContentType = "text/xml;charset=\"utf-8\"";
    webRequest.Accept = "text/xml";
    webRequest.Method = "POST";
    return webRequest;
}

private static XmlDocument CreateSoapEnvelope()
{
    XmlDocument soapEnvelop = new XmlDocument();

    soapEnvelop.LoadXml(@"<SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:xsi=""http://www.w3.org/1999/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/1999/XMLSchema""><SOAP-ENV:Body  xmlns=""http://soapinterop.org//"" SOAP-ENV:encodingStyle=""http://schemas.xmlsoap.org/soap/encoding/""> <q1:Execute xmlns:q1=""http://secure.directtrack.com/api/soap_affiliate.php/dailyStatsInfo""><client xsi:type=""xsd:string"">MyClientNAme</client><add_code xsi:type=""xsd:string"">MyCode</add_code><password xsi:type=""xsd:string"">MyPassword</password><program_id xsi:type=""xsd:int"">161</program_id></q1:Execute></SOAP-ENV:Body></SOAP-ENV:Envelope>");


    return soapEnvelop;
}

private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
{
    using (Stream stream = webRequest.GetRequestStream())
    {
        soapEnvelopeXml.Save(stream);
    }
}

What is the problem? Thanks in advance.

like image 781
user1562231 Avatar asked Apr 19 '13 12:04

user1562231


People also ask

Is 500 an internal server error?

The HyperText Transfer Protocol (HTTP) 500 Internal Server Error server error response code indicates that the server encountered an unexpected condition that prevented it from fulfilling the request.

What is error 500 in C#?

"500.0 – Internal Server Error" is an IIS Error code meaning that the web service is unavailable. This means the error is with the API, not your client.

How do I fix error request failed with status code 500?

Most of the time, the issue is only temporarily and can be corrected by trying the page again. You can use the refresh/reload button, pressing F5 , or by trying the URL again from the address bar. Sometimes this error is caused when a service restarts, and you happen to catch it at exactly the wrong time.


1 Answers

An internal server error means that the error is on the server side. Your code may be calling the service exactly correctly, or you may be passing a parameter that the server doesn't know how to process (but the server code isn't smart enough to tell you so).

Without knowing more about the server and what it expects, it isn't possible to diagnose the issue.

That said, it's possible that your soap envelope is the issue. Are you sure you've entered the correct client name, add code, password, program id, etc?

like image 144
JDB Avatar answered Sep 29 '22 00:09

JDB