dongpin3794 2016-11-11 01:48
浏览 63
已采纳

如何在网页上选择多种表单之一来使用HttpClient POST数据?

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 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. 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

  • 写回答

1条回答 默认 最新

  • duanrui3480 2016-11-11 02:35
    关注

    Firstly you don't post to a form, a form posts to a URL. So you have most of the details right already. The issues you have are:

    1. You named the input incorrectly. Instead of value it should be catalog to match the name attribute of the select input.
    2. 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" }
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面