1

如何从 vb.net 发送可以$HTTP_ROW_POST在 PHP 中使用的 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

但我无法在 PHP 文件中使用$HTTP_ROW_POST.

4

1 回答 1

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

于 2013-04-24T08:01:54.023 回答