NSURL *url = [NSURL URLWithString:@"http://example.com/form/"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
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:@"
--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="photo"; filename="%@.jpg"
", self.message.photoKey] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream
" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"
--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="message"
%@", self.message.message] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"
--%@
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name="user"
%d", 1] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@" --%@ ", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request setHTTPBody:body];
NSURLResponse *response;
NSError *error;
nsdata* data=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
//post example
NSURL *baseurl = [NSURL URLWithString:@"http://115.29.137.123:8080/microbiz/api/v2/userstrack/post"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:baseurl];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"0xKhTmLbOuNdArY";
NSMutableData *body = [NSMutableData data];
NSMutableDictionary *d= [NSMutableDictionary new];
[d setObject:[NSNumber numberWithInt:30] forKey:@"userId"];
[d setObject:@"9802140982155" forKey:@"imei"];
[d setObject:@"a78714d18d9d49b5a7ee549c4cca40fe" forKey:@"token"];
[d setObject:[NSNumber numberWithInt:12] forKey:@"action"];
[d setObject:[NSNumber numberWithDouble:1] forKey:@"longitude"];
[d setObject:[NSNumber numberWithDouble:1] forKey:@"latitude"];
[d setObject:@"a" forKey:@"address"];
[d enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSString *thisFieldString = [NSString stringWithFormat:
@"--%@
Content-Disposition: form-data; name="%@"
%@",
boundary, key, obj];
[body appendData:[thisFieldString dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"
" dataUsingEncoding:NSUTF8StringEncoding]];
}];
[body appendData: [[NSString stringWithFormat:@"--%@--
", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[[NSString stringWithFormat:@"
%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSString *charset = (__bridge NSString *)CFStringConvertEncodingToIANACharSetName(CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding));
[request setValue:[NSString stringWithFormat:@"multipart/form-data; charset=%@; boundary=%@", charset, boundary]
forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%lu", (unsigned long) [body length]] forHTTPHeaderField:@"Content-Length"];
DLog(@"body_string:%@", [[NSString alloc]initWithData:body encoding:NSUTF8StringEncoding]);
[request setHTTPBody:body];
NSURLResponse *response;
NSError *error;
NSData*data= [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
DLog(@"request.allHTTPHeaderFields:%@",request.allHTTPHeaderFields);
DLog(@"data:%@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] );