将xml从vb.net发送到php文件[副本]

时间:2021-03-13 14:03:35

This question already has an answer here:

这个问题已经有了答案:

How to send xml file from vb.net that can be caught using $HTTP_ROW_POST in PHP?

如何从PHP中使用$HTTP_ROW_POST从vb.net发送xml文件?

My script is:

我的脚本:

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

But I can not capture it in the PHP file using $HTTP_ROW_POST.

但是我不能使用$HTTP_ROW_POST在PHP文件中捕获它。

1 个解决方案

#1


4  

Don't set the content-type to application/x-www-form-urlencoded as it would imply key=value pairs sent in the request body. Set it to application/xml because that is what you're sending.

不要将内容类型设置为application/x-www-form- urlencodes,因为这意味着在请求主体中发送的键=值对。将它设置为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

works well with the php server script

使用php服务器脚本工作得很好

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

see also: http://docs.php.net/wrappers.php.php

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

#1


4  

Don't set the content-type to application/x-www-form-urlencoded as it would imply key=value pairs sent in the request body. Set it to application/xml because that is what you're sending.

不要将内容类型设置为application/x-www-form- urlencodes,因为这意味着在请求主体中发送的键=值对。将它设置为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

works well with the php server script

使用php服务器脚本工作得很好

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

see also: http://docs.php.net/wrappers.php.php

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