views:

136

answers:

1

I have various php files which data is Posted to (like the password when the user signs in) How can I can I post to these php from vb.net (a desktop application that is Windows Forms, this is not about ASP.net)

thanks

A: 

You can use the WebClient class. You need to set the Content-Type header to application/x-www-form-urlencoded and then use the UploadData method. The documentation of that method contains a simple example, which basically boils down to this:

Dim myWebClient As New WebClient()
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

Dim responseArray = myWebClient.UploadData("http://...", "POST", Encoding.ASCII.GetBytes(postData))
Dim response = Encoding.ASCII.GetString(responseArray)

The Wikipedia page of HTTP POST contains information of how the POST data must be encoded:

Each key-value pair is separated by an '&' character, and each key is separated from its value by an '=' character. Keys and values are both escaped by replacing spaces with the '+' character and then using URL encoding on all other characters.

So, your postData variable can be filled like this (assuming that the fields you want to post are called Username and Password):

Dim postData = String.Format("Username={0}&Password={1}", _
  HttpUtility.UrlEncode(username), _
  HttpUtility.UrlEncode(password))
Heinzi