我目前正在尝试在iphone应用程序中发送多部分/混合请求,但该服务始终返回http状态代码500.我无权访问webservice的源代码。在android中,注册服务被称为:
HttpPost httpost = new HttpPost(PlayerUrl.REGISTER_URL);
httpost.setHeader("Content-Type", "multipart/mixed");
MultipartEntity entity = new MultipartEntity();
String jsonData = "{\"firstName\":\"abc\",\"lastName\":\"xyz\",\"email\":\"abc@xyz.com\",\"password\":\"abc\",\"inviteType\":\"1\"}";
StringBody jsonBean = new StringBody(jsonData, "application/json", null);
entity.addPart("user", jsonBean);
entity.addPart("image", new FileBody(new File(
"/data/face.png"), "face.png",
"application/octet-stream", null));
httpost.setEntity(entity);
HttpResponse response = client.execute(httpost);
System.out.println(response);
但是,如何在iPhone中做到这一点我不知道。我搜索了整整一天,无法实现任何目标。我目前有以下代码:
request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:method];
//build body
NSMutableData *bodyForRequest = [NSMutableData data];
//NSString *jsonData = [NSString stringWithFormat:@"%@", dataForJson];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"91473780983146649988274664144";
NSString *contentType = [NSString stringWithFormat:@"multipart/mixed; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
[request addValue:@"1.0" forHTTPHeaderField: @"MIME-Version"];
[bodyForRequest appendData:[[NSString stringWithFormat:@"If you can see this MIME than your client doesn't accept MIME types!\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyForRequest appendData:[@"Content-Type: application/json;name = \"user\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyForRequest appendData:[@"Content-Transfer-Encoding: 7bit\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//[bodyForRequest appendData:[@"Content-ID: user\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//[bodyForRequest appendData:[[NSString stringWithFormat:@"Content-Disposition: name=\"user\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyForRequest appendData:dataForJson];
[bodyForRequest appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyForRequest appendData:[@"Content-Type: image/jpg; name=\"image\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[bodyForRequest appendData:[@"Content-Transfer-Encoding: base64\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//[bodyForRequest appendData:[@"Content-ID: image\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
NSString *str=[NSString stringWithFormat:@"Content-Disposition: attachment ;file=\"image.jpg\";\r\n\r\n"];
[bodyForRequest appendData:[[NSString stringWithString:str] dataUsingEncoding:NSUTF8StringEncoding]];
[bodyForRequest appendData:[NSData dataWithData:dataForImage]];
[bodyForRequest appendData:[[NSString stringWithFormat:@"--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:bodyForRequest];
// save request start time
requestStartTime = [CNUtility currentTimeInMilliseconds];
// _connection the request now
NSLog(@"%@...request...%@",self,request);
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[_connection start];
我的要求是将JSON数据和个人资料图片一起发送到服务器请帮帮我或者请给我一些可以帮助我的东西。
此外,我不允许使用ASI,所以如果有其他内容,请告诉我。
答案 0 :(得分:0)
尝试使用AFNetworking让您的生活更轻松,您的代码看起来就像这个与您相关的重要部分以粗体显示
AFHTTPClient *httpClient = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", kUrl]]];
重要的一部分 - 开始
NSMutableURLRequest *afRequest = [httpClient multipartFormRequestWithMethod:@"POST"
path:[NSString stringWithFormat:@"link?access_token=%@", kAPIToken]
parameters:nil constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
{
[formData appendPartWithFileData:videoData name:@"link[data]" fileName:@"filename.mov" mimeType:@"video/quicktime"];
NSString *tagStr = @"#partytrain";
NSData *tagData = [tagStr dataUsingEncoding:NSUTF8StringEncoding];
[formData appendPartWithFormData:tagData name:@"link[text]"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:afRequest];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite)
{
//use a progress indicator here
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success");
[picker dismissViewControllerAnimated:YES completion:nil];
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {NSLog(@"error: %@", operation.responseString);}];
[operation start];