在C#中使用HttpClient提交表单
我通过htmlagilitypack获取网站表单,设置表单变量并尝试提交表单。看起来一切正常,但表单提交的响应为空。在C#中使用HttpClient提交表单
static void Main(string[] args)
{
string urlAddress = "mywebsite";
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(urlAddress);
// Post link
var post = doc.GetElementbyId("post").GetAttributeValue("href", "");
doc = web.Load(post);
// get the form
var form = doc.DocumentNode.SelectSingleNode("//form[@class='picker']");
// get the form URI
string actionValue = form.Attributes["action"]?.Value;
System.Uri uri = new System.Uri(actionValue);
// Populate the form variable
var formVariables = new List<KeyValuePair<string, string>>();
formVariables.Add(new KeyValuePair<string, string>("id", "ho"));
var formContent = new FormUrlEncodedContent(formVariables);
// submit the form
HttpClient client = new HttpClient();
var response = client.PostAsync(uri, formContent);
}
有谁知道为什么我的变量响应为空?
感谢
HttpClient.PostAsync
回报Task<HttpResponseMessage>
所以通常这将需要等待。正如你在main方法中使用它,你将不得不从任务
var response = client.PostAsync(uri, formContent).GetAwaiter().GetResult();
或者简单的
var response = client.PostAsync(uri, formContent).Result;
在这两种情况下的反应得到的结果将是HttpResponseMessage
一个实例。您可以检查该实例的HTTP状态和响应的内容。
如果使用.NET的核心,你甚至还可以使用
static async Task Main(string[] args) {
//..code removed for brevity
var response = await client.PostAsync(uri, formContent);
var content = await response.Content.ReadAsStringAsync();
//...
}
我更改为client.PostAsync(uri,formContent).Result但它被异常捕获:\t InnerException \t {“服务器提交的协议违规。部分= ResponseStatusLine”} \t System.Exception {System.Net.WebException} – user6824563
@ user6824563你将不得不检查是什么导致了错误https:// stackoverflow。com/questions/18496451/c-httpclient-server-committed-a-protocol-violation-section-responsestatu – Nkosi
它可能与我正在做的网站有关吗? – user6824563
异步main方法丢失。 Result
在您的PostAsync
。简单地说,行更改为:
var response = client.PostAsync(uri, formContent).Result;
.Result
然而,同步运行的任务。如果发生异常使其稍微难以调试,则返回AggregateException
。
如果您使用的是Visual Studio 2017 Update 15.3或更高版本和C#7.1,但现在支持aysnc
main
。
可以按如下方式修改代码:
static async Task Main()
{
string urlAddress = "mywebsite";
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(urlAddress);
// Post link
var post = doc.GetElementbyId("post").GetAttributeValue("href", "");
doc = web.Load(post);
// get the form
var form = doc.DocumentNode.SelectSingleNode("//form[@class='picker']");
// get the form URI
string actionValue = form.Attributes["action"]?.Value;
System.Uri uri = new System.Uri(actionValue);
// Populate the form variable
var formVariables = new List<KeyValuePair<string, string>>();
formVariables.Add(new KeyValuePair<string, string>("id", "ho"));
var formContent = new FormUrlEncodedContent(formVariables);
// submit the form
HttpClient client = new HttpClient();
var response = await client.PostAsync(uri, formContent);
}
这将帮助你await
的async
任务。如果您得到任何例外,您将收到实际例外,而不是AggregateException
。
请务必注意,C#7.1目前尚未默认启用。要启用7.1功能,您需要更改项目的语言版本设置。
请参阅此link了解更多详情。
您是否收到回复,如果您是开发者控制台,它会说什么? –
它说:Id = 3,Status = WaitingForActivation,Method =“{null}”,Result =“{Not yet computed}” – user6824563
@ user6824563,你必须等待'PostAsync'返回的'Task' – Nkosi