doujiao4705
doujiao4705
2014-10-10 03:34

使用PHP POST的VB.NET WebRequest

Hello I am having trouble sending the post variable to received in the PHP document in my server. I tried it with the GET and it works fine. But what I notice is the POST VARIABLE doesn't receive the content I am sending. This is my code and thank in advanced:

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 CODE

<?php
//I tried this   $user_name= 'SOMETHING';   and works fine.
$user_name= $_POST['user_name']; 
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • douyu7879 douyu7879 6年前

    Changing the ContentType should do the trick.

    request.ContentType = "application/x-www-form-urlencoded"
    
    点赞 评论 复制链接分享

相关推荐