as

Friday, 28 February 2014

The remote server returned an error: (500) Internal Server Error - While sending POST request C#

When you send the POST request to server, you may get the response from the server saying The remote server returned an error: (500) Internal Server Error.

This usually happens when you send the POST request with incorrect post data or wrong content Type.

To fix this issue you will have to see actual data being sent to particular URL using Google Chrome Developer Tools. You can open the developer tools using ctrl+shift+J in google chrome browser.
In network tab, you can see the GET and POST requests details along with response and cookies being transferred.

View POST request data in Chrome 
As you can see in above figure, the content type for the POST request is application/x-www-form-urlencoded

The Form data is email=gf&password=dfg&Submit=Login
If the data is encoded, you will have to use HttpUtility.UrlEncode method to encode the string.

Once you know actual data being sent to server you will have to send same data to server. You will have to also ensure that the CookieContainer is sent to the server to maintain the session.
Also make sure that you have contentType set as displayed in above figure.
To summarize, You will have to ensure below things to fix the internal server error.
  1. Before sending the post request, you will have to send the GET request. While sending the GET request, use the CookieContainer object to send/ receive the Cookies. Ensure that you use the same CookieContainer object while sending the following POST request. This will ensure that Server gets the required cookies.
  2. Ensure that POST data is encoded correctly.Sometimes the data is sent from the hidden fields. So you will have to send this data as well in form data.
  3. Ensure that your POST url is correct.
  4. Ensure that Content Type is correct.
If you still face the issue, you can contact me at reply2sagar@gmail.com

What do you think on this topic? Please express your opinion through comment below. You can write to me at reply2sagar@gmail.com

Sponsored Links

Popular Posts

Comments

ShareThis