I have various PHP files which data is Posted to (like the password when the user signs in) How can I post to these PHP from vb.net (a desktop application that is Windows Forms, this is not about ASP.net)
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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))