首页 > 代码库 > vb.net模拟文件上传
vb.net模拟文件上传
Imports Microsoft.VisualBasic Imports System.Net Imports System.Security.Cryptography.X509Certificates Imports System.Security.Authentication Imports System.Net.Security Imports System.Collections.Generic Imports System.Text Imports System.IO Public Class HttpUpload Public Function GetWebRequest(ByVal url As String, ByVal method As String) As HttpWebRequest Dim req As HttpWebRequest = Nothing If url.Contains("https") Then ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf CheckValidationResult) req = CType(WebRequest.CreateDefault(New Uri(url)), HttpWebRequest) Else req = CType(WebRequest.Create(url), HttpWebRequest) End If req.ServicePoint.Expect100Continue = False req.Method = method req.KeepAlive = True req.UserAgent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36" Return req End Function Public Function CheckValidationResult(ByVal sender As Object, ByVal certificate As X509Certificate, ByVal chain As X509Chain, ByVal errors As SslPolicyErrors) As Boolean Return True End Function ‘‘‘ <summary> ‘‘‘ 上传文件 ‘‘‘ </summary> ‘‘‘ <param name="url"></param> ‘‘‘ <param name="textParams"></param> ‘‘‘ <param name="filename"></param> ‘‘‘ <param name="filestream"></param> ‘‘‘ <returns></returns> ‘‘‘ <remarks></remarks> Public Function UploadFile(ByVal url As String, ByVal textParams As Dictionary(Of String, String), ByVal filename As String, ByVal filestream As System.IO.Stream) As String Dim boundary As String = DateTime.Now.Ticks.ToString("X") ‘ 随机分隔线 Dim req As HttpWebRequest = GetWebRequest(url, "POST") req.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary Dim reqStream As System.IO.Stream = req.GetRequestStream() Dim itemBoundaryBytes As Byte() = Encoding.UTF8.GetBytes(vbCrLf & "--" & boundary & vbCrLf) Dim endBoundaryBytes As Byte() = Encoding.UTF8.GetBytes(vbCrLf & "--" & boundary & "--" & vbCrLf) ‘ 组装文本请求参数 If Not IsNothing(textParams) Then Dim textTemplate As String = "Content-Disposition:form-data;name=""{0}""" & vbCrLf & "Content-Type:text/plain" & vbCrLf & vbCrLf & "{1}" For Each kvp As KeyValuePair(Of String, String) In textParams Dim textEntry As String = String.Format(textTemplate, kvp.Key, kvp.Value) Dim textBytes As Byte() = Encoding.UTF8.GetBytes(textEntry) reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length) reqStream.Write(textBytes, 0, textBytes.Length) Next End If ‘ 组装文件请求参数 Dim fileTemplate As String = "Content-Disposition:form-data;name=""{0}"";filename=""{1}""" & vbCrLf & "Content-Type:{2}" & vbCrLf & vbCrLf Dim fileEntry As String = String.Format(fileTemplate, "file", filename, "application/octet-stream") Dim itemBytes As Byte() = Encoding.UTF8.GetBytes(fileEntry) reqStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length) reqStream.Write(itemBytes, 0, itemBytes.Length) Dim fileBytes As Byte() = New Byte(filestream.Length) {} filestream.Read(fileBytes, 0, fileBytes.Length) ‘把文件流读入字节数组 filestream.Close() reqStream.Write(fileBytes, 0, fileBytes.Length) ‘上传字节数组 reqStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length) ‘结尾标志 reqStream.Close() Dim rsp As HttpWebResponse = CType(req.GetResponse(), HttpWebResponse) Return GetResponseAsString(rsp, Encoding.UTF8) End Function ‘‘‘ <summary> ‘‘‘ 把响应流转换为文本。 ‘‘‘ </summary> ‘‘‘ <param name="rsp"></param> ‘‘‘ <param name="encoding"></param> ‘‘‘ <returns></returns> ‘‘‘ <remarks></remarks> Public Function GetResponseAsString(ByVal rsp As HttpWebResponse, ByVal encoding As Encoding) As String Dim stream As System.IO.Stream = Nothing Dim reader As StreamReader = Nothing Try ‘ 以字符流的方式读取HTTP响应 stream = rsp.GetResponseStream() reader = New StreamReader(stream, encoding) Return reader.ReadToEnd() Catch ex As Exception Finally ‘ 释放资源 If Not IsNothing(reader) Then reader.Close() If Not IsNothing(stream) Then stream.Close() If Not IsNothing(rsp) Then rsp.Close() End Try Return String.Empty End Function End Class
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。