这是邮递员客户端的屏幕截图。该 api 正在处理我的代码中的问题,如何设置响应和请求。我正在使用 AFNetworking 3.0 在目标 c 中发出发布请求,但请求响应错误请求(400)这是错误
{ status code: 400, headers {
"Cache-Control" = private;
"Content-Length" = 1647;
"Content-Type" = "text/html";
Date = "Sat, 09 Apr 2016 15:49:32 GMT";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} }, NSErrorFailingURLKey=http://mytesturl_Json.svc/ValidateLoginService, com.alamofire.serialization.response.error.data=<efbbbf3c 3f786d6c
这是发出帖子请求的代码
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:@"http://mytesturl_Json.svc/ValidateLoginService" parameters:inputs progress:nil success:^(NSURLSessionTask *task, id responseObject) {
NSLog(@"JSON: %@", responseObject);
} failure:^(NSURLSessionTask *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
我也试过这样
-(void)postdata
{
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"http://virtuzo.in/AhprepaidTestService/AhprepaidAPI_Json.svc/ValidateLoginService"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPMethod:@"POST"];
NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"admin", @"UserName",
@"1234", @"Pwd",
nil];
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
[request setHTTPBody:postData];
NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"reponse==%@",response);
NSLog(@"error==%@",error);
}];
[postDataTask resume];
}
我仍然收到同样的错误
reponse==<NSHTTPURLResponse: 0x7f9baa433990> { URL: http://virtuzo.in/AhprepaidTestService/AhprepaidAPI_Json.svc/ValidateLoginService } { status code: 400, headers {
"Cache-Control" = private;
"Content-Length" = 1647;
"Content-Type" = "text/html";
Date = "Thu, 14 Apr 2016 02:57:21 GMT";
Server = "Microsoft-IIS/7.5";
"X-AspNet-Version" = "4.0.30319";
"X-Powered-By" = "ASP.NET";
} }
2016-04-14 08:27:22.833 Ahprepaid[79163:1281702] error==(null)