简体   繁体   English

通过httpWebRequest发布数据

[英]post data through httpWebRequest

I need to "Post" some data to an external website using HttpWebRequest object from my application(desktop) and get a response back into my application through HttpWebResponse object. 我需要“邮报”的一些数据到外部网站使用HttpWebRequest对象从我的应用程序(桌面)和打通一个响应返回到我的应用程序HttpWebResponse对象。 But the webpage on which im posting data have textboxes which have dynamic names. 但是即时消息发布数据所在的网页上的文本框具有动态名称。

How can I get the name of those textboxes and post data in HttpWebResquest ? 如何获得这些文本框的名称并在HttpWebResquest发布数据?

For example when I load the page the textbox name is like this U2FsdGVkX183MTQyNzE0MrhLOmUpqd3eL60xF19RmCwLlSiG5nC1H6wvtBDhjI3uM1krX_B8Fwc but when I refresh the page name change to this U2FsdGVkX182MjMwNjIzMPAtotst_q9PP9TETomXB453Mq3M3ZY5HQt70ZeyxbRb118Y8GQbgP8 . 例如,当我加载页面的文本框的名字就是这样U2FsdGVkX183MTQyNzE0MrhLOmUpqd3eL60xF19RmCwLlSiG5nC1H6wvtBDhjI3uM1krX_B8Fwc但是当我刷新页面名称更改这个U2FsdGVkX182MjMwNjIzMPAtotst_q9PP9TETomXB453Mq3M3ZY5HQt70ZeyxbRb118Y8GQbgP8

Thanks for any suggestions. 感谢您的任何建议。

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");
}

You could et those names by XPath eg and user them like: 您可以使用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();

It looks like you will have to get the page with a HttpWebRequest and parse the content of the corresponding HttpWebResponse to find out the names of text boxes. 看来您将不得不使用HttpWebRequest获取页面并解析相应HttpWebResponse的内容以找出文本框的名称。 Then you submit the values to the page by using another HttpWebRequest. 然后,您使用另一个HttpWebRequest将值提交到页面。

So basically, what you need to do is the following: 因此,基本上,您需要执行以下操作:

  1. Issue a HttpWebRequest with GET method to the URL where the page with text boxes is located 将带有GET方法的HttpWebRequest发出到带有文本框的页面所在的URL
  2. Get the response stream of the HttpWebResponse 获取HttpWebResponse的响应流
  3. Parse the page contained in the response stream and get the names of text boxes. 解析响应流中包含的页面并获取文本框的名称。 You can use HTML Agility Pack for this purpose. 您可以为此目的使用HTML Agility Pack
  4. Issue a HttpWebRequest with POST method, with content type set to "application/x-www-form-urlencoded" and key-value pairs as the content. 使用POST方法发出HttpWebRequest,其内容类型设置为“ application / x-www-form-urlencoded”,并将键值对作为内容。

First part of your problem: Maybe the HTML tree is stable. 问题的第一部分:也许HTML树是稳定的。 Then you can find your way to the textbox of your interrest with XPath. 然后,您可以找到通向XPath的文本框的方法。 Use XmlReader, XDocument and Linq to go throught it. 使用XmlReader,XDocument和Linq进行浏览。

I use this function to post data. 我使用此功能发布数据。 But the url you pass has to be formatted as such for example 但是您传递的网址必须经过格式化,例如

http://example.com/login.php?userid=myid&password=somepassword 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM