我目前正在尝试在 iphone 应用程序中发送多部分/混合请求,但该服务始终返回 http 状态代码 500。我无权访问 Web 服务的源代码。在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,所以如果有其他问题,请告诉我。