1

I'm trying to connect from a http client but if server is down for some reason I'm getting an exception while sending the request.

 string Send = "Request";
        using (var client = new HttpClient())
        {
            client.Timeout = TimeSpan.FromMilliseconds(2000);
            var response = await client.PostAsync("http://192.168.1.15:8282/", new StringContent(Send, Encoding.UTF8, "application/json"));

            if (response.IsSuccessStatusCode)
            {
                string content = await response.Content.ReadAsStringAsync();
                MessageBox.Show(content);
            }
        }

Is there any method to check if server is not down. Also how can I set server connection timeout, Send request timeout and receive answer timeout?

Bakri Bitar
  • 1,543
  • 18
  • 29
Dhi
  • 157
  • 3
  • 11
  • 2
    How about using try catch for exception handling? – Bakri Bitar Sep 22 '18 at 21:26
  • Ok i will do that. I just wonder if it has something similar with tcp client TcpClient client = new TcpClient(); if (client.ConnectAsync(SERVER_IP, PORT_NO).Wait(2000)) No Problem i will use try-catch.Also does it have Request or Receive timeout? – Dhi Sep 22 '18 at 21:31
  • You can write multiple catches depending on exception type, or if it doesn't matter you can write just one and it will catch all unhandled exceptions :) – Bakri Bitar Sep 22 '18 at 21:34
  • Thank you for answers what about timeouts? – Dhi Sep 22 '18 at 21:37
  • 2
    Chcek this out https://stackoverflow.com/questions/48155951/change-default-timeout – Bakri Bitar Sep 22 '18 at 21:49

0 Answers0