Upload files with HTTPWebrequest (multipart/form-data)

后端 未结 21 3084
独厮守ぢ
独厮守ぢ 2020-11-21 04:53

Is there any class, library or some piece of code which will help me to upload files with HTTPWebrequest?

Edit 2:

I do not

21条回答
  •  -上瘾入骨i
    2020-11-21 05:09

    VB Example (converted from C# example on another post):

    Private Sub HttpUploadFile( _
        ByVal uri As String, _
        ByVal filePath As String, _
        ByVal fileParameterName As String, _
        ByVal contentType As String, _
        ByVal otherParameters As Specialized.NameValueCollection)
    
        Dim boundary As String = "---------------------------" & DateTime.Now.Ticks.ToString("x")
        Dim newLine As String = System.Environment.NewLine
        Dim boundaryBytes As Byte() = Text.Encoding.ASCII.GetBytes(newLine & "--" & boundary & newLine)
        Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri)
    
        request.ContentType = "multipart/form-data; boundary=" & boundary
        request.Method = "POST"
        request.KeepAlive = True
        request.Credentials = Net.CredentialCache.DefaultCredentials
    
        Using requestStream As IO.Stream = request.GetRequestStream()
    
            Dim formDataTemplate As String = "Content-Disposition: form-data; name=""{0}""{1}{1}{2}"
    
            For Each key As String In otherParameters.Keys
    
                requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
                Dim formItem As String = String.Format(formDataTemplate, key, newLine, otherParameters(key))
                Dim formItemBytes As Byte() = Text.Encoding.UTF8.GetBytes(formItem)
                requestStream.Write(formItemBytes, 0, formItemBytes.Length)
    
            Next key
    
            requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
    
            Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""{2}Content-Type: {3}{2}{2}"
            Dim header As String = String.Format(headerTemplate, fileParameterName, filePath, newLine, contentType)
            Dim headerBytes As Byte() = Text.Encoding.UTF8.GetBytes(header)
            requestStream.Write(headerBytes, 0, headerBytes.Length)
    
            Using fileStream As New IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read)
    
                Dim buffer(4096) As Byte
                Dim bytesRead As Int32 = fileStream.Read(buffer, 0, buffer.Length)
    
                Do While (bytesRead > 0)
    
                    requestStream.Write(buffer, 0, bytesRead)
                    bytesRead = fileStream.Read(buffer, 0, buffer.Length)
    
                Loop
    
            End Using
    
            Dim trailer As Byte() = Text.Encoding.ASCII.GetBytes(newLine & "--" + boundary + "--" & newLine)
            requestStream.Write(trailer, 0, trailer.Length)
    
        End Using
    
        Dim response As Net.WebResponse = Nothing
    
        Try
    
            response = request.GetResponse()
    
            Using responseStream As IO.Stream = response.GetResponseStream()
    
                Using responseReader As New IO.StreamReader(responseStream)
    
                    Dim responseText = responseReader.ReadToEnd()
                    Diagnostics.Debug.Write(responseText)
    
                End Using
    
            End Using
    
        Catch exception As Net.WebException
    
            response = exception.Response
    
            If (response IsNot Nothing) Then
    
                Using reader As New IO.StreamReader(response.GetResponseStream())
    
                    Dim responseText = reader.ReadToEnd()
                    Diagnostics.Debug.Write(responseText)
    
                End Using
    
                response.Close()
    
            End If
    
        Finally
    
            request = Nothing
    
        End Try
    
    End Sub
    

提交回复
热议问题