0
我目前正在嘗試在iphone應用程序中發送多部分/混合請求,但服務始終返回http狀態代碼500.我無權訪問web服務的源代碼。在Android的註冊服務被稱爲像:multipart/Iphone中混合應用
HttpPost httpost = new HttpPost(PlayerUrl.REGISTER_URL);
httpost.setHeader("Content-Type", "multipart/mixed");
MultipartEntity entity = new MultipartEntity();
String jsonData = "{\"firstName\":\"abc\",\"lastName\":\"xyz\",\"email\":\"[email protected]\",\"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,所以如果有其他事情請告訴我。
有你嘗試使用網絡多部分/混合?我在下面提供了一個例子。 – 2013-05-03 15:41:32