发布到表单会导致 AggregateException
Posting to a form results in an AggregateException
所以我正在尝试 post 使用 HttpClient(和 CloudFlareUtilities)的表单,并将其初始化如下所示:
var handler = new ClearanceHandler {
MaxRetries = 4,
};
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Connection.Clear();
client.DefaultRequestHeaders.ConnectionClose = false;
client.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36");
client.Timeout = TimeSpan.FromSeconds(30);
创建客户端成功,没有出现明显的问题。但是,我创建了一个如下所示的 POST 请求:
var content = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("login", "username"),
new KeyValuePair<string, string>("password", "pw"),
new KeyValuePair<string, string>("cookie_check", "1"),
new KeyValuePair<string, string>("redirect", "myRedirect"),
new KeyValuePair<string, string>("register", "0"),
new KeyValuePair<string, string>("remember", "1")
});
和post数据:
var post = client.PostAsync("https://www.mc-market.org/login/login", content).Result.Content.ReadAsStreamAsync().Result;
上述行抛出错误:
Inner Exception 1:
HttpRequestException: Error while copying content to a stream.
Inner Exception 2:
IOException: The read operation failed, see inner exception.
Inner Exception 3:
WinHttpException: The operation has been canceled
我可以从中解释的是出了点问题,某些事情取消了任务,但我自己从未取消过它。
将错误行限制为
var post = client.PostAsync("https://www.mc-market.org/login/login", content);
给出同样的错误。
是否我创建的内容不正确?对我可能去过的地方的一些指示表示赞赏。
为简单起见,我决定不使用 HttpClient
。
相反,我选择了 PhantomJS 和 Selenium,它本身支持 Javascript in-browser(我决定启用 Cloudflare 旁路)。
然后我使用它的 SendKeys()
方法在网站 https://www.mc-market.org/login/
的登录字段中输入(而不是 https://www.mc-market.org/login/login
),然后按下按钮 Click()
。示例如下所示;
driver.FindElementById("ctrl_pageLogin_login").SendKeys("NAME");
driver.FindElementById("ctrl_pageLogin_password").SendKeys("PASS");
driver.FindElementById("ctrl_pageLogin_remember").Click();
driver.FindElementByCssSelector("input.button.primary").Click();
结果简单多了,而且效果很好。
所以我正在尝试 post 使用 HttpClient(和 CloudFlareUtilities)的表单,并将其初始化如下所示:
var handler = new ClearanceHandler {
MaxRetries = 4,
};
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Connection.Clear();
client.DefaultRequestHeaders.ConnectionClose = false;
client.DefaultRequestHeaders.Add("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " +
"(KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36");
client.Timeout = TimeSpan.FromSeconds(30);
创建客户端成功,没有出现明显的问题。但是,我创建了一个如下所示的 POST 请求:
var content = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("login", "username"),
new KeyValuePair<string, string>("password", "pw"),
new KeyValuePair<string, string>("cookie_check", "1"),
new KeyValuePair<string, string>("redirect", "myRedirect"),
new KeyValuePair<string, string>("register", "0"),
new KeyValuePair<string, string>("remember", "1")
});
和post数据:
var post = client.PostAsync("https://www.mc-market.org/login/login", content).Result.Content.ReadAsStreamAsync().Result;
上述行抛出错误:
Inner Exception 1: HttpRequestException: Error while copying content to a stream.
Inner Exception 2: IOException: The read operation failed, see inner exception.
Inner Exception 3: WinHttpException: The operation has been canceled
我可以从中解释的是出了点问题,某些事情取消了任务,但我自己从未取消过它。
将错误行限制为
var post = client.PostAsync("https://www.mc-market.org/login/login", content);
给出同样的错误。
是否我创建的内容不正确?对我可能去过的地方的一些指示表示赞赏。
为简单起见,我决定不使用 HttpClient
。
相反,我选择了 PhantomJS 和 Selenium,它本身支持 Javascript in-browser(我决定启用 Cloudflare 旁路)。
然后我使用它的 SendKeys()
方法在网站 https://www.mc-market.org/login/
的登录字段中输入(而不是 https://www.mc-market.org/login/login
),然后按下按钮 Click()
。示例如下所示;
driver.FindElementById("ctrl_pageLogin_login").SendKeys("NAME");
driver.FindElementById("ctrl_pageLogin_password").SendKeys("PASS");
driver.FindElementById("ctrl_pageLogin_remember").Click();
driver.FindElementByCssSelector("input.button.primary").Click();
结果简单多了,而且效果很好。