iOS使用HTTPPOST上传图像和文本我是iOS的新手,我正在尝试用以下方式上传图片和文本multi-part form encoding在iOS。这个curl等价物是这样的:curl -F "param1=value1" -F "param2=@testimage.jpg" "http://some.ip.address:5000/upload"这个curl命令中返回预期的正确响应。JSON.问题:我一直收到一个HTTP 400请求,这意味着我在编写HTTPPOST主体时做错了什么。我所做的:作为参考,我试过FlickrAPIIOS应用程序“帖子大小太大了!”和目标C:如何使用HTTPPOST上传图像和文本?..但是,我一直得到一个HTTP 400。我试过ASIHttpRequest但是那里有一个不同的问题(回调没有被调用)。但是,我没有进一步研究这个问题,因为我听说开发人员已经停止支持这个库了:http://allseeing-i.com/[request_release];有人能帮帮我吗?
3 回答
白衣染霜花
TA贡献1796条经验 获得超10个赞
NSString *str=[NSString stringWithFormat:@"%@registration.php",appdel.baseUrl]; NSString *urlString = [NSString stringWithFormat:@"%@",str]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:urlString]]; [request setHTTPMethod:@"POST"]; NSMutableData *body = [NSMutableData data]; NSString *boundary = @"---------------------------14737809831466499882746641449"; NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary]; [request addValue:contentType forHTTPHeaderField: @"Content-Type"]; [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Disposition: form-data; name=\"file\"; filename=\"a.jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[NSData dataWithData:imgData]]; [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // parameter username [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"username\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[userName.text dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // parameter token [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"email\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[eMail.text dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // parameter method [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"pass\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[passWord.text dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; //parameter method NSLog(@"%@",countryCode); [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"country\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[countryCode dataUsingEncoding:NSUTF8StringEncoding]]; [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; // close form [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; // setting the body of the post to the reqeust [request setHTTPBody:body]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; // NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]; NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil]; Nslog(@"%@",dict); //
- 3 回答
- 0 关注
- 747 浏览
添加回答
举报
0/150
提交
取消