服务器认为NSMutableURLRequest“POST”参数是“GET”

时间:2015-06-02 02:13:51

标签: ios objective-c post http-headers nsurlsession

我在使用NSMutableURLRequest的iOS应用中遇到问题。我将标题设置为“POST”,并将字典加载到请求中,但我在响应中返回错误页面,并检查服务器日志显示消息:

"org.springframework.web.HtpRequestMethodNotSupportedEception: Request method 'GET' not supported"

服务器端代码已正确设置,因为它已经工作了很长时间而没有任何更改。我正在将客户端从RestKit实现更改为使用本机iOS apis,例如NSURLSessionConfiguration,NSURLSession,NSURLSessionUploadTask和NSMutableURLRequest。

下面是代码,您可以在其中看到我使用“Post”参数设置了URL请求。知道我做错了什么吗?

            //Set the url string to send post for products
            NSString* command = [NSString stringWithFormat:@"%@/mobile/%@/addProductJson.do", [configuration serverUrl],[configuration authKey]];

            // Create the actual url
            NSURL *myUrl = [NSURL URLWithString:command];

            NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
            NSURLSession *session = [NSURLSession sessionWithConfiguration:config];

            [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;


            // Create the request and set the header to post
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:myUrl];
            [request setHTTPMethod:@"POST"];


            //Create dictionary for uploads
            NSMutableDictionary* upcDict = [[NSMutableDictionary alloc]init];


            for(NSManagedObject *matches in arrScans)
            {
                [arrUp addObject: [matches valueForKey:@"upc"]];
            };


            [upcDict setObject:arrUp forKey:@"upc"];
            [upcDict setObject:@"myMobileList" forKey:@"listName"];


            NSError* error = nil;

            // create the data to be sent
            NSData* jsonData = [NSJSONSerialization dataWithJSONObject:upcDict options:0 error:&error];

            if (!error)
            {
                NSURLSessionUploadTask *uploadTask =
                    [session uploadTaskWithRequest:request
                            fromData:jsonData completionHandler:^(NSData *data,NSURLResponse *response,NSError *error)
                {
}];
                    if (!error) {
                        NSLog(@"success");

                    } else {
                        NSLog(@"No");
                    }


                    [uploadTask resume];
            }
            else
            {
                NSLog(@"JSON error");
            }
            [upcDict release];
        }

一个重要的附录 - 使用以下声明打印标题:

NSLog(@"all request fields: %@ ", [request allHTTPHeaderFields]);

显示一个空数组。

0 个答案:

没有答案