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