Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - POST to webpage in vb.net (win forms, desktop, not ASP.net)

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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("https://...", "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))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...