IOS:使用多部分/表单数据时获取“400错误请求”

IOS:使用多部分/表单数据时获取“400错误请求”,ios,iphone,jboss7.x,afnetworking,resteasy,Ios,Iphone,Jboss7.x,Afnetworking,Resteasy,我是新的IOS开发者,我面临着一个头痛的问题,关于使用multipart上传文件和一些文本,我已经尝试了很多 状态代码始终返回400。我厌倦了用另一种方法来测试我的web服务,比如用HttpComponent API构建Rest客户端,使用RestEASY客户端,这两种方法都很成功,真有趣 我使用的是Xcode 6.1.1,我的源代码: -(void)uploadPhoto { NSMutableURLRequest *request = [[NSMutableURLRequest alloc]

我是新的IOS开发者,我面临着一个头痛的问题,关于使用multipart上传文件和一些文本,我已经尝试了很多

状态代码始终返回400。我厌倦了用另一种方法来测试我的web服务,比如用HttpComponent API构建Rest客户端,使用RestEASY客户端,这两种方法都很成功,真有趣

我使用的是Xcode 6.1.1,我的源代码:

-(void)uploadPhoto {
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost/my-rest-service"]];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:@"POST"];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"avatar_temp"], 1.0);
NSString *boundary = @"12345-6789-abc"; //
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
[body appendData:[[NSString stringWithFormat:@"%@--", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@\r\n\r\n", @"type"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@\r\n", @"HELLO"] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:@"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];

[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if(data.length > 0){
//success
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"[String] %@", str);
}
}];
}
任何建议都有助于我的调查

谢谢


您忘记添加图像数据,因此添加:

       [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]];
一个完整的工作示例:

       [request setHTTPMethod:@"POST"];
        NSData *imageData = UIImageJPEGRepresentation(imageToSave,0.9);     //change imageToSave with your own UIImage defined
        NSString *filenames = @"first field";
        NSString *token = @"second field";
        NSString *boundary = @"---------------------------14738723651466499882746641449";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

        NSMutableData *body = [NSMutableData data];
        ////+++  first form field for post -> for example a field called filenames see NSString above
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"filenames\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[filenames dataUsingEncoding:NSUTF8StringEncoding]];
        ////--- the second field for form post-> we named token see above NSString

        ////+++
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"token\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[token dataUsingEncoding:NSUTF8StringEncoding]];
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
        ////---

        ////+++ the file used to upload /post

        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"ceva.jpg\"\r\n"] 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]];
        ////---

        // setting the body of the post to the reqeust
        [request setHTTPBody:body];            // now lets make the connection to the web

现在,您只需要在服务器端检查php代码

向我们展示您用于上传的代码。嗨!我刚刚更新了内容