将POST JSON POST到REST Web服务,最后是错误:请求失败:错误请求(400)这是我在iOS端使用的代码:
-(IBAction)createUser:(id)sender {
NSDictionary *parameters = @{@"username":@"username", @"password":@"pass"};
AFHTTPRequestOperationManager *createUserManager = [AFHTTPRequestOperationManager manager];
createUserManager.securityPolicy.allowInvalidCertificates = YES;
AFJSONRequestSerializer *reqSerializer = [AFJSONRequestSerializer serializer];
[reqSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[reqSerializer setValue:@"text/plain" forHTTPHeaderField:@"Accept"];
createUserManager.requestSerializer = reqSerializer;
AFJSONResponseSerializer *resSerializer = [AFJSONResponseSerializer serializer];
[resSerializer setReadingOptions:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers];
resSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", @"application/json", nil];
createUserManager.responseSerializer = resSerializer;
NSString *urlString = @"http://.../Service1.svc/createUser";
[createUserManager POST:urlString parameters:parameters success:^(AFHTTPRequestOperation *operationAllPosts, id responseObjectAllPosts) {
if ([responseObjectAllPosts isKindOfClass:[NSArray class]]) {
NSArray *responseArray = responseObjectAllPosts;
NSLog(@"responseArray %@", responseArray);
/* do something with responseArray */
} else if ([responseObjectAllPosts isKindOfClass:[NSDictionary class]]) {
NSDictionary *responseDict = responseObjectAllPosts;
NSLog(@"responseDict %@",responseDict);
/* do something with responseDict */
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", [error debugDescription]);
NSLog(@"Error: %@", [error localizedDescription]);
NSLog(@"operation %@", operation.response);
}];
}
使用相同的逻辑,但GET方法正在起作用,我做错了什么?