Reputation: 310
First, I must admit that I'm not certain whether I should be using Windows.Web.Http.HttpClient or System.Net.Http.HttpClient. It seems Windows.Web.Http.HttpClient is the way to go for UWP, but I've tried both without success.
From the URL, I expect to receive a JSON object. When I copy and paste the URL into a browser, I can see the JSON object just fine. If I leave the cache alone after connecting with a browser, the UWP has no problem reading from the cache. I used my same code to connect to https://jsonplaceholder.typicode.com/todos/1 and was able to retrieve the JSON object from there without any issues. Here's my basic attempt:
async void Connect()
{
using (Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient())
{
Uri uri = new Uri(@"https://www.nottheactualdomainname.com/?api=software-api&[email protected]&licence_key=xxxx&request=status&product_id=xxxxinstance=20181218215300");
Windows.Web.Http.HttpResponseMessage httpResponse = new Windows.Web.Http.HttpResponseMessage();
string httpResponseBody = "";
try
{
httpResponse = await httpClient.GetAsync(uri);
httpResponse.EnsureSuccessStatusCode();
httpResponseBody = await httpResponse.Content.ReadAsStringAsync();
if (JsonObject.TryParse(httpResponseBody, out JsonObject keyValuePairs))
{
//handle JSON object
}
else
{
//didn't recieve JSON object
}
}
catch (Exception ex)
{
httpResponseBody = "Error: " + ex.HResult.ToString("X") + " Message: " + ex.Message;
}
}
}
I have tried including this at various points (i.e. before the using statement, right before initializing httpResponse, and right before the try statement):
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
As well as this (knowing that I would only able to do this while debugging):
//for debugging ONLY; not safe for production
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
Sometimes I get this error:
Error: 80131500 Message: Response status code does not indicate success: 403 ().
And sometiemes I get this error:
Error: 80190193 Message: Forbidden (403).
Or both. I have tried this with both Windows.Web.Http.HttpClient and System.Net.Http.HttpClient.
I will eventually need to make sure that my URL includes appropriate credentials for the server. However, incorrect credentials should just return a JSON with error information, and I'm not getting that. I can connect to https://www.nottheactualdomainname.com. What should I check next?
Upvotes: 1
Views: 730
Reputation: 310
I found the answer here
I was able to use the browser developer tools to look at the request headers. Then I added this:
httpClient.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
Upvotes: 1