WebException details changed on VS.Net

446 Views Asked by At

A Rest API call throws an exception on post. And that is

The remote server returned an error: (404) Not Found.

The following code calling the api

 try
 {
        RestClient client = new RestClient(...);
        string apiResponse = client.MakeRequest(..);
 }    
 catch (WebException wex)
 {
        throw wex;
}

It works fine when the fiddler is capturing traffic. But if the fiddler is not capturing then client received the following exception.

The remote server returned an error: (502) Bad Gateway.

PS. Postman (Chrome extension) and Soap UI also received 404: Not Found

UPDATED: This is the code and the error is received while debugging

public string MakeRequest(string parameters)
{
    var request = (HttpWebRequest)WebRequest.Create(_RestEndPoint + (parameters ?? String.Empty));
    request.Proxy = WebRequest.DefaultWebProxy;
    request.ContentLength = 0;
    request.Method = _Method.ToString();
    request.ContentType = _ContentType;
    request.KeepAlive = true;
    if (_RequestHeader != null)
    {
        foreach (var hdr in _RequestHeader)
        {
            request.Headers.Add(hdr.Key, hdr.Value);
        }
    }
    if (_Method == HttpVerb.POST || _Method == HttpVerb.PUT)
    {
       if (!string.IsNullOrEmpty(_PayLoad))
       {                    
          var bytes = Encoding.UTF8.GetBytes(_PayLoad);
          request.ContentLength = bytes.Length;

          using (var writeStream = request.GetRequestStream())
          {
              writeStream.Write(bytes, 0, bytes.Length);
              writeStream.Close();                        
          }
        }
    }
    using (var response = (HttpWebResponse)request.GetResponse())//Error returns here
    {
        var responseValue = string.Empty;
        checkForResponseExceptions(response.StatusCode, parameters);

        // grab the response
        using (var responseStream = request.GetResponse().GetResponseStream())
        {
            if (responseStream != null)
            {
                using (var reader = new StreamReader(responseStream))
                {
                    responseValue = reader.ReadToEnd();
                }
            }
        }

        return responseValue;
    }
}

Can anyone please point out what am i missing?

Update 2:

I have tried the following code and have the same issue

if (_Method == HttpVerb.POST)
{
    using (var wb = new WebClient())
    {
        wb.Headers[HttpRequestHeader.ContentType] = _ContentType;
        foreach (var hdr in _RequestHeader)
        {
            wb.Headers.Add(hdr.Key, hdr.Value);
        }
        try
        {
            var response = wb.UploadString(_RestEndPoint + (parameters ?? String.Empty), _Method.ToString(), _PayLoad);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

So, I guess something is wrong in the network settings as @GSerg commented on Dec 4 at 10:01. But there are no proxy set up in connection tab of IE settings. What and how should i check to get rid of this problem??

1

There are 1 best solutions below

2
On BEST ANSWER

HTTP errors are grouped by "type of errors"

4xx errors are "client" errors, errors that are linked to the user doing a mistake, like 404 is the client asking for something that does not exist on the server.

5xx errors are "server" errors, meaning that the server had a problem answering the request, because of internal issues.

The 502 bad gateway is usually sent from a server when he couldn't get a request from another server. In your case, it could be coming from fiddler, because you're probably still making the query to fiddler who is stopped, and thus cannot answer.