学无止境

-------至弱即为至强!

导航

实现向指定Url提交信息并取得返回信息

Posted on 2006-06-16 08:30  赵国亮  阅读(686)  评论(0编辑  收藏  举报
            以下代码实现了向指定Url提交信息并取得该Url的返回信息:

            Dim HttpWReq As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create("Url地址"), System.Net.HttpWebRequest)

            Dim postData As String

            postData = "<?xml version=""1.0"" encoding=""GBK""?>" & vbCrLf
            postData = postData & "<order>" & vbCrLf
            postData = postData & "<source>" & Message.source & "</source>" & vbCrLf
            postData = postData & "<phone>" & Message.phone & "</phone>" & vbCrLf
            postData = postData & "<media>" & Message.media & "</media>" & vbCrLf
            postData = postData & "<content>" & Message.content & "</content>" & vbCrLf
            postData = postData & "<product_id>" & Message.productid & "</product_id>" & vbCrLf
            postData = postData & "<orderitem_id>" & Message.orderitemid & "</orderitem_id>" & vbCrLf
            postData = postData & "<link_id>" & Message.linkid & "</link_id>" & vbCrLf
            postData = postData & "</order>" & vbCrLf

            Dim postByte As Byte() = System.Text.Encoding.GetEncoding("gb2312").GetBytes(postData)

            HttpWReq.Method = "post"
            HttpWReq.ContentLength = System.Text.Encoding.GetEncoding("gb2312").GetByteCount(postData)
            HttpWReq.ContentType = "application/x-www-form-urlencoded"
            HttpWReq.Timeout = 1000

            Dim WriteStream As Stream = HttpWReq.GetRequestStream
            WriteStream.Write(postByte, 0, postByte.Length)
            WriteStream.Flush()
            WriteStream.Close()

            Dim HttpWResp As HttpWebResponse = CType(HttpWReq.GetResponse(), HttpWebResponse)
            Dim ReadStream As StreamReader = New StreamReader(HttpWResp.GetResponseStream(), System.Text.Encoding.GetEncoding("gb2312"))

            Dim ret As String = ""
            While ReadStream.ReadLine <> ""
                ret = ret + ReadStream.ReadLine
            End While
            Dim resultBegin As Integer = ret.IndexOf("<resultcode>")
            Dim resultEnd As Integer = ret.IndexOf("</resultcode>")
            Dim returnResult As String = ret.Substring(resultBegin + 12, resultEnd - resultBegin - 12)

            HttpWResp.Close()

            If returnResult.ToString = "00" Then
                Return "1"
            Else
                Return "信息发送失败!"
            End If