我正在使用NSURLConnection来整合Web服务。
我的代码如下:
NSURL *url = [NSURL URLWithString:Sign_Up_URL];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
NSString *params = [NSString stringWithFormat:@"vimage=%@&@"vFname=%@&vLname=%@&vEmail=%@&vPassword=%@&vDeviceType=%@&vDeviceToken=%@",image_data,txt_first.text,txt_last.text,txt_email.text,txt_password.text,@"IOS",@"safasf423526dgdsgdsg"];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setValue:header_filed forHTTPHeaderField:@"X_API_KEY"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];
[NSURLConnection sendAsynchronousRequest:urlRequest
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *connectionError) {
// handle response
[MBProgressHUD hideHUDForView:self.view animated:YES];
NSDictionary *json_response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
NSLog(@"%@",json_response);
// [self login_response:json_response];
}];
但如何使用此参数上传图片?
为此提供帮助。
答案 0 :(得分:0)
请阅读文章:http://nthn.me/posts/2012/objc-multipart-forms.html,并使用ContentType: Multipart / from-data ;
供您参考:
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];
希望这会有所帮助,如果您收到任何错误,请更新我。
答案 1 :(得分:0)
使用NSURLConnection,您可以执行以下操作
NSData* fileData = [[NSData alloc] initWithContentsOfFile:image_data];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:Sign_Up_URL]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithString:@"0xKhTmLbOuNdArY"]; // This is important! //NSURLConnection is very sensitive to format.
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",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"VFname\"; filename=\"thefilename\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:fileData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"param2\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:self.emailtextfield.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"param3\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:self.passwordtextfield.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:self.vlnametextfield.text] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
(或使用ASIHTTPRequest,您可以按照以下方式执行)
ASIFormDataRequest *request=[ASIFormDataRequest requestWithURL:Sign_Up_URL];
[request setDelegate:self];
[request setPostValue:vnamelabel.textforKey:@"VFname"];
[request setPostValue:emaillabel.text forKey:@"vEmail"];
[request setPostValue:passwordlabel.text forKey:@"vPassword"];
[request setPostValue:devicetypelabel.text forKey:@"vDeviceType"];
[request addData:self.imagedata withFileName:@"image.jpeg" andContentType:@"image/jpeg" forKey:@"vimage"];
//----------------------
希望它可以帮助你..
答案 2 :(得分:-1)