Duyuruyu Kapat
Facebook Gözat
Twitter Gözat

http post multipart/form-data

Konu, 'Visual Basic .Net' kısmında msdztrk tarafından paylaşıldı.

  1. msdztrk

    msdztrk Yeni Üye

    Kayıt:
    15 Ocak 2014
    Mesajlar:
    9
    Beğenilen Mesajlar:
    0
    Merhaba,
    VB.NET üzerinden bir restlike api metoduna file tipinde veri göndermeye çalışıyoruz.
    Bütün denemelerimize rağmen "500 internal server" ve "400 bad request" hatalarını almaktan ileriye geçemedik. Bu konudaki tecrübelerinizden istifade etmek isteriz.

    Public Function VariantUploadImage(variantImage As String, apiKey As String) As String
    Dim strResult As String = ""
    Try
    Dim boundary As String = "------" & DateTime.Now.Ticks.ToString("x")
    Dim url As String = "xxxxx"
    Dim request As HttpWebRequest = CType(HttpWebRequest.Create("" & url & ""), HttpWebRequest)

    request.Headers("Cookie") = "xxxxx"
    request.ContentType = "multipart/form-data; boundary=" & boundary
    request.Method = "POST"
    request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0"
    request.KeepAlive = True
    request.MediaType = "image/jpeg"

    Dim ms As New MemoryStream
    Dim ms2 As New MemoryStream

    Dim postdata As String = boundary & "Content-Disposition: form-data; name=""file""; filename=""imageExample.jpg""" & vbCrLf &
    "Content-Type: image/jpeg"

    Dim formitembytes As Byte() = System.Text.Encoding.UTF8.GetBytes(postdata)
    ms.Write(formitembytes, 0, formitembytes.Length)

    Using client As New WebClient()
    ms2 = New MemoryStream(client.DownloadData(variantImage))
    ms.Write(ms2.ToArray(), 0, ms2.ToArray().Length)
    End Using

    Dim byteData As Byte() = ms.ToArray()

    postdata = boundary & "---"

    formitembytes = System.Text.Encoding.UTF8.GetBytes(postdata)
    ms.Write(formitembytes, 0, formitembytes.Length)

    request.ContentLength = byteData.Length

    Using requestStream As Stream = request.GetRequestStream()
    ms.Position = 0
    Dim tempBuffer As Byte() = New Byte(ms.Length - 1) {}
    ms.Read(tempBuffer, 0, tempBuffer.Length)
    ms.Close()
    requestStream.Write(tempBuffer, 0, tempBuffer.Length)​
    End Using

    Dim response = DirectCast(request.GetResponse(), HttpWebResponse)
    Using streamReader = New StreamReader(response.GetResponseStream())
    strResult = streamReader.ReadToEnd()
    End Using

    Catch ex As Exception
    End Try
    Return strResult
    End Function