×

VB.NET发送post请求的三种方式指定发送字符串内容、键值对和json串

Kalet Kalet 发表于2023-09-07 16:28:49 浏览511 评论0

抢沙发发表评论

转自:VB.NET发送post请求的三种方式指定发送字符串内容、键值对和json串_vb.net post-CSDN博客


   ''' <summary>

    ''' 指定Post地址使用Get 方式获取全部字符串

    ''' </summary>

    ''' <param name="url">请求地址</param>

    ''' <param name="dic">键值对</param>

    ''' <returns>返回字符串</returns>

    ''' <remarks></remarks>

    Public Function Post(ByVal url As String, ByVal dic As Dictionary(Of String, String)) As String

        Dim result As String = ""

        Dim req As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)

        req.Method = "POST"

        req.ContentType = "application/x-www-form-urlencoded;charset=UTF-8"

        Dim builder As StringBuilder = New StringBuilder()

        Dim i As Integer = 0

        For Each item In dic

            If i > 0 Then builder.Append("&")

            builder.AppendFormat("{0}={1}", item.Key, item.Value)

            i += 1

        Next

        Dim data As Byte() = Encoding.UTF8.GetBytes(builder.ToString())

        req.ContentLength = data.Length

        Using reqStream As Stream = req.GetRequestStream()

            reqStream.Write(data, 0, data.Length)

            reqStream.Close()

        End Using

        Dim resp As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)

        Dim stream As Stream = resp.GetResponseStream()

        Using reader As StreamReader = New StreamReader(stream, Encoding.UTF8)

            result = reader.ReadToEnd()

        End Using

 

        Return result

    End Function

 

    ''' <summary>

    ''' 指定Post地址使用Get 方式获取全部字符串

    ''' </summary>

    ''' <param name="url">请求地址</param>

    ''' <param name="content">Post提交数据内容(utf-8编码的)</param>

    ''' <returns>返回字符串</returns>

    ''' <remarks></remarks>

    Public Function Post(ByVal url As String, ByVal content As String) As String

        Dim result As String = ""

        Dim req As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)

        req.Method = "POST"

        req.ContentType = "application/x-www-form-urlencoded"

        Dim data As Byte() = Encoding.UTF8.GetBytes(content)

        req.ContentLength = data.Length

 

        Using reqStream As Stream = req.GetRequestStream()

            reqStream.Write(data, 0, data.Length)

            reqStream.Close()

        End Using

 

        Dim resp As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)

        Dim stream As Stream = resp.GetResponseStream()

 

        Using reader As StreamReader = New StreamReader(stream, Encoding.UTF8)

            result = reader.ReadToEnd()

        End Using

 

        Return result

    End Function

 

''' <summary>

    ''' json串post请求

    ''' </summary>

    ''' <param name="url"></param>

    ''' <param name="postData">json字符串</param>

    ''' <returns></returns>

    ''' <remarks></remarks>

    Public Function Post(ByVal url As String, ByVal postData As String) As String

        Dim result As String = ""

        Dim req As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)

        req.Method = "POST"

        req.Timeout = "800"

        req.ContentType = "application/json"

        Dim data As Byte() = Encoding.UTF8.GetBytes(postData)

        req.ContentLength = data.Length

 

        Using reqStream As Stream = req.GetRequestStream()

            reqStream.Write(data, 0, data.Length)

            reqStream.Close()

        End Using

 

        Dim resp As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse)

        Dim stream As Stream = resp.GetResponseStream()

 

        Using reader As StreamReader = New StreamReader(stream, Encoding.UTF8)

            result = reader.ReadToEnd()

        End Using

 

        Return result

    End Function


json串最好新建一个实体类(tip:关键字和属性名冲突,可以中括号括起来声明[属性名]),实体类对象(new class.root)通过Newtonsoft.Json.dll序列化成json

群贤毕至

访客