Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpWebRequest. The remote server returned an error: (500) Internal Server Error

I need help with HttpWebRequest in C#. Below lines of codes are working fine for local IIS but when I upload to remote server, it starts to giving me "The remote server returned an error: (500) Internal Server Error.". I have try many variations with GET and POST method but unable to figure it out what is the problem. Please have a look into below code and let me know what is wrong with this.

try
{
    string postData = "applicaitonid=abc&deviceid=xyz";
    string uri = System.Configuration.ConfigurationManager.AppSettings.Get("baseUrl") + System.Configuration.ConfigurationManager.AppSettings.Get("ABApiPath") + "ConfirmAppBinding/?" + postData;

    System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)WebRequest.Create(uri);
    request.Method = "POST"; // Set type Post
    //request.Method = "GET";
    request.UserAgent = Request.UserAgent.ToString();
    request.ContentType = @"application/json";
    request.MediaType = "application/json";
    request.Accept = "application/json";
    request.KeepAlive = false;
    request.ProtocolVersion = HttpVersion.Version11;
    //byte[] buffer = Encoding.GetEncoding("UTF-8").GetBytes(postData);
    request.Timeout = 500000;             //Increase timeout for testing

    Stream reqstr = request.GetRequestStream();
    //reqstr.Write(buffer, 0, buffer.Length);
    reqstr.Close();

    // Read Response
    var httpResponse = request.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        JavaScriptSerializer jsSerializer = new JavaScriptSerializer();
        JsonMessage.message = streamReader.ReadToEnd();
        streamReader.Close();
    }
}
catch (WebException e)
{
    JsonMessage.message = e.Message;
    return Json(JsonMessage, JsonRequestBehavior.AllowGet);
}

As I told you, I have used default GET method but it didn't solve the problem.

like image 392
sim Avatar asked Nov 18 '13 22:11

sim


People also ask

How do I fix the remote server returned an error 500 internal server error?

This can be a php timeout. In such cases, the webserver will return a 500 Internal Server Error. We can fix this error by increasing timeout values or setting other appropriate timeout options so that the remote server will not return a timeout error but wait for the request to be processed.

What does error 500 mean?

The HTTP status code 500 is a generic error response. It means that the server encountered an unexpected condition that prevented it from fulfilling the request.


2 Answers

Use this code for catch

catch (WebException e)
{
   string pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd().ToString();
   return pageContent;
}

It'll show the exact error you are facing.

like image 160
SanyTiger Avatar answered Sep 28 '22 00:09

SanyTiger


You can use try and catch block to find the root cause.

catch (WebException ex)
{
    string message = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
}
like image 43
Papun Sahoo Avatar answered Sep 28 '22 02:09

Papun Sahoo