我刚开始使用htmlagilitypack,到目前为止我很喜欢它。
我尝试选择单选按钮并使用htmlagility提交表单。
以下是该网站的结构:
<form class="picker" action="link.html" method="POST">
<ul class="selection-list">
<li>
<label>
<span class="left-side">
<input name="id" value="t1" type="radio">
</span>
<span class="right-side">
Test1
</span>
</label>
</li>
<li>
<label>
<span class="left-side">
<input name="id" value="t2" type="radio">
</span>
<span class="right-side">
Test2
</span>
</label>
</li>
<li>
<label>
<span class="left-side">
<input name="id" value="t3" type="radio">
</span>
<span class="right-side">
Test3
</span>
</label>
</li>
</ul>
</form>
我可以获得表格。这是代码:
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load(urlAddress);
// Get the form
var form = doc.DocumentNode.SelectSingleNode("//form[@class='picker']");
如何选择
由于
答案 0 :(得分:1)
var uri = // get uri from form;
var formVariables = new List<KeyValuePair<string, string>>();
// Populate your variables here; HtmlAgilityPack is useful for propagating existing form values
formVariables.Add(new KeyValuePair<string,string>("id","t2"));
var formContent = new FormUrlEncodedContent(formVariables);
using (var message = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = uri, Content = formContent })
{
// use HttpClient to send the message
using (var postResponse = await client.SendAsync(message))
{
if (postResponse.IsSuccessStatusCode)
{
var stringContent = await response.Content.ReadAsStringAsync();
// Do something with string content
}
}
}