1
votes

I have a UWP app written in C#.

For some specific users (i.e. specific PCs), the app is not able to perform HTTP requests.

Those PCs are not under any VPN, they turned off any firewall or antivirus, they tried with several connections (e.g. home router, phone hotspot, public wifi, etc.), always with the same result.

Opening a browser and browsing to

https://ltbackend.azurewebsites.net/diagnostic/ping

they are able to see the correct page (actually, a plain text "OK").

But if they use the app (which performs an HTTP GET call using C#), this one fails.

This is the code in C# that we use:

string pingUrl = "https://ltbackend.azurewebsites.net/diagnostic/ping";
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, pingUrl);
using (HttpClient httpClient = new HttpClient())
{
 try
 {
  using (HttpResponseMessage response = await httpClient.SendAsync(req))
  {
    string stringRes = await response.Content.ReadAsStringAsync();
    HttpStatusCode respCode = response.StatusCode;

    // .... our biz logic with stringRes and respCode...
  }
 }
 catch (HttpRequestException e)
 {
    // the ping request for those users throws this exception...
    // the error message is "An error occurred while sending the request."
 }
}
1
could be lots of things - looking for certain tls versions, headers, user-agents, etc. what exactly do you get as a response? does your uwp have the permissions to use the network?Daniel A. White
Usually, this works (for hundreds of thousands of installations spreaded worldwide), but for those specific devices, we actually don't get a response, we get an HttpRequestException with message "An error occurred while sending the request."Cristiano Ghersi
Don't create / dispose lots of HttpClient's. Reuse them across requests. Is the system clock in sync? a bad clock could cause false positive ssl failures. Are their browsers using a proxy?Jeremy Lakeman

1 Answers

0
votes

But if they use the app (which performs an HTTP GET call using C#), this one fails.

I have run your code within UWP platform, it could work well and return 'ok' string. But it looks you used HttpClient under System.Net.Http namespace, and it often used in cross-platform, such as xamarin app.

For sending get request within UWP, we suggest you use Windows.Web.Http.HttpClient. The following is simple get method you could refer to.

Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient();

//Add a user-agent header to the GET request. 
var headers = httpClient.DefaultRequestHeaders;

//The safe way to add a header value is to use the TryParseAdd method and verify the return value is true,
//especially if the header value is coming from user input.
string header = "ie";
if (!headers.UserAgent.TryParseAdd(header))
{
    throw new Exception("Invalid header value: " + header);
}

header = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)";
if (!headers.UserAgent.TryParseAdd(header))
{
    throw new Exception("Invalid header value: " + header);
}

Uri requestUri = new Uri("https://ltbackend.azurewebsites.net/diagnostic/ping");

//Send the GET request asynchronously and retrieve the response as a string.
Windows.Web.Http.HttpResponseMessage httpResponse = new Windows.Web.Http.HttpResponseMessage();
string httpResponseBody = string.Empty;
try
{
    //Send the GET request
    httpResponse = await httpClient.GetAsync(requestUri);
    httpResponse.EnsureSuccessStatusCode();
    httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
}
catch (Exception ex)
{
    httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
}

For more info please refer HttpClient document.