vb.net 使用 HttpWebRequest 发送 POST 值并获得响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14110797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send POST values and get response using HttpWebRequest
提问by Alex
I have this php file:
我有这个 php 文件:
<?php
if (isset($_POST['message']))
{
$msg = $_POST['message'];
if($msg == "age")
echo "I am 18";
else if($msg == "name")
echo "my name is Jonathan";
else
echo "I do not know";
}
?>
I want to make a HttpWebRequest in VB.NET such that if I send the value "age" from a TextBox:
To get this following msgbox:
I tried something like this :
我想在 VB.NET 中创建一个 HttpWebRequest,这样如果我从 TextBox 发送值“age”:
要获得以下 msgbox:
我尝试了这样的事情:
Public Sub SendPostData(ByVal site As String, ByVal message As String)
Dim request As WebRequest
request = WebRequest.Create(site)
Dim response As WebResponse
Dim postData As String = "message=" + message
Dim data As Byte() = Encoding.UTF8.GetBytes(postData)
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = data.Length
Dim stream As Stream = request.GetRequestStream()
stream.Write(data, 0, data.Length)
stream.Close()
response = request.GetResponse()
Dim sr As New StreamReader(response.GetResponseStream())
MsgBox(sr.ReadToEnd)
End Sub
and I got this :
我得到了这个:
Any idea why i'm getting that instead of one of the 3 possible message in the php file? ("I am 18", "my name is Jonathan", or "I do not know") ?
Please note that I tested the php normally from a html form and it worked, so I wonder why it's not working from the program.
知道为什么我得到它而不是 php 文件中的 3 个可能的消息之一吗?(“我 18 岁”、“我叫乔纳森”或“我不知道”)?请注意,我通常从 html 表单测试了 php 并且它有效,所以我想知道为什么它不能从程序中运行。
采纳答案by Zeddy
The reason you're getting WHAT you're getting is because you have stated that the FORM content is of a "x-www-form-urlencoded" format AND you have used the POST method and one supposes that whatever is in the cookie (cookie data) is the information that you passed through but it has been encoded by the server before sending.
您得到 WHAT 的原因是因为您已经声明 FORM 内容是“x-www-form-urlencoded”格式,并且您使用了 POST 方法,并且假设 cookie 中的内容( cookie 数据)是您传递的信息,但在发送之前已由服务器编码。
I'm not sure how but you should be able to un-encode it too. Alternatively you could use GET as opposed to POST and also if there is only going to be TEXT and no SPECIAL symbols or characters which ACTUALLY require encoding, then maybe you could use a different CONTENT-TYPE that CORRECTLY identifies the type of content that you are sending...
我不确定如何,但您也应该能够取消编码。或者,您可以使用 GET 而不是 POST,并且如果只有 TEXT 而没有实际需要编码的特殊符号或字符,那么也许您可以使用不同的 CONTENT-TYPE 来正确标识您的内容类型发送...
The following page at W3.org is good reading and explains the differences between the various bits and pieces and how they interact with one another and with the server too.
W3.org 上的以下页面很好阅读,并解释了各个部分之间的差异以及它们如何相互交互以及如何与服务器交互。
Forms ( http://www.w3.org/TR/html401/interact/forms.html)
表单( http://www.w3.org/TR/html401/interact/forms.html)
How Forms are processed( http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3)
如何处理表单( http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3)
回答by sav
I have used WebClient to solve a similar problem where i needed to use parameters in the HTTP POST. For your case, a simplified solution like this could help if you play well around with your code; Assuming you have a textbox to capture the age, you could pass it as a variable to the url using this code;
我使用 WebClient 解决了一个类似的问题,我需要在 HTTP POST 中使用参数。对于您的情况,如果您能很好地处理代码,像这样的简化解决方案可能会有所帮助;假设您有一个文本框来捕获年龄,您可以使用此代码将其作为变量传递给 url;
Using client As New WebClient
Dim postdata = client.UploadString(url, "age")
MsgBox(postdata)
End Using
回答by Ananth
I am experience .net software and knows php also
我体验过 .net 软件,也知道 php
Never user httpwebrequest and httpwebresponse, 'coz both are out dated
永远不要使用 httpwebrequest 和 httpwebresponse,因为它们都过时了
here is the link check properly http://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx
这是链接检查正确 http://msdn.microsoft.com/en-us/library/debx8sh9%28v=vs.110%29.aspx
It uses WebRequest
and WebResponse
, add references System.Net
and System.Web
in your code
它使用WebRequest
and WebResponse
,在你的代码中添加引用System.Net
和System.Web
And target framework must be 4.0, 4.5 or higher but not to select name containing Client Profile
并且目标框架必须是 4.0、4.5 或更高版本但不要选择包含 Client Profile 的名称
回答by Paul Ishak
Have you considered doing something like this?
你有没有考虑过做这样的事情?
Option Strict On
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Action As String = "http://www.mywebsite.com/myaction.php"
PostMessage("submit", "Paul", Action)
End Sub
Sub PostMessage(ByVal Type As String, ByVal Value As String, ByVal Action As String)
Dim TmpFile As String = My.Computer.FileSystem.SpecialDirectories.Temp & "\post.html"
Using WB As New WebBrowser
Dim HTML As String = _
"<html><body>" &
"<form method=""post"" action=""" & Action & """>" &
"<input type=""" & Type & """ value=""" & Value & """ />" &
"</form>" &
"</body></html>"
My.Computer.FileSystem.WriteAllText(TmpFile, HTML, False)
WB.Navigate(TmpFile)
Do : Application.DoEvents()
Loop Until WB.ReadyState = WebBrowserReadyState.Complete
WB.Document.GetElementById(Type).InvokeMember(Type)
Do : Application.DoEvents()
Loop Until WB.ReadyState = WebBrowserReadyState.Complete
End Using
My.Computer.FileSystem.DeleteFile(TmpFile)
End Sub
End Class
回答by Ferdinand Botigan
just change this line:
只需更改这一行:
Dim postData As String = "message=" + message
Dim postData As String = "message=" + message
to:
到:
Dim postData As String = "message=" & message
Dim postData As String = "message=" & message
use "&" instead of "+"
使用“&”代替“+”
Regards...
问候...