【问题标题】:Unable to send POST data of a form无法发送表单的 POST 数据
【发布时间】:2017-03-02 13:31:26
【问题描述】:

我在使用 Web 发布表单时遇到了问题。 我已经下载了页面,我推断了两个必需的值(form_build_idform_token),但是一旦发送 POST,服务器就不会在 POST 中收到任何内容。

排除的错误:

  • 链接错误(可以下载页面)。
  • 外推数据不正确(已验证)。
  • myParameters 字符串错误(已验证)。
  • 我已经手动测试了表单,它工作正常。

有什么想法吗?在那里我猛击了两天!

using (WebClientEx wc = new WebClientEx())
        {
            wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            string HTMLPage = wc.DownloadString(CREAT_TICKET_URL);

            string form_build_id    = SearchValue(HTMLPage, "<input type=\"hidden\" name=\"form_build_id\"", "value=\"", "\"  />");
            string form_token       = SearchValue(HTMLPage, "<input type=\"hidden\" name=\"form_token\"", "value=\"", "\"  />");

            string myParameters = "macchina=" + cmacExtID + "&utente=" + custExtID + "&oggetto=" + Title + "&body=" + Note + "&op=Conferma&form_build_id=" + form_build_id + "&form_token=" + form_token + "&form_id=app_form_new_ticket";

            string HtmlResult = wc.UploadString(CREAT_TICKET_URL, myParameters);
        }

注意:WebClientEx 类继承 WebClient。我将这种方法用于登录和工作等其他形式。

最后一个问题是:如果这种方法是错误的,那么执行“下载页面、从 HTML 中提取值、发送表单”这一系列操作的最佳方法是什么?

【问题讨论】:

    标签: c# http-post webclient


    【解决方案1】:

    问题是标题! 每次调用都应该设置标题,而我认为只设置第一次就足够了。

    using (WebClientEx wc = new WebClientEx())
        {
            wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            string HTMLPage = wc.DownloadString(CREAT_TICKET_URL);
    
            string form_build_id    = SearchValue(HTMLPage, "<input type=\"hidden\" name=\"form_build_id\"", "value=\"", "\"  />");
            string form_token       = SearchValue(HTMLPage, "<input type=\"hidden\" name=\"form_token\"", "value=\"", "\"  />");
    
            string myParameters = "macchina=" + cmacExtID + "&utente=" + custExtID + "&oggetto=" + Title + "&body=" + Note + "&op=Conferma&form_build_id=" + form_build_id + "&form_token=" + form_token + "&form_id=app_form_new_ticket";
    
            wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            string HtmlResult = wc.UploadString(CREAT_TICKET_URL, myParameters);
        }
    

    【讨论】:

      猜你喜欢
      • 2011-04-16
      • 1970-01-01
      • 1970-01-01
      • 2023-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-09
      • 2020-06-05
      相关资源
      最近更新 更多