将xml从vb.net发送到php文件

时间:2013-04-24 07:28:59

标签: php xml vb.net http-post

如何从vb.net发送可以使用PHP中的$HTTP_ROW_POST捕获的xml文件?

我的脚本是:

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

但我无法使用$HTTP_ROW_POST在PHP文件中捕获它。

1 个答案:

答案 0 :(得分:4)

不要将内容类型设置为application/x-www-form-urlencoded,因为它会暗示在请求正文中发送key=value对。将其设置为application/xml,因为这是您要发送的内容。

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

Module Module1

    Sub Main()
        Dim resp As String = PHP("http://localhost/test.php", "POST", "<xml>test</xml")
        System.Console.WriteLine(resp)
    End Sub

    Public Function PHP(ByVal url As String, ByVal method As String, ByVal data As String)
        Try
            Dim byteArray As Byte() = Encoding.UTF8.GetBytes(data)
            Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)
            request.Method = method
            request.ContentType = "application/xml"
            request.ContentLength = byteArray.Length
            request.GetRequestStream().Write(byteArray, 0, byteArray.Length)

            Dim response As WebResponse = request.GetResponse()
            Dim responseFromServer As String = (New StreamReader(response.GetResponseStream())).ReadToEnd()

            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 Module

适用于php服务器脚本

<?php
$c = file_get_contents('php://input');
echo 'got: ', $c;

另见:http://docs.php.net/wrappers.php.php

相关问题