我已经成功地生成了一个异步 Web 请求并为其设置了超时。但是,连接的设置仍在 UI 线程上,GetRequestStream
如果没有互联网连接,它似乎会冻结。如何解决这个问题?
private WebRequest getPostLoginRequest (string username, string password)
{
string postData = string.Format (@"<login><user>{0}</user><password>{1}</password></login>", username, password);
byte[] buf = Encoding.UTF8.GetBytes (postData);
var request = HttpWebRequest.Create (String.Format (@"{0}/auth/", baseServiceUrl));
request.Method = "POST";
request.ContentType = "application/xml";
request.ContentLength = buf.Length;
request.GetRequestStream ().Write (buf, 0, buf.Length);
return request;
}