使用Visual Basic POST到PHP?

时间:2012-07-17 16:20:59

标签: php vb.net post

我正在为我编写的一些PHP代码编写GUI。我收到有关POST不成功的错误。

我从一个在线示例中获取了PHP函数代码,当它编译并运行时,它对我不起作用,因为我收到有关帖子不能正常工作的错误。

Imports System.Text
Imports System.IO
Imports System.Net

Public Class Form1

    Private Sub btnIP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIP.Click
        Dim ip, firstBit, secondBit, completeCall As String

        firstBit = "apikey=eb4a84&method=risk&ip="
        secondBit = "&categories=&options=url_detail"
        ip = txtIP.Text

        completeCall = firstBit + ip + secondBit

        txtCall.Text = completeCall

        Dim url, method As String

        method = "POST"
        url = "http://localhost/myfiles/WorkingVersionVQuickLookXML.php"




        txtCall.Text = PHP(url, method, ip)


    End Sub

    Public Function PHP(ByVal url As String, ByVal method As String, ByVal data As String)
        Try

            Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
            request.Method = method
            Dim postData = data
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
            request.ContentType = "application/x-www-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()
            Return (responseFromServer)
        Catch ex As Exception
            Dim error1 As String = ErrorToString()
            If error1 = "Invalid URI: The format of the URI could not be determined." Then
                MsgBox("ERROR! Must have HTTP:// before the URL.")
            Else
                MsgBox(error1)
            End If
            Return ("ERROR")
        End Try
    End Function
End Class

我使用html页面运行相同的确切文件发布到php,它运行正常。

这是错误:

这是我的php应该将ip变量发布到:

require("IPQFunctionworkingversionVXML.php");
$ipaddress = $_POST["ipaddress"];

$results = array();

$results = getScore($ipaddress);

echo $results;

一旦正确拥有ipaddress字段,其他字段应该可以正常工作。

我的想法是帖子没有发布到我的php文件中的“ipaddress”字段。

如果有人能在代码中发现任何内容?或者有一个替代解决方案发布到PHP请告诉我!

2 个答案:

答案 0 :(得分:0)

一个主要问题是您在下一行中设置表单数据,但从未将该数据传递给PHP调用例程。

电话会看起来像这样:

txtCall.Text = PHP(url, method, completeCall)

答案 1 :(得分:0)

替换此

$_POST["ipaddress"];

用这个

$_POST["ip"];