【问题标题】:post data through httpWebRequest通过 httpWebRequest 发布数据
【发布时间】:2017-11-28 12:49:22
【问题描述】:

我需要使用“发布”一些数据到外部网站 HttpWebRequest 来自我的应用程序(桌面)的对象并获得响应 通过HttpWebResponse 对象返回我的应用程序。 但是我发布数据的网页有动态名称的文本框。

如何获取这些文本框的名称并在HttpWebResquest 中发布数据?

例如,当我加载页面时,文本框名称是这样的 U2FsdGVkX183MTQyNzE0MrhLOmUpqd3eL60xF19RmCwLlSiG5nC1H6wvtBDhjI3uM1krX_B8Fwc,但是当我刷新页面名称时,会更改为 U2FsdGVkX182MjMwNjIzMPAtotst_q9PP9TETomXB453Mq3M3ZY5HQt70ZeyxbRb118Y8GQbgP8

感谢您的任何建议。

【问题讨论】:

    标签: c# httpwebrequest


    【解决方案1】:
    var request = WebRequest.Create("http://foo");
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    using (var writer = new StreamWriter(request.GetRequestStream()))
    {
        writer.Write("field=value");
    }
    

    【讨论】:

    • 但我事先不知道字段名称。就是这个问题???字段名称不是硬编码的,它们会在页面加载或刷新时发生变化。
    • 为此打开另一个问题,因为它在这个问题中完全是题外话(并不意味着它在一般情况下是题外话)
    【解决方案2】:

    您可以通过 XPath 设置这些名称,例如并像这样使用它们:

    byte[]  data = new ASCIIEncoding().GetBytes("textBoxName1=blabla");
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost/myservlet");
    httpWebRequest.Method = "POST";
    httpWebRequest.ContentType = "application/x-www-form-urlencoded";
    httpWebRequest.ContentLength = data.Length;
    Stream myStream = httpWebRequest.GetRequestStream();
    myStream.Write(data,0,data.Length);
    myStream.Close();
    

    【讨论】:

      【解决方案3】:

      看起来您必须使用 HttpWebRequest 获取页面并解析相应 HttpWebResponse 的内容以找出文本框的名称。然后使用另一个 HttpWebRequest 将值提交到页面。

      所以基本上,你需要做的是:

      1. 向带有文本框的页面所在的 URL 发出带有 GET 方法的 HttpWebRequest
      2. 获取HttpWebResponse的响应流
      3. 解析响应流中包含的页面并获取文本框的名称。为此,您可以使用HTML Agility Pack
      4. 使用 POST 方法发出 HttpWebRequest,内容类型设置为“application/x-www-form-urlencoded”,键值对作为内容。

      【讨论】:

        【解决方案4】:

        问题的第一部分: 也许 HTML 树是稳定的。然后,您可以使用 XPath 找到您感兴趣的文本框。 使用 XmlReader、XDocument 和 Linq 来完成。

        【讨论】:

          【解决方案5】:

          我使用此功能发布数据。但是您传递的网址必须格式化为例如

          http://example.com/login.php?userid=myid&password=somepassword

          Private Function GetHtmlFromUrl(ByVal url As String) As String
          
                  If url.ToString() = vbNullString Then
                      Throw New ArgumentNullException("url", "Parameter is null or empty")
                  End If
                  Dim html As String = vbNullString
                  Dim request As HttpWebRequest = WebRequest.Create(url)
                  request.ContentType = "Content-Type: application/x-www-form-urlencoded"
                  request.Method = "POST"
          
          
                  Try
                      Dim response As HttpWebResponse = request.GetResponse()
                      Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
                      html = Trim$(reader.ReadToEnd)
                      GetHtmlFromUrl = html
                  Catch ex As WebException
                      GetHtmlFromUrl = ex.Message
                  End Try
          
              End Function
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2013-01-20
            • 1970-01-01
            • 2012-04-30
            • 1970-01-01
            • 1970-01-01
            • 2012-08-01
            • 1970-01-01
            相关资源
            最近更新 更多