使用 HttpWebRequest 发送 POST 值并获得响应 [英] Send POST values and get response using HttpWebRequest

查看:25
本文介绍了使用 HttpWebRequest 发送 POST 值并获得响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 php 文件:

I have this php file:

<?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";

}

?>

我想在 VB.NET 中创建一个 HttpWebRequest,这样如果我从 TextBox 发送值age":
要获得以下消息框:


我试过这样的事情:

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 :

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


我得到了这个:


知道为什么我得到它而不是 php 文件中的 3 个可能的消息之一吗?(我 18 岁"、我叫乔纳森"或我不知道")?请注意,我通常从 html 表单测试了 php 并且它有效,所以我想知道为什么它在程序中不起作用.


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.

推荐答案

你得到 WHAT 你得到的是因为你已经声明 FORM 内容是x-www-form-urlencoded"格式并且您使用了 POST 方法,并且假设 cookie(cookie 数据)中的任何内容都是您传递的信息,但在发送之前已由服务器对其进行编码.

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.

我不确定如何,但您也应该能够对其进行取消编码.或者,您可以使用 GET 而不是 POST,并且如果只有 TEXT 而没有实际需要编码的特殊符号或字符,那么也许您可以使用不同的 CONTENT-TYPE 来正确标识您的内容类型发送...

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

W3.org 上的以下页面很好读,它解释了各个部分之间的差异以及它们如何相互交互以及如何与服务器交互.

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.

表格(http://www.w3.org/TR/html401/interact/forms.html)

表单的处理方式(http://www.w3.org/TR/html401/interact/forms.html#h-17.13.3)

这篇关于使用 HttpWebRequest 发送 POST 值并获得响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆