duanleiliu7373 2013-04-24 07:28
浏览 61

将xml从vb.net发送到php文件[重复]

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?

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.

</div>
  • 写回答

1条回答 默认 最新

  • dsjbest2015 2013-04-24 08:01
    关注

    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.

    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
    $c = file_get_contents('php://input');
    echo 'got: ', $c;
    

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

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料