0

Question Background:

I am attempting to make a simple HttpWebRequest to a Web API that I have hosted in Azure.

The Issue:

If I access my Web API end point through a browser or a request tool such as Postman I am receiving a JSON response with no Issues.

If I try accessing the same end point through a HttpWebRequest Call I receive a 500 fatal error but I am able to see in the response property of the Try Catch that the JSON response has been returned.

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

The Code:

The following is the simple request I am making to the Web API I have. As stated before, in the Catch the response is returning back the JSON I expect, as shown:

{"ResponseMessage":"OK","ResponseContent:[{"kind":"youtube#playlistItem","etag":"\"gMxXHe- ........ etc }]}

The Request:

 string url ="http://myapi.azurewebsites.net/api/videos/GetYouTubeVideos";

    try
    {
        HttpWebRequest req = WebRequest.Create(url) as HttpWebRequest;

        WebResponse wr = req.GetResponse();
    }
    catch (WebException wex)
    {
        var pageContent = new StreamReader(wex.Response.GetResponseStream())
                              .ReadToEnd();
    }

I appreciate that identifying the cause of a 500 isn't straight forward but can anyone provide a reason I would be getting this error but also still receiving the JSON from the service that is apparently throw an exception?

user1352057
  • 3,162
  • 9
  • 51
  • 117
  • Does your JSON response is valid? The response you've given is not valid..probably it because you have added just example data..second thing add your full method code and add exception to the response message! – Divyang Desai Jan 28 '17 at 05:14

1 Answers1

0

there are some properties about WebHeaders you must be care!example codes:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Method = "GET";
webRequest.Proxy = webProxy;
webRequest.AllowAutoRedirect = true;
webRequest.Timeout = 20 * 1000;
webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36";
WebResponse wr = webRequest.GetResponse()
Johan Shen
  • 158
  • 6