栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

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

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

 ''' 
    ''' 指定Post地址使用Get 方式获取全部字符串
    ''' 
    ''' 请求地址
    ''' 键值对
    ''' 返回字符串
    ''' 
    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

    ''' 
    ''' 指定Post地址使用Get 方式获取全部字符串
    ''' 
    ''' 请求地址
    ''' Post提交数据内容(utf-8编码的)
    ''' 返回字符串
    ''' 
    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

''' 
    ''' json串post请求
    ''' 
    ''' 
    ''' json字符串
    ''' 
    ''' 
    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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/908974.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号