将xml从vb.net发送到php文件 [英] Send xml from vb.net to php file

查看:115
本文介绍了将xml从vb.net发送到php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的脚本是:

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

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

推荐答案

不要设置内容-type到 application / x-www-form-urlencoded 因为它意味着在请求中发送的 key = value 对身体。将其设置为 application / xml ,因为这是您要发送的内容。

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

与php服务器脚本配合良好

works well with the php server script

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

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

这篇关于将xml从vb.net发送到php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆