我目前有問題,那是阻止我的UI線程。我知道這是在下面的函數發生的事情:C#線程UI被阻止|可能的原因WebRequest.Create?
public async Task<string> function(string username, string password, string handle)
{
try
{
string finalStr;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://url.com");
request.CookieContainer = cookie;
request.AllowAutoRedirect = true;
var response = await request.GetResponseAsync();
string str = new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();
string str2 = this.getToken(str, "_token\" value=\"", "\">", 0);
string[] textArray1 = new string[] { "postVariables=" + str2 };
HttpWebRequest httpWebRequest_0 = (HttpWebRequest)WebRequest.Create("https://url.com");
httpWebRequest_0.CookieContainer = cookie;
httpWebRequest_0.Method = "POST";
httpWebRequest_0.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
httpWebRequest_0.Referer = "https://twitter.com/settings/account";
httpWebRequest_0.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2";
httpWebRequest_0.AllowAutoRedirect = true;
httpWebRequest_0.ContentType = "application/x-www-form-urlencoded";
byte[] bytes = Encoding.ASCII.GetBytes(string.Concat(textArray1));
httpWebRequest_0.ContentLength = bytes.Length;
Stream requestStream = await httpWebRequest_0.GetRequestStreamAsync();
await requestStream.WriteAsync(bytes, 0, bytes.Length);
var response2 = await httpWebRequest_0.GetResponseAsync();
using (StreamReader reader = new StreamReader(response2.GetResponseStream()))
{
finalStr = reader.ReadToEnd();
}
if (finalStr.Contains(handle))
{
return "success";
}
else
{
requestStream.Close();
return "error";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
我相信這是這部分功能:
HttpWebRequest httpWebRequest_0 = (HttpWebRequest)WebRequest.Create("https://url.com");
我怎麼可以創建一個async
WebRequest.Create?還有什麼我做錯了嗎?
我欣賞任何形式的幫助和建議。
我認爲你複製並粘貼了錯誤的鏈接* Dns.GetHostByName * –
@ScottChamberlain它是* Dns.GetHostByName *被引用的地方。不是它的代碼。 (也許我應該提到哪個使用* gethostname * API) –
該鏈接將我帶到Internal.cs中的屬性WebRequestPrefixElement.Creator。在'NclUtilities.GetLocalHost'函數中禁用的'else //!FEATURE_PAL'節中,除了Internal.cs中的任何地方,我沒有看到Dns.GetHostByName被調用。 –