我正在尝试集成http的连接方法。我找到了UWP的代码:
StreamSocket clientSocket = new StreamSocket();
HostName serverHost = new HostName("ecosia.org");
string serverServiceName = "https";
// Try to connect to contoso using HTTP (port 80)
try {
// Call ConnectAsync method with a plain socket
await clientSocket.ConnectAsync(serverHost, serverServiceName, SocketProtectionLevel.PlainSocket);
NotifyUser("Connected");
}
catch (Exception exception) {
Debug.WriteLine("Connection failed.");
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown) {
throw;
}
NotifyUser("Connect failed with error: " + exception.Message);//, NotifyType.ErrorMessage);
// Could retry the connection, but for this simple example
// just close the socket.
clientSocket.Dispose();
clientSocket = null;
return;
}
这适用于建立https连接。但是,我需要访问一个http页面,并尝试更改前两行,如下所示:
HostName serverHost = new HostName("www.seite.com");
string serverServiceName = "http";
但这在UWP中失败了。我怎么解决这个问题?请注意,我尝试通过HttpClient类建立,我面临同样的问题。