POST to PHP using VisualBasic?

牧云@^-^@ 提交于 2020-01-06 03:37:05

问题


I am writing GUI for some PHP code I've written. I'm getting errors about the POST being unsuccessful.

I took the PHP function code from an online example, and while it compiles and runs, it does not work for me in that I get errors about the post not working.

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

I've run the same exact file using a html page to post to the php and it works perfectly.

Here's the error:

Here is my php that ip variable should be posted to:

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

$results = array();

$results = getScore($ipaddress);

echo $results;

Once it has the ipaddress field correctly the other fields should work.

My thoughts are that the post is not posting to the "ipaddress" field on my php file.

If anyone can spot anything in the code? or has an alternative solution for posting to the php please let me know!


回答1:


A major problem looks to be that you're setting your form data in the following line, but never passing that data to your PHP calling routine.

The call should look something like this:

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



回答2:


Replace this

$_POST["ipaddress"];

with this

$_POST["ip"];


来源:https://stackoverflow.com/questions/11526729/post-to-php-using-visualbasic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!