Multipart /form-data试图上传文件到服务器



我正在开发一个客户端发送图像到服务器使用multipart/form-data,我的搭档用的是MAC,我用的是win8。

我正在发送它在MAC上工作的所有方式,但从客户端它不能通过webservice,错误消息是:"bad content body"

这是我的代码:

string boundary = "96c334567890";
byte[] sub = AuxiliaryMethods.ReadFileInCloudStorage(urlImg, i);
//POST 
// Create a request using a URL that can receive a post. 
WebRequest request = (HttpWebRequest)WebRequest.Create(urlImgPost);
// Create POST data and convert it to a byte array.
string strAux = "--" + boundary + "rn" + 
"Content-Disposition: form-data; name="file"; filename="photo" + i + ".jpg"" + "rn" +
"Content-Type: image/jpeg" + "rnrn" +
System.Text.Encoding.UTF8.GetString(sub) + "rn" +
boundary + "--";
byte[] byteArray = Encoding.UTF8.GetBytes(strAux);
// Set the Method property of the request to POST.
request.Method = "POST";
request.Headers.Add("Authorization: Token token=token");
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Set the ContentType property of the WebRequest.
request.ContentType = "multipart/form-data; boundary="+boundary;

和我的标题和正文应该看起来像这样:

POST https://apidev.test.pt/images HTTP/1.1
Authorization: Token token=token
Content-Type: multipart/form-data; boundary=96c334567890
Host: apidev.test.pt
Content-Length: 764939
--96c334567890
content-disposition: form-data; name="file"; filename="photo1.jpg"
content-type: image/jpeg
(image binary data)
96c334567890--

我应该添加一些东西,因为我是在Windows系统上,还是我的请求被做错了?这是用RFC2388

格式化的

上传文件到服务器

strBoundary = AlphaNumString(32)
strBody = "--" & strBoundary & vbCrLf
strBody = strBody & "Content-Disposition: form-data; name=""" & UploadName & """; filename=""" & filename & """" & vbCrLf
strBody = strBody & "Content-Type: " & MimeType & vbCrLf
strBody = strBody & vbCrLf & strData
strBody = strBody & vbCrLf & "--" & strBoundary & "--"

Length = Len(strBody)
strHTTP = "POST " & DestUrl.URI & "?" & DestUrl.Query & " HTTP/1.0" & vbCrLf
strHTTP = strHTTP & "Host: " & DestUrl.Host & vbCrLf
strHTTP = strHTTP & "Content-Type: multipart/form-data, boundary=" & strBoundary & vbCrLf
strHTTP = strHTTP & "Content-Length: " & Length & vbCrLf & vbCrLf
strHTTP = strHTTP & strBody

最新更新