在vb.net中发布到网页(Win窗体,桌面,而不是ASP.net) [英] POST to webpage in vb.net (win forms, desktop, not ASP.net)
问题描述
我有各种PHP文件,这些数据被发布到其中(例如用户登录时的密码)如何从vb.net(Windows窗体的桌面应用程序,这与ASP.net无关)发布到这些PHP. )
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)
推荐答案
您可以使用WebClient
类.您需要将Content-Type
标头设置为application/x-www-form-urlencoded
,然后使用UploadData
方法. 该方法的文档包含一个简单的示例,基本上可以归结为对此:
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)
HTTP POST的维基百科页面包含有关必须如何处理POST数据的信息被编码:
The Wikipedia page of HTTP POST contains information of how the POST data must be encoded:
每个键值对之间用&"分隔字符,并且每个键都由一个'='字符与其值分隔.通过用'+'字符替换空格,然后对所有其他字符使用URL编码,可以对键和值进行转义.
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.
因此,您的postData
变量可以这样填充(假设要发布的字段称为用户名和密码):
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))
这篇关于在vb.net中发布到网页(Win窗体,桌面,而不是ASP.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!