1

With httpClient I do a POST request, however, the problem is that even if there is no connection because the destination API is down, it does not return any errors.

using (var httpClient = new HttpClient())
{
    httpClient.BaseAddress = new Uri(proxySettingsConfiguration.BaseAdress);

    var json = JsonConvert.SerializeObject(item);
    var parameters = new Dictionary<string, string> { { "Application", "Demo" }, { "Payload", json } };
    var encodedContent = new FormUrlEncodedContent(parameters);

    var response = await httpClient.PostAsync(proxySettingsConfiguration.RequestUri, encodedContent);
}

Is there a way to check beforehand if the destination works? Without using PING

gregory
  • 10,969
  • 2
  • 30
  • 42
gtx911
  • 1,189
  • 4
  • 25
  • 46
  • https://stackoverflow.com/questions/39343399/easy-best-way-to-check-if-webapi-is-available-in-c-sharp-code-behind see if this helps – Harry Sep 06 '18 at 09:39
  • you can send empty request and check reply status in request head – Presto Sep 06 '18 at 09:39
  • 2
    Do you not get a HttpResponse code? If there's an error you'll get an error code like 404, 500 etc – RoguePlanetoid Sep 06 '18 at 09:40
  • You should get a timeout exception after (I think) 100 seconds. – user247702 Sep 06 '18 at 09:40
  • 3
    Possible duplicate of [What is the best way to check for Internet connectivity using .NET?](https://stackoverflow.com/questions/2031824/what-is-the-best-way-to-check-for-internet-connectivity-using-net) – Adrian Sep 06 '18 at 09:41
  • As for my duplicate, it's the same principle for this given interface. – Adrian Sep 06 '18 at 09:41
  • This might be interesting to read. Read the comments of the first answer. https://stackoverflow.com/questions/15067014/c-sharp-detecting-tcp-disconnect – nl-x Sep 06 '18 at 09:42
  • @Adriani6 `httpClient` not has `OpenRead` like `WebClient` – gtx911 Sep 06 '18 at 09:48
  • You don't need `OpenRead` as I said it's the same principle for the interface you're using... please see the `try/catch` specifically.. other way to do it would be using ping but neither methods are 100% reliable check if there is an internet connection. Therefore this question is a duplicate. – Adrian Sep 06 '18 at 09:50
  • No amount of pre-checking will tell you whether the server is reachable and usable *now* for the specific action you're wanting it to perform. – Damien_The_Unbeliever Sep 06 '18 at 10:04

1 Answers1

1

Recently I used Youtube API, and I had the same problems with getting the data, searching the song name...etc, what I did is I put the (in your case)

response = await httpClient.PostAsync(proxySettingsConfiguration.RequestUri, encodedContent);

in try/catch, so it would be

try
        {
            response = await httpClient.PostAsync(proxySettingsConfiguration.RequestUri, encodedContent);
        }
        catch (Exception ex)
        {//In my case I was looking at this error

            if (ex.GetType().ToString() == "Google.Apis.Auth.OAuth2.Responses.TokenResponseException")
            {
                MessageBox.Show("Failed to login", "Error on login",
                       MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                MessageBox.Show("Failed to login", "Error on login",
                       MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

If you print out the Exception(messagebox,debug) you will see it's a type of error,or you can search more information about the API you are using, and see what error types can it throw,find the one that's good,you can add multiple ones with different messages,so your program will not crash if it has some kind of errors. Hope it helps a little bit.

Edwinke
  • 69
  • 1
  • 6