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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。