1

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!

GK1667
  • 1,362
  • 3
  • 14
  • 22
  • Generally speaking, if you mention you're getting an error, you should include the exact error message, and where this error message is coming from - is it a VB error? A php error? An apache error? browser error? – Marc B Jul 17 '12 at 16:41
  • @MarcB Error has been updated. Its coming from the php. – GK1667 Jul 17 '12 at 17:30
  • undefined index means you're accessing an item in an array that doesn't exist, and the rest tell you what the undefined property is. You need to show the PHP code, not the VB code. – Marc B Jul 17 '12 at 17:31
  • @MarcB the php code is not the problem. As I said, it works perfectly when i run it using the html page to post the data. the problem lies in the VB posting – GK1667 Jul 17 '12 at 17:35
  • The two things are still related. You could be making an incorrect assumption in your VB code. – Evert Jul 17 '12 at 17:37
  • If it was running perfectly, you wouldn't get undefined index warnings, which means you're not checking for the existence of data, and your vb is most likely not sending that data. – Marc B Jul 17 '12 at 17:53
  • @mMarcB I've uploaded the php... I still do not see what's wrong. Perhaps you might? – GK1667 Jul 17 '12 at 17:56
  • X-Ref: [Send xml from vb.net to php file](http://stackoverflow.com/q/16185958/367456?rq=1) – hakre Apr 25 '13 at 10:15

2 Answers2

0

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)
Frazell Thomas
  • 6,031
  • 1
  • 20
  • 21
  • I appreciate your answer. It makes sense, but the context is different. That was something I need displayed, but I'm not actually connecting to that site. The site where its linked is correct, and then the ip address is sent to be POST – GK1667 Jul 17 '12 at 17:53
0

Replace this

$_POST["ipaddress"];

with this

$_POST["ip"];
gunakkoc
  • 1,069
  • 11
  • 30