我有如下方法来获取远程 URL 字符串。
public string FnGetURLContent(string _QsURL)
{
string _Return = "";
try
{
HttpWebRequest _HttpWebRequest = (HttpWebRequest)WebRequest.Create(_QsURL);
_HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)";
_HttpWebRequest.MaximumAutomaticRedirections = 4;
_HttpWebRequest.MaximumResponseHeadersLength = 4;
_HttpWebRequest.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse _HttpWebResponse = (HttpWebResponse)_HttpWebRequest.GetResponse();
Stream receiveStream = _HttpWebResponse.GetResponseStream();
encoding format.
StreamReader _StreamReader = new StreamReader(receiveStream, Encoding.UTF8);
_Return = _StreamReader.ReadToEnd();
_HttpWebResponse.Close();
_StreamReader.Close();
}
catch (WebException _WebException)
{
_Return = "";
}
return _Return;
}
当方法运行时,表单 UI 会冻结,直到方法完成处理。我该如何改进代码?
先感谢您