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" }
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥15 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码
  • ¥15 stm32标准库的PID角度环