Prestashop-无法使用Prestashop 1.6 webservice上载新图像产品

Prestashop-无法使用Prestashop 1.6 webservice上载新图像产品,prestashop,prestashop-1.6,Prestashop,Prestashop 1.6,我试图通过vb.net应用程序为带有prestashop webservice的产品上载新图像,但收到以下错误消息: “无法保存此图像” 用于上载图像的URL如下:http://localhost/prestashop/api/images/products/1 发出请求的函数的源代码如下: Public Sub executeAddImage(ByVal resource As String, ByVal id As String, ByVal imageToAdd As Image)

我试图通过vb.net应用程序为带有prestashop webservice的产品上载新图像,但收到以下错误消息:

“无法保存此图像”

用于上载图像的URL如下:
http://localhost/prestashop/api/images/products/1
发出请求的函数的源代码如下:

Public Sub executeAddImage(ByVal resource As String, ByVal id As String, ByVal imageToAdd As Image)
    Dim response As String = Nothing

    Try
        Dim ms As New MemoryStream()
        imageToAdd.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
        Dim byteArray As Byte() = ms.ToArray()

        Dim requestUrl As String = Me.WebServiceURL & "/" & resource & "/" & id
        MsgBox(requestUrl)
        Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(requestUrl), HttpWebRequest)
        webRequest.Method = WebServicePrestashop.CRUDMethod.Create
        'webRequest.ContentType = "image/jpeg"
        webRequest.ContentType = "application/x-www-form-urlencoded"
        webRequest.Credentials = New NetworkCredential(Me.LoginName, WebServicePrestashop._password)
        webRequest.ContentLength = byteArray.Length
        MsgBox(byteArray.Length)
        ' Get the request stream
        Using dataStream As Stream = webRequest.GetRequestStream()
            dataStream.Write(byteArray, 0, byteArray.Length)
        End Using

        ' Get the response
        Using webResponse As HttpWebResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
            If webResponse.StatusCode = HttpStatusCode.OK Then
                Using reader As New StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)
                    Dim imageNew As Image = Image.FromStream(webResponse.GetResponseStream())
                End Using
            End If
        End Using

    Catch ex As WebException
        MsgBox(ex.Message.ToString())
        Dim reader As New StreamReader(ex.Response.GetResponseStream)
        MsgBox(reader.ReadToEnd)
    End Try
End Sub
我使用的是HTTPPOST方法,POST内容是新图像的二进制内容

我如何修复它?

这里是解决方案。 我认为关键是我必须以编程方式编写webrequest的主体,将边界(二进制数组格式)、内容类型链(二进制数组格式)和要上载的图像内容(二进制数组格式)添加到webrequest的流中

Public Sub executeAddImage(ByVal resource As String, ByVal id As String, ByVal imageToAdd As Byte())
    Dim response As String = Nothing


    Try
        Dim requestUrl As String = "urlShop" & "/api/" & resource & "/" & id
        MsgBox(requestUrl)
        Dim webRequest As HttpWebRequest = DirectCast(System.Net.WebRequest.Create(requestUrl), HttpWebRequest)
        webRequest.KeepAlive = True
        webRequest.Credentials = New NetworkCredential(Me.LoginName, WebServicePrestashop._password)
        webRequest.ContentLength = imageToAdd.Length
        webRequest.Method = "POST"
        webRequest.ContentType = "image/jpeg"


        Dim boundary As String = "----" & DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture)
        webRequest.ContentType = "multipart/form-data; boundary=" & boundary
        Dim beginPostData = "--" & boundary & vbCrLf & "Content-Disposition: form-data; name=""image""; filename=""torrente.jpg""" & _
            vbCrLf & "Content-Type: image/jpeg" & vbCrLf & vbCrLf
        Dim boundaryBytes = System.Text.Encoding.ASCII.GetBytes(vbCrLf & "--" & boundary & "--" & vbCrLf)
        Dim beginPostDataBytes = System.Text.Encoding.ASCII.GetBytes(beginPostData)


        webRequest.ContentLength = beginPostData.Length + imageToAdd.Length + boundaryBytes.Length


        ' Get the request stream
        Using dataStream As Stream = webRequest.GetRequestStream()
            dataStream.Write(beginPostDataBytes, 0, beginPostDataBytes.Length)
            dataStream.Write(imageToAdd, 0, imageToAdd.Length)
            dataStream.Write(boundaryBytes, 0, boundaryBytes.Length)
        End Using


        ' Get the response
        Using webResponse As HttpWebResponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
            If webResponse.StatusCode = HttpStatusCode.OK Then
                Using reader As New StreamReader(webResponse.GetResponseStream())
                    response = reader.ReadToEnd()
                    MsgBox(response)
                End Using
            End If
        End Using


    Catch ex As WebException
        MsgBox(ex.Message.ToString())
        Dim reader As New StreamReader(ex.Response.GetResponseStream)
        MsgBox(reader.ReadToEnd)
    End Try
End Sub