0%

HTTP POST and multipart forms

HTTP POST 使用注意事项:

  • http Body 中的 NSData 编码方式要用 NSASCIIStringEncoding 而不是 NSUTF8StringEncoding * 通过
    NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; 计算数据的长度

POST 参数设置

1
2
3
4
5
6
//设置 header Content-Length
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
//设置 header contentType
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
//设置 body
[request setHTTPBody:postData];

备注:普通 postheaderCurrent-Typeapplication/x-www-form-urlencoded #### Multipart Forms POST 参数设置

1
2
3
4
5
6
//设置 header contentType
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

//设置 body contentType
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

备注: Multipart FormsheaderCurrent-Typemultipart/form-data request body like this

    --YOUR_BOUNDARY_STRING
    Content-Disposition: form-data; name="photo"; filename="calm.jpg"
    Content-Type: image/jpeg

    YOUR_IMAGE_DATA_GOES_HERE
    --YOUR_BOUNDARY_STRING
    Content-Disposition: form-data; name="message"

    My first message
    --YOUR_BOUNDARY_STRING
    Content-Disposition: form-data; name="user"

    1
    --YOUR_BOUNDARY_STRING

I’m sending over three variables: an image named photo, a string named message, and an integer named user. It’s important to note the linebreaks and the dashes before the boundary string. These must be included in order to build a good request. Now lets write some objective-c:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

NSString *boundary = @"YOUR_BOUNDARY_STRING";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData data];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n", self.message.photoKey] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n%@", self.message.message] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user\"\r\n\r\n%d", 1] dataUsingEncoding:NSUTF8StringEncoding]];

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[request setHTTPBody:body];

Now all we need to do is make a connection to the server and send the request:

[request setHTTPBody:body];
1
2
3
4
NSURLResponse *response;
NSError *error;

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

参考资料:

备注:欢迎转载,但请一定注明出处! http://blog.wangruofeng007.com