14

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. But the webpage on which i m posting data have textboxes which have dynamic names.

How can I get the name of those textboxes and post data in 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.

Thanks for any suggestions.

0

5 Answers 5

31
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"); } 
Sign up to request clarification or add additional context in comments.

2 Comments

but i dont know the field name in advance. thats the issue???? the field names are not hardcoded they are changing whenever the page load or refreshes.
Open another question for that, as it's completely offtopic in THIS question (doesn't mean it is off-topic on SO in general)
10

You could et those names by XPath e.g. and user them like:

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(); 

Comments

2

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. Then you submit the values to the page by using another 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
  2. Get the response stream of the 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.
  4. Issue a HttpWebRequest with POST method, with content type set to "application/x-www-form-urlencoded" and key-value pairs as the content.

Comments

0

First part of your problem: Maybe the HTML tree is stable. Then you can find your way to the textbox of your interrest with XPath. Use XmlReader, XDocument and Linq to go throught it.

Comments

0

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

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 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.