I want to create my own remote app for a domotic project which uses Domoticz, with Universal Apps (Win 10) in C#. I use Basic-Auth and it works perfectly with WinForm or WPF project, I can connect and get (in this case) or set values in the server :
private async void request()
{
string uri = @"http://username:password@192.168.1.1:8080/json.htm?type=devices&filter=all&used=true&order=Name";
HttpClient client = new HttpClient();
string token = Convert.ToBase64String(Encoding.ASCII.GetBytes("username:password"));
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", token);
string body = await client.GetStringAsync(uri);
}
However, this sample doesn't work with Universal Apps (Win 10), I get this exception :
An error occurred while sending the request.
When I look at InnerException I see that :
A security problem occurred. (Exception from HRESULT: 0x800C000E)
Is there a solution to connect my app to my domotic server like WinForms apps?
Do not include the user name and password in the URI (RFC 3986 3.2.1 User Information).
Instead, use a HttpClientHandler
and a NetworkCredential
to pass the user info, i.e.:
Uri uri = new Uri(
"http://192.168.1.1:8080/json.htm?type=devices&filter=all&used=true&order=Name");
HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new System.Net.NetworkCredential(
"username",
"password");
HttpClient client = new HttpClient(handler);
await httpClient.GetAsync(uri);
Remove @ from the URI -
see string uri = @"http://username:password192.168.1.1:8080/json.htm?type=devices&filter=all&used=true&order=Name";
User contributions licensed under CC BY-SA 3.0