[英]How can you choose one of multiple forms on a webpage to POST data to using HttpClient?
I'm creating a method that will go to our university catalog website and return the degree plan for the selected school year. 我正在创建一种方法,该方法将转到我们的大学目录网站,并返回所选学年的学位计划。 This is my code to send a POST request to the dropdown box to select the school year.
这是我的代码,用于将POST请求发送到下拉框以选择学年。
// Post to catalog website to choose catalog year
HttpClient client = new HttpClient();
string catalogURL = "http://catalog.cpp.edu/index.php";
//Example choose 2015/16 catalog (value = "8")
//input parameter for drop down box in catalog
var parameter = new Dictionary<string,string>{ { "value", "8" } };
var encodedContent = new FormUrlEncodedContent(parameter);
var response = await client.PostAsync(catalogURL, encodedContent).ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.OK)
{
//get content:
var responseContent = await response.Content.ReadAsStringAsync ().ConfigureAwait (false);
//return html doc for catalog year
return responseContent;
}
However, I don't believe it's working. 但是,我认为它没有用。 It's returning the html doc for the default page, the 2016/17 catalog, not the 2015/16 catalog.
它会返回默认页面,2016/17目录而不是2015/16目录的html文档。 I think it may not be posting to the correct form, as there is another one before the drop down box: a search bar.
我认为它可能无法发布到正确的表单,因为在下拉框之前还有另一个表单:搜索栏。 What am I doing wrong?
我究竟做错了什么?
This is what the source code looks like for the drop down box that I want to access: 这是我要访问的下拉框的源代码:
<form name="select_catalog" method="post" action="/index.php" style="margin: 0px">
<table border="0" cellspacing="0" cellpadding="0">
<tbody>
<tr valign="top">
<td>
<select tabindex="201" name="catalog" title="select a catalog" id="select_catalog">
<option value="10" selected> 2016-2017 University Catalog</option>
<option value="8"> 2015-2016 University Catalog</option>
<option value="9"> 2014-2015 University Catalog</option>
<option value="5"> 2013-2014 University Catalog</option>
<option value="4"> 2012-2013 University Catalog</option>
</select>
</td>
*Note: the url for the page remains the same regardless of the year selected, but the linke to the curriculum plans are different. *注意:无论选择的年份如何,页面的网址都保持不变,但是与课程计划的链接不同。
Sorry if this is a dumb question, but I don't have much experience with C# or .NET 抱歉,这是一个愚蠢的问题,但是我对C#或.NET的经验不足
Firstly you don't post to a form, a form posts to a URL. 首先,您不发布到表单,而是表单发布到URL。 So you have most of the details right already.
因此,您已经掌握了大多数细节。 The issues you have are:
您遇到的问题是:
value
it should be catalog
to match the name
attribute of the select
input. catalog
,而不是value
,以匹配select
输入的name
属性。 You are also missing a form field: 您还缺少一个表单字段:
<input type="submit" title="Select a Catalog" name="sel_cat_submit" value="GO" ...>
So you need to simply change your parameters to this: 因此,您只需将参数更改为此:
var parameter = new Dictionary<string, string>
{
{ "catalog", "8" },
{ "sel_cat_submit", "GO" }
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.