使用PHP POST的VB.NET WebRequest

时间:2014-10-10 03:34:45

标签: php vb.net post webrequest

您好我在我的服务器中将post变量发送到PHP文档中时遇到了问题。我尝试使用GET,它工作正常。但我注意到POST VARIABLE没有收到我发送的内容。这是我的代码,谢谢高级:

VB.NET WINFORM CODE

enter code here


        Dim Username = TxtUser.Text
        Dim PostData = "user_name=" & Username
        Dim request As WebRequest = WebRequest.Create("http://website.com/test.php")

        request.Method = "POST"
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(PostData)
        request.ContentType = "application/x-form-urlencoded"
        request.ContentLength = byteArray.Length
        Dim dataStream As Stream = request.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        Dim response As WebResponse = request.GetResponse()
        dataStream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        reader.Close()
        dataStream.Close()
        response.Close()
        MsgBox(responseFromServer)

PHP代码

<?php
//I tried this   $user_name= 'SOMETHING';   and works fine.
$user_name= $_POST['user_name']; 
?>

1 个答案:

答案 0 :(得分:1)

更改ContentType应该可以解决问题。

request.ContentType = "application/x-www-form-urlencoded"