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

猜你喜欢

转载自www.cnblogs.com/shangdishijiao/p/12517844.html