IOS:得到错误请求' 400错误请求'使用multipart / form-data时

时间:2015-03-26 08:59:32

标签: ios iphone jboss7.x afnetworking resteasy

我是新的IOS开发人员,我正面临着使用multipart上传文件和一些文本的头痛问题,我在here

尝试了很多

状态代码总是返回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);
}
}];
}

任何建议都有助于我的调查

由于

这是

2 个答案:

答案 0 :(得分:0)

您忘记添加imageData,因此请添加:

       [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代码。

答案 1 :(得分:-1)

实际上我在RestEasy和Jboss AS 7上创建了Rest服务。我想发送文件和文本,这就是我只用文本数据进行测试的原因。使用PHP代码服务,一切都很好,但只有Java问题。我的休息服务定义:

@POST
@Consumes("multipart/form-data")
@Produces("application/json")
public Response create(@Context HttpHeaders httpHeaders
        , MultipartFormDataInput multipartFormDataInput) {...}

你觉得怎么样?