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



此问题已经存在 这里有一个答案:</ p>





  • 从VB.NET应用程序发送文件到PHP脚本



    5条答案
    </ span>

    </ li>

    </ ul>

    </ div>

    如何发送 来自vb.net的xml文件可以使用PHP中的 $ HTTP_ROW_POST </ code>来捕获吗?</ p>

    我的脚本是:</ p>

     < 代码>公共函数PHP(ByVal url As String,ByVal方法As String,ByVal data As String)
    
    

    尝试

    Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(url)\ n request.Method = method
    Dim postData = data
    Dim byteArray As Byte()= Encoding.UTF8.GetBytes(postData)
    request.ContentType =“a pplication / 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()
    返回(responseFromServer)
    Catch ex As Exception
    Dim error1 As String = ErrorToString()
    如果error1 =“无效的URI:格式为 无法确定URI。“ 然后
    MsgBox(“错误!必须在URL之前有HTTP://”。)
    否则
    MsgBox(error1)
    结束如果
    返回(“错误”)
    结束尝试
    结束功能
    </ code> </ pre>

    但我无法使用 $ HTTP_ROW_POST </ code>在PHP文件中捕获它。</ p>
    </ div>

展开原文

原文

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>

duanjiao6731
duanjiao6731 还相关:当它到达php的$_POST时,通过vb.net中的HttpWebRequest发送的HTML数据是空的
7 年多之前 回复

1个回答



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

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

模块Module1

Sub Main()
Dim resp As String = PHP(“http://localhost/test.php”,“POST”,“&lt; xml&gt; test&lt; / xml”)\ n System.Console.WriteLine(resp)
End Sub

公共函数PHP(ByVal url As String,ByVal方法As String,ByVal data As String)
尝试
Dim byteArray As Byte()=编码。 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()
返回(responseFromServer)
Catch ex As Exception
Dim error1 As String = ErrorToString()
如果error1 =“无效的URI:无法确定URI的格式。” 然后
MsgBox(“错误!必须在URL之前有HTTP://”。)
否则
MsgBox(error1)
结束如果
返回(“错误”)
结束尝试
结束功能\ n
End Module
</ code> </ pre>

适用于php服务器脚本</ p>

 &lt;?php 
$ c = file_get_contents('php:// input');
echo'got:',$ c;
</ code> </ pre>

另见: http://docs.php.net/wrappers.php.php </ p>
</ div>

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐