locked
How to tell server error from no-internet when using HttpClient RRS feed

  • Question

  • Hello,

    I am using HttpClient.PostAsync to get some data.

    Tried something like the following:

    try
    {
          HttpClient HttpClientInstance = new HttpClient();
    
          HttpResponseMessage response = await HttpClientInstance.PostAsync(requestUri, content);
          try
          {
              response.EnsureSuccessStatusCode();
          }
          catch (HttpRequestException hre)
          {
              throw hre;  // mark 1
          }
    } catch (Exception e)
    {
       throw e;  // mark 2
    }
     

    But when the request failed, either because of server error, or no internet connection, both returned HttpRequestException with HResult -2146233088 on "mark 2" (didn't go mark 1). How to tell if the failure of PostAsync is because of server error, or no internet?

    Thanks a lot.

    Chunmiao

    Wednesday, September 18, 2013 6:03 AM

Answers

  • i had the same issue and fixed it with this:

    HttpClient client = CreateHttpClient(account);
                try
                {
    
                    var url = string.Format("{0}/Projects?$top={1}", account.Endpoint, int.MaxValue);
    
                    var reponse = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, url));
    
                    if (!reponse.IsSuccessStatusCode)
                        throw new RequestException();
    
                    var stream = await reponse.Content.ReadAsStreamAsync();
    
                    var streamReader = new StreamReader(stream);
                    var reader = new JsonTextReader(streamReader);
    
                    JObject json = JObject.Load(reader);
                    var projectsJson = (JArray)json["d"]["results"];
                    return projectsJson.Select(x => new Project { Name = (string)x["Name"], Account = account }).ToList();
                }
                catch (HttpRequestException e)
                {
                    throw new ConnectionException();
                }

    the request exception is when the server gives an error. The coonnectionexception when cant reach the server


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Chunmiao Thursday, September 19, 2013 9:41 AM
    Wednesday, September 18, 2013 8:48 AM

All replies

  • Hello Chunmiao,

    you can handle WebException in catch. Try this code:

    catch (WebException ex)
                            {
                          //Handle Exception
                            }


    Wednesday, September 18, 2013 6:35 AM
  • Thanks Khant for your prompt reply.

    What's the purpose to catch WebException, for no-internet-connection? I tried to catch it before "catch (Exception e)" but didn't get it when disconnected internet.

    Wednesday, September 18, 2013 8:08 AM
  • Hello Chunmiao,

    WebException is throw due to 2 reason:

    1. when the URL entered is wrong(A url that does not exist).
    2. When there is no internet connection.
    Wednesday, September 18, 2013 8:38 AM
  • i had the same issue and fixed it with this:

    HttpClient client = CreateHttpClient(account);
                try
                {
    
                    var url = string.Format("{0}/Projects?$top={1}", account.Endpoint, int.MaxValue);
    
                    var reponse = await client.SendAsync(new HttpRequestMessage(HttpMethod.Get, url));
    
                    if (!reponse.IsSuccessStatusCode)
                        throw new RequestException();
    
                    var stream = await reponse.Content.ReadAsStreamAsync();
    
                    var streamReader = new StreamReader(stream);
                    var reader = new JsonTextReader(streamReader);
    
                    JObject json = JObject.Load(reader);
                    var projectsJson = (JArray)json["d"]["results"];
                    return projectsJson.Select(x => new Project { Name = (string)x["Name"], Account = account }).ToList();
                }
                catch (HttpRequestException e)
                {
                    throw new ConnectionException();
                }

    the request exception is when the server gives an error. The coonnectionexception when cant reach the server


    Microsoft Certified Solutions Developer - Windows Store Apps Using C#

    • Marked as answer by Chunmiao Thursday, September 19, 2013 9:41 AM
    Wednesday, September 18, 2013 8:48 AM
  • Thanks a lot Dave, this works!

    Chunmiao

    Thursday, September 19, 2013 9:42 AM