VB net HTTP request

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 & "--" &DimvbNewLine)

             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 of As  String = XMLRequest
         ' instance of a character Transcoding Object' 
        Dim encoding of As System.Text.Encoding = System.Text.Encoding.GetEncoding ( " UTF-. 8 " )
         ' Create a web request object' 
        Dim Request of As the System. = Net.WebRequest System.Net.WebRequest.Create (posturl)
         ' set mode request to POST' 
        request.method = " the POST " 
        ' defines an array of bytes' 
        Dim PostData () of as a System.Byte = encoding.GetBytes (XMLRequest)
         'Setting the request object request length bytes' 
        request.ContentLength = postdata.Length
         ' Get request object data stream' 
        Dim requesstream of As System.IO.Stream The = request.GetRequestStream ()
         ' data content is filled into the stream' 
        requesstream. the Write (PostData, 0 , postdata.Length)
         ' off stream' 
        requesstream.Close ()
         ' Get request object in response to the request according to the response object' 
        Dim response of As System.Net.WebResponse = request.GetResponse ()
         ' Get response data stream Object ' 
        Dim responseStream of As the StreamReader =New the StreamReader (response.GetResponseStream ())
         ' the response data stream read' 
        Dim HTML of As  String = responsestream.ReadToEnd () 
        requesstream.Close () 
        response.Close () 
        ' returns the response data to this request' 
        the Return HTML
     Function End 

    the Shared  Function AllHttpUploadFile ( ByVal URL of As  String , ByVal filePath of As  String )
         Dim Result of As  String = Nothing 
        the Try 
            Dim boundaryAs 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

 

Guess you like

Origin www.cnblogs.com/shangdishijiao/p/12517844.html