如何在VB.net中将xml元素发布到api [英] How do you post a xml element to api in VB.net

查看:103
本文介绍了如何在VB.net中将xml元素发布到api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我一直在使用Google搜索,似乎无法找到有关如何执行此操作的简单说明,因此,如果有人可以将我指向一个可以找到一些有关如何执行此操作的信息的网站,那将是非常棒的. br/>
我已对问题进行了编辑,以使其更易于理解.

我使用http api时正在通过Internet发送短信,在此我一个接一个地发送短信,这在发送数千个短信时会有点慢.所以他们告诉我,使用xml方法会更好,我假设我将使用下面的格式创建xml文件,并且能够以这种方式发送.即时消息如何将来自选项卡的已删除文件中的数据转换为xml格式是我的下一个问题,但现在我只想看看我是否可以发布xml文件并获取它以发送短信.

这是从那里的网站

Hi , I have been googling this and cant seem to find a simple explanation of how to do this so if anyone could maybe point me to a site where I can find some information on how to do this that would be great.

I have edited the question to make it a bit easier to understand.

I am sending out sms''s via the internet at the moment I use a http api where I send the sms one by one which is a bit slow when sending thousands.So they have told me that using the xml method would be better , I assumed that I would create the xml file using there format below and would be able to send that way.How im going to get the data from the tab delimeted file into the xml format is my next problem , but for now I just want to see if I can post the xml file and get it to send the sms''s.

This is from there site

The XML generated has to be posted on the URL.

        URL: http://southafrica.msg91.com/NewXML.php

    The entire XML is store in single form element named "data" to be posted.

For Example:

<html>
<body>
<form name="frm" action="http://southafrica.msg91.com/NewXML.php" method="post">
<input type="hidden" name="data" value="" />
<input type="submit" value="SUBMIT" />
</form>
</body>
</html>



来自该站点的XML格式



XML Format from there site

<MESSAGE>
   <username>username</username>
   <password>password</password>
   <seqid>sequence_number</seqid>
   <requestid>random_unique_id</requestid>
   <SMS TEXT="message1">
   <ADDRESS FROM="senderid1" TO="number1" SEQ="unique_for_each_number" ></ADDRESS>
   <ADDRESS FROM="senderid2" TO="number2" SEQ="unique_for_each_number" ></ADDRESS>
   </SMS>
   <SMS TEXT="hi test message">
   <ADDRESS FROM="senderid3" TO="number3" SEQ="unique_for_each_number" ></ADDRESS>
   </SMS>
   </MESSAGE>



这就是我尝试过的



This is what I have tried

Dim strPostURL = "http://southafrica.msg91.com/NewXML.php"



        Dim requestStream As Stream = Nothing
        Dim fileStream As FileStream = Nothing
        Dim uploadResponse As Net.HttpWebResponse = Nothing

        Try

            Dim uploadRequest As Net.HttpWebRequest = CType(Net.HttpWebRequest.Create(strPostURL), Net.HttpWebRequest)
            uploadRequest.Method = Net.WebRequestMethods.Http.Post
            ' UploadFile is not supported through an Http proxy
            ' so we disable the proxy for this request.
            uploadRequest.Proxy = Nothing

            requestStream = uploadRequest.GetRequestStream()
            fileStream = File.Open("c:\test\sms.xml", FileMode.Open)

            Dim buffer(1024) As Byte
            Dim bytesRead As Integer
            While True
                bytesRead = fileStream.Read(buffer, 0, buffer.Length)
                If bytesRead = 0 Then
                    Exit While
                End If
                requestStream.Write(buffer, 0, bytesRead)
            End While

            ' The request stream must be closed before getting the response.
            requestStream.Close()

            uploadResponse = uploadRequest.GetResponse()
            Dim responseReader As StreamReader = New StreamReader(uploadRequest.GetResponse.GetResponseStream())
            Dim x As String = responseReader.ReadToEnd()
            responseReader.Close()





        Finally
            If uploadResponse IsNot Nothing Then
                uploadResponse.Close()
            End If
            If fileStream IsNot Nothing Then
                fileStream.Close()
            End If
            If requestStream IsNot Nothing Then
                requestStream.Close()
            End If
        End Try
    End Sub



但是我不知道我是否符合上述要求.感谢Christian的代码,我将看看我是否可以使用它.



But I dont know if I,m on the right track with the above or not.Thanks for code from Christian , I,ll see if I can use it.

推荐答案

存储在单个表单元素中"表示它是一个表单变量.据我所知,您需要转义XML,然后将其以data = xxx格式传递到页面,其中xxx是设置方法= POST后的编码XML.这是我在google上找到的代码.

''stored in a single form element'' means that it''s a form variable. From what I can tell, you need to escape your XML, and then pass it to the page in the format data=xxx where xxx is your encoded XML after setting method = POST. Here''s the code I found with google.

'The variables used to initialize post are already declared or parameters
'Strings should be formatted for a URI
Dim post As String = _
    "&type=" & Uri.EscapeUriString(feedbackType) &
    "&followup=" & Uri.EscapeUriString(requestReply) &
    "&email=" & Uri.EscapeUriString(email) &
    "&version=" & Uri.EscapeUriString(appVersion) &
    "&message=" & Uri.EscapeUriString(message)
Dim postBuffer As Byte() = 'Then we get the text in a binary format. I believe that PHP prefers ASCII.
System.Text.Encoding.ASCII.GetBytes(post);

'Create and initialize the request
'Note that the URL is not hardcoded.
Dim request As HttpWebRequest = _
    DirectCast(WebRequest.Create(feedbackUrl), HttpWebRequest)
request.UserAgent = "My Program Agent"
request.Method = "POST"

'Write POST data to request
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = postBuffer.Length
Dim requestStream As Stream = request.GetRequestStream()
requestStream.Write(postBuffer, 0, postBuffer.Length)
requestStream.Close()

'Execute request and read response.
Dim response As HttpWebResponse = _
    DirectCast(request.GetResponse(), HttpWebResponse)
ReadResponse(response)

postBuffer = Nothing


这篇关于如何在VB.net中将xml元素发布到api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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