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