• VB net HTTP请求


    Imports System.IO
    Imports System.Net
    Imports System.Text
    
    Public Class Http
        Shared Function HttpRequest(ByVal url As String, ByVal verb As String, ByVal postVars As String) As String
            Dim result As String = Nothing
            Try
                Dim req As HttpWebRequest = TryCast(WebRequest.Create(New Uri(url)), HttpWebRequest)
                req.Method = verb
                req.ContentType = "application/json; charset=UTF-8"
                req.Accept = "application/json; charset=UTF-8"
                If (verb.Equals("POST")) OrElse (verb.Equals("PUT")) Then
                    Dim content As Byte() = Encoding.UTF8.GetBytes(postVars)
                    req.ContentLength = content.Length
                    Using post As Stream = req.GetRequestStream()
                        post.Write(content, 0, content.Length)
                    End Using
                End If
                Using resp As HttpWebResponse = TryCast(req.GetResponse(), HttpWebResponse)
                    Dim reader As New StreamReader(resp.GetResponseStream())
                    result = reader.ReadToEnd()
                    reader.Close()
                End Using
            Catch [error] As WebException
            End Try
            Return result
        End Function
    
        Shared Function HttpUploadFile(ByVal url As String, ByVal filePath As String)
            Dim result As String = Nothing
            Try
                Dim boundary As String = Path.GetRandomFileName
                Dim header As New StringBuilder()
                header.AppendLine("--" & boundary)
                header.Append("Content-Disposition: form-data; name=""file"";")
                header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(filePath))
                header.AppendLine()
                header.AppendLine("Content-Type: application/octet-stream")
                header.AppendLine()
    
                Dim headerbytes() As Byte = Encoding.UTF8.GetBytes(header.ToString)
                Dim endboundarybytes() As Byte = Encoding.ASCII.GetBytes(vbNewLine & "--" & boundary & "--" & vbNewLine)
    
                Dim req As HttpWebRequest = TryCast(WebRequest.Create(New Uri(url)), HttpWebRequest)
                req.ContentType = "multipart/form-data; boundary=" & boundary
                req.ContentLength = headerbytes.Length + New System.IO.FileInfo(filePath).Length + endboundarybytes.Length
                req.AllowWriteStreamBuffering = False
                req.Method = "POST"
                Using s As Stream = req.GetRequestStream()
                    s.Write(headerbytes, 0, headerbytes.Length)
                    Dim filebytes() As Byte = My.Computer.FileSystem.ReadAllBytes(filePath)
                    s.Write(filebytes, 0, filebytes.Length)
                    s.Write(endboundarybytes, 0, endboundarybytes.Length)
                End Using
    
                Using resp As HttpWebResponse = TryCast(req.GetResponse(), HttpWebResponse)
                    Dim reader As New StreamReader(resp.GetResponseStream())
                    result = reader.ReadToEnd()
                    reader.Close()
                End Using
            Catch ex As WebException
            End Try
            Return result
        End Function
    
    
    End Class
    Imports System.IO
    Imports System.Net
    Imports System.Text
    Imports Newtonsoft.Json
    
    Public Class HttpHelp
    
        Shared Function AllHttpRequest(ByVal url As String, ByVal verb As String, ByVal postVars As String) As String
            Dim result As String = Nothing
            Try
                Dim req As HttpWebRequest = TryCast(WebRequest.Create(New Uri(url)), HttpWebRequest)
                req.Method = verb
                req.ContentType = "application/json; charset=UTF-8"
                req.Accept = "application/json; charset=UTF-8"
                req.Headers.Add(String.Format("Authorization: Bearer {0}", AccessToken))
    
                If (verb.Equals("POST")) OrElse (verb.Equals("PUT")) Then
                    Dim content As Byte() = Encoding.UTF8.GetBytes(postVars)
                    req.ContentLength = content.Length
                    Using post As Stream = req.GetRequestStream()
                        post.Write(content, 0, content.Length)
                    End Using
                End If
                Using resp As HttpWebResponse = TryCast(req.GetResponse(), HttpWebResponse)
                    Dim reader As New StreamReader(resp.GetResponseStream())
                    result = reader.ReadToEnd()
                    reader.Close()
                End Using
            Catch [error] As WebException
            End Try
            Return result
        End Function
    
        Shared Function AllGETHttpRequest(ByVal url As String, ByVal postVars As String) As String
            Dim result As String = Nothing
            Try
                ' postVars = "UserName=admin&Password=123"; 
                url = String.Format(url + "?{0}", postVars)
                Dim req As HttpWebRequest = TryCast(WebRequest.Create(New Uri(url)), HttpWebRequest)
                req.Method = "GET"
                req.ContentType = "application/json; charset=UTF-8"
                req.Accept = "application/json; charset=UTF-8"
                req.Headers.Add(String.Format("Authorization: Bearer {0}", AccessToken))
    
                Using resp As HttpWebResponse = TryCast(req.GetResponse(), HttpWebResponse)
                    Dim reader As New StreamReader(resp.GetResponseStream())
                    result = reader.ReadToEnd()
                    reader.Close()
                End Using
            Catch [error] As WebException
            End Try
            Return result
        End Function
    
        Public Function PostRequest(ByVal xmlRequest As String, ByVal postUrl As String) As String
            Dim xml As String = xmlRequest
            '实例化一个字符转码对象'
            Dim encoding As System.Text.Encoding = System.Text.Encoding.GetEncoding("utf-8")
            '创建一个web请求对象'
            Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(postUrl)
            '设置请求方式为post'
            request.Method = "POST"
            '定义字节数组'
            Dim postdata() As System.Byte = encoding.GetBytes(xmlRequest)
            '设置request对象的请求字节的长度'
            request.ContentLength = postdata.Length
            '获取request对象的数据流'
            Dim requesstream As System.IO.Stream = request.GetRequestStream()
            '将数据内容填充到流中'
            requesstream.Write(postdata, 0, postdata.Length)
            '关闭流'
            requesstream.Close()
            '根据请求的request对象获取响应的response对象'
            Dim response As System.Net.WebResponse = request.GetResponse()
            '获取response数据流对象'
            Dim responsestream As StreamReader = New StreamReader(response.GetResponseStream())
            '将response流中的数据读取'
            Dim html As String = responsestream.ReadToEnd()
            requesstream.Close()
            response.Close()
            '返回本次请求的响应数据'
            Return html
        End Function
    
        Shared Function AllHttpUploadFile(ByVal url As String, ByVal filePath As String)
            Dim result As String = Nothing
            Try
                Dim boundary As String = Path.GetRandomFileName
                Dim header As New StringBuilder()
                header.AppendLine("--" & boundary)
                header.Append("Content-Disposition: form-data; name=""file"";")
                header.AppendFormat("filename=""{0}""", IO.Path.GetFileName(filePath))
                header.AppendLine()
                header.AppendLine("Content-Type: application/octet-stream")
                header.AppendLine()
    
                Dim headerbytes() As Byte = Encoding.UTF8.GetBytes(header.ToString)
                Dim endboundarybytes() As Byte = Encoding.ASCII.GetBytes(vbNewLine & "--" & boundary & "--" & vbNewLine)
    
                Dim req As HttpWebRequest = TryCast(WebRequest.Create(New Uri(url)), HttpWebRequest)
                req.ContentType = "multipart/form-data; boundary=" & boundary
                req.ContentLength = headerbytes.Length + New System.IO.FileInfo(filePath).Length + endboundarybytes.Length
                req.AllowWriteStreamBuffering = False
                req.Headers.Add(String.Format("Authorization: Bearer {0}", AccessToken))
                req.Method = "POST"
    
                Using s As Stream = req.GetRequestStream()
                    s.Write(headerbytes, 0, headerbytes.Length)
                    Dim filebytes() As Byte = My.Computer.FileSystem.ReadAllBytes(filePath)
                    s.Write(filebytes, 0, filebytes.Length)
                    s.Write(endboundarybytes, 0, endboundarybytes.Length)
                End Using
    
                Using resp As HttpWebResponse = TryCast(req.GetResponse(), HttpWebResponse)
                    Dim reader As New StreamReader(resp.GetResponseStream())
                    result = reader.ReadToEnd()
                    reader.Close()
                End Using
            Catch ex As WebException
            End Try
            Return result
        End Function
    
    
    
    End Class
  • 相关阅读:
    P5287 [HNOI2019]JOJO border理论 主席树
    P3973 [TJOI2015]线性代数 最小割
    P1712 [NOI2016] 区间 尺取法 线段树
    P7093 [CERC2014]Can't stop playing (动态规划)
    P6958 [NEERC2017]The Great Wall
    P4827 [国家集训队] Crash 的文明世界 第二类斯特林数
    CF932E Team Work 第二类Strling数
    P3175 [HAOI2015]按位或 FMT Min-Max容斥
    ue shader parameter structure/class FSceneTexturesUniformParameters
    【微信小程序】性能优化
  • 原文地址:https://www.cnblogs.com/shangdishijiao/p/12517844.html
Copyright © 2020-2023  润新知