質問

i'm developing a client to send images to a server using multipart/form-data, my partner is a using a mac and i'm on win8.

i'm sending everything the way it works on a mac but from the client side it can't pass through the webservice and the error message is: "bad content body"

this is my code:

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 + "\r\n" + 
"Content-Disposition: form-data; name=\"file\"; filename=\"photo" + i + ".jpg\"" + "\r\n" +
"Content-Type: image/jpeg" + "\r\n\r\n" +
System.Text.Encoding.UTF8.GetString(sub) + "\r\n" +
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;

and my header and body should look something like this:

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--

should i be adding something because i'm on a windows system or is my request beeing done the wrong way? this is formatted with the RFC2388

役に立ちましたか?

解決

To upload a file to server

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
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top