【发布时间】:2014-04-06 01:33:12
【问题描述】:
我正在尝试在 WP8 上构建一个应用程序,该应用程序将使用 NuGet 包中的 httpclient 从网络服务器下载 json 数据,但该内容未托管在标准端口上(使用的是 85)。当我尝试下载它时,我得到“指定的端口无效”。我假设这意味着默认情况下它被限制为 80、8080、443 或 8443,但我希望有一些方法可以让它适用于非标准 Web 端口。我在我的 Windows 8 Metro 应用程序中也有同样的工作。关于如何修复它的任何建议?只是提前声明,不,我不能更改服务器端的端口。
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();
}
【问题讨论】:
标签: c# windows-phone-8 dotnet-httpclient