I got a wrong post(php:echo $_POST) like below: Array ( [email"aa@qq_com] => -----------------------------14737809831466499882746641449 Content-Disposition: form-data; name="password" c8837b23ff8aaa8a2dde915473ce0991 )
我的代码:
// set header value , some random text that will never occur in the body
NSString *boundary = @"---------------------------14737809831466499882746641449"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; /* now lets create the body of the post */ NSMutableData *body = [NSMutableData data]; // email part
[body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="email"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[anEmail dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"
" dataUsingEncoding:NSUTF8StringEncoding]]; // password part [body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name="password"
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[aPassword dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"
" dataUsingEncoding:NSUTF8StringEncoding]]; // image part [body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat: @"Content-Disposition: form-data; name="uploadingImage"; filename=%@
", anImageName] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream
"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:aFileData]];
[body appendData:[@"
" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body]; // now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];