Welcome to OStack Knowledge Sharing 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...