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

前端 未结 3 720
情歌与酒
情歌与酒 2020-12-03 03:20

I am trying to use the ImageShack API to upload images. To use it, I am supposed to POST the image using multipart/form-data. I did it like ...

3条回答
  •  甜味超标
    2020-12-03 04:09

    I believe that you are not building the request body correctly. First, you need to include part boundary (random text) in content type header. For example,

    Content-Type: multipart/form-data; boundary=----WebKitFormBoundarySkAQdHysJKel8YBM

    Now format of request body will be something like

    ------WebKitFormBoundarySkAQdHysJKel8YBM 
    Content-Disposition: form-data;name="key"
    
    KeyValueGoesHere
    ------WebKitFormBoundarySkAQdHysJKel8YBM 
    Content-Disposition: form-data;name="param2"
    
    ValueHere
    ------WebKitFormBoundarySkAQdHysJKel8YBM 
    Content-Disposition: form-data;name="fileUpload"; filename="y1.jpg"
    Content-Type: image/jpeg 
    
    [image data goes here]
    

    I will suggest you to use tool such as Fiddler to understand how these requests are built.

提交回复
热议问题