I am developing an C# console application for testing whether a URL is valid or works. It works well for most of URLs and can get response with HTTP Status Code from target website. But when testing some other URLs, the application throw an "An error occurred while sending the request" exception when running HttpClient.SendAsync method. So I can't get any response or HTTP Status Code even this URL actually works in the browser. I am desperate to find out how to handle this case. If the URL doesn't work or the server reject my request, it should at least give me corresponding HTTP Status code.
Here are the simplified code of my test application:
using System;
using System.Net.Http;
using System.Threading.Tasks;
namespace TestUrl
{
class Program
{
static void Main(string[] args)
{
// var urlTester = new UrlTester("http://www.sitename.com/wordpress"); // works well and get 404
// var urlTester = new UrlTester("http://www.fc.edu/"); // Throw exception and the URL doesn't work
var urlTester = new UrlTester("http://www.ntu.edu.tw/english/"); // Throw exception and the URL works actually
Console.WriteLine("Test is started");
Task.WhenAll(urlTester.RunTestAsync());
Console.WriteLine("Test is stoped");
Console.ReadKey();
}
public class UrlTester
{
private HttpClient _httpClient;
private string _url;
public UrlTester(string url)
{
_httpClient = new HttpClient();
_url = url;
}
public async Task RunTestAsync()
{
var httpRequestMsg = new HttpRequestMessage(HttpMethod.Head, _url);
try
{
using (var response = await _httpClient.SendAsync(httpRequestMsg, HttpCompletionOption.ResponseHeadersRead))
{
Console.WriteLine("Response: {0}", response.StatusCode);
}
}
catch (Exception e)
{
}
}
}
}
}