10

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
sim
  • 197
  • 1
  • 4
  • 9
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Nov 18 '13 at 22:29
  • 1
    The problem is on the server side. – Jim Rhodes Nov 18 '13 at 22:32
  • Have you looked at the response body in the exception? See http://stackoverflow.com/questions/11828843/c-sharp-webexception-how-to-get-whole-response-with-a-body. – John Saunders Nov 18 '13 at 22:32

3 Answers3

18

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.

SanyTiger
  • 666
  • 1
  • 8
  • 23
4

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

catch (WebException ex)
{
    string message = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
}
Papun Sahoo
  • 407
  • 5
  • 13
1

i will take a stab in the dark

Could it be you have a spelling mistake in your querystring and when reference the key in code it comes back with a nullvalue exception ?

{ String postData = "applicaitonid=abc&deviceid=xyz"; 
}

should be

{ String postData = "applicationid=abc&deviceid=xyz";  }
Wombelite
  • 302
  • 1
  • 6
  • Ignore spelling mistake in query string. Its typo in here. Code is working fine in locally. But when I publish it to remote server then its giving "The remote server returned an error: (500) Internal Server Error". – sim Nov 18 '13 at 22:44
  • There are a lot of "Variables", how about your uri value? does the abapipath directory exist on the remote server ? – Wombelite Nov 19 '13 at 05:59
  • @Wombelite I indeed was hitting same issue. Instead of `userId`, I was using 'userID'. Thanks. – Atul Kumar Apr 27 '16 at 15:12