Domanda

I am trying to build an app on WP8 that will download json data from a webserver using httpclient from the NuGet package, but that content is not hosted on a standard port (85 is what is used). When I try to download it I get "Invalid Port Specified". I'm assuming this means it is restricted to 80, 8080, 443, or 8443 by default but I'm hoping there is some way to get it to work for a non-standard web port. I have the same thing working in my Windows 8 metro app. Any suggestions on how to fix it? Just to state in advance, no I cannot change the port on the server side of things.

HttpClientHandler handler = new HttpClientHandler();
handler.Credentials = new NetworkCredential(App.UserName, App.Password);
handler.UseDefaultCredentials = false;
handler.AllowAutoRedirect = true;
HttpClient client = new HttpClient(handler);
HttpResponseMessage response = await client.GetAsync(url);
if (response.StatusCode != System.Net.HttpStatusCode.OK)
{
    MessageBoxResult result = MessageBox.Show("Error accessing server. " + response.StatusCode.ToString(), "Server access failure", MessageBoxButton.OKCancel);
    return "";
}
else
{
    return await response.Content.ReadAsStringAsync();
}
È stato utile?

Soluzione

Include the port information in the url.

HttpResponseMessage response = await client.GetAsync("http://www.bob.com:85/endpoint.json");
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top