gpt4 book ai didi

vb.net - 上传图像多部分表单 HTTPWebRequest - 获取图像数据到字符串?

转载 作者:行者123 更新时间:2023-12-01 23:07:50 25 4
gpt4 key购买 nike

我正在尝试使用 HTTPWebRequest 上传一个包含多个部分的表单,在我添加要上传的图片之前一切正常,主要是我正在尝试执行与浏览器发出的请求完全相同的请求,如下所示:

-----------------------------41184676334
Content-Disposition: form-data; name="file"; filename="guitar tape.jpg"
Content-Type: image/jpeg

IMAGEDATAHERE
-----------------------------41184676334
Content-Disposition: form-data; name="save"

save
-----------------------------41184676334--

我不知道如何格式化/读取图像以将其设置为我在下面提出的请求:

            Dim boundary As String = "-----------------------------" & DateTime.Now.Ticks.ToString("x")
Dim req As HttpWebRequest = DirectCast(WebRequest.Create("http://www.mysite.com/upload.php"), HttpWebRequest)
req.Method = "POST"
req.ContentType = "multipart/form-data; boundary=" & "---------------------------" & DateTime.Now.Ticks.ToString("x")
req.KeepAlive = False
Dim builder As New StringBuilder()
builder.Append(boundary & vbCrLf & "Content-Disposition: form-data; name=""variable1""" & vbCrLf & vbCrLf & "1" & vbCrLf)
builder.Append(boundary & vbCrLf & "Content-Disposition: form-data; name=""file""; filename=""" & FileName & """" & vbCrLf)
builder.Append("Content-Type: application/octet-stream")
builder.Append(vbCrLf & vbCrLf)
' Add Photo Here
If UpdateImage = True Then
' Load Image
Dim ImageData As System.Drawing.Image
Dim fs As New System.IO.FileStream(ImagePath, System.IO.FileMode.Open)
ImageData = Image.FromStream(fs)
fs.Close()
' Add Image To Header
builder.Append(ImageData)
builder.Append(vbCrLf)
Else
builder.Append(vbCrLf)
End If
builder.Append(boundary & vbCrLf & "Content-Disposition: form-data; name=""save""" & vbCrLf & vbCrLf & "save")
' Footer Bytes
Dim close As Byte() = Encoding.UTF8.GetBytes("--")
Dim postHeader As String = builder.ToString()
Dim postHeaderBytes As Byte() = Encoding.UTF8.GetBytes(postHeader)
Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes(vbCrLf & boundary & "--" & vbCrLf)
Dim length As Long = postHeaderBytes.Length + boundaryBytes.Length
req.ContentLength = length
Dim requestStream As Stream = req.GetRequestStream()
Dim fulllength As Integer = postHeaderBytes.Length + boundaryBytes.Length
' Write out our post header
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length)
' Write out the trailing boundary
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length)
Dim responce As WebResponse
responce = req.GetResponse()
requestStream.Close()
Dim s As Stream = responce.GetResponseStream()
Dim sr As New StreamReader(s)
Dim Content As String = sr.ReadToEnd()

目前它只是将“System.Drawing.Bitmap”作为图像数据发布,但我不确定如何为看起来像这样的图像获取相同的原始数据:

    J©õݨe‚Lnž¿Ëã/ǧúÐ5ý¼C÷Cý>ß’t;fm—=Äw:�/E±ËÙÏ$á@%Pc>×    Šgw.²Ab“:ÅÓù:ϯÌh6à€Z§Ó‚g£®hÚD6¨Ø^Ú2ô`ä¨L�YÆÄÅCX#I“ÈÌãj¦L˜•’|¥�Eb¡ëQ–¤Ú, 3\UzL  öÔoj4�•±’u«c¼#„oÕ`îF>·o—ŠûÅ«ÎÑ™¶Ç˜ýº*i°œÈVŒ�Qû”Ñ[.�ÔmçE•ì¦eNCh�Ù
é§�É$m¿ôš"»ÌNæ(VÌmp›F¹XÈ88™ªüµ…d•XµÔÜ#�ˆŠv‘º‚F‚§Yûb

关于我如何做到这一点或者我是否需要改变我的方法有什么想法吗?

最佳答案

    builder.Append(ImageData)

不正确。您需要以字节形式读取图像,然后将字节 [] 添加到多部分帖子中。

详见 Using HttpWebRequest to POST data/upload image using multipart/form-data

并确保使用 http 嗅探器(即 fiddler)来查看它实际发送的内容。

首先,将图像加载到字节数组中,然后将其转换为base64:

imgBase64 = Convert.ToBase64String(my_image_byte_array)

关于vb.net - 上传图像多部分表单 HTTPWebRequest - 获取图像数据到字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13918238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com