状态代码:400,ios afnetworking3.0 中的 headers
status code: 400, headers in afnetworking3.0 in ios
这是来自 postman client 的屏幕截图。api 正在处理我的代码中的问题,如何设置响应和请求。
我正在使用 AFNetworking 3.0 在 objective c 中发出 post 请求,但请求响应错误请求 (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
这是发出 post 请求的代码
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)
原因是您得到的 Web 服务响应实际上不是 JSON 响应。检查您收到的响应,这将有助于您的代码工作!
尝试不同的响应序列化程序。响应格式是什么?它是字符串还是 json 格式或其他格式?根据该设置的响应序列化程序。您可以使用不同的工具(例如 advance rest client、postman)检查您的回复,或者您可以询问制作 api 或网络服务的人..!
更新:
Update :
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"[JSON SERVER"];
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: @"TEST IOS", @"name",
@"IOS TYPE", @"typemap",
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) {
}];
[postDataTask resume];
尝试这样的事情。这是没有 afnetworking 的原生方式。您将在完成处理程序中将数据作为响应。如果您的响应是字符串,则将该数据转换为字符串。如果以 json 格式响应,则在完成处理程序块中以 json 转换该数据。转换示例,
//data to string
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
/data to json object
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:myNSData options:kNilOptions error:&error]; // here two posibilitiew NSArray or NSDictionary (Try both ot you can use id)
希望这能解决您的问题..:)
更新 2:
NSString *urlString2 = @"http://virtuzo.in/AhprepaidTestService/AhprepaidAPI_Json.svc/ValidateLoginService?UserName=admin&Pwd=1234";
NSURL *myUrl = [NSURL URLWithString:urlString2];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myUrl];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str is %@",str);
}]resume];
这与邮递员的回复相同。我在我的演示中尝试过。我刚刚用 url.
附加了参数
更新 3:
您得到的是 xml 字符串,所以序列化起来有点复杂。
1) 下载XML Parser。拖放您的项目。 (当你构建时,你会在这个 class 中得到错误。每当你在这个 class 中得到错误注释(//)这一行时,这是因为它没有使用 ARC)。
更新代码:
NSString *urlString2 = @"http://virtuzo.in/AhprepaidTestService/AhprepaidAPI_Json.svc/ValidateLoginService?UserName=admin&Pwd=1234";
NSURL *myUrl = [NSURL URLWithString:urlString2];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myUrl];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str is %@",str);
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:str error:&parseError];
NSLog(@" %@", xmlDictionary);
NSString *myfinalString = [[xmlDictionary valueForKey:@"string"]valueForKey:@"text"];
NSLog(@"string is %@",myfinalString);
NSError *jsonError;
NSData *objectData = [myfinalString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
NSLog(@"json is : %@",json);
}]resume];
{ 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
这是发出 post 请求的代码
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)
原因是您得到的 Web 服务响应实际上不是 JSON 响应。检查您收到的响应,这将有助于您的代码工作!
尝试不同的响应序列化程序。响应格式是什么?它是字符串还是 json 格式或其他格式?根据该设置的响应序列化程序。您可以使用不同的工具(例如 advance rest client、postman)检查您的回复,或者您可以询问制作 api 或网络服务的人..!
更新:
Update :
NSError *error;
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:@"[JSON SERVER"];
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: @"TEST IOS", @"name",
@"IOS TYPE", @"typemap",
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) {
}];
[postDataTask resume];
尝试这样的事情。这是没有 afnetworking 的原生方式。您将在完成处理程序中将数据作为响应。如果您的响应是字符串,则将该数据转换为字符串。如果以 json 格式响应,则在完成处理程序块中以 json 转换该数据。转换示例,
//data to string
NSString *myString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
/data to json object
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:myNSData options:kNilOptions error:&error]; // here two posibilitiew NSArray or NSDictionary (Try both ot you can use id)
希望这能解决您的问题..:)
更新 2:
NSString *urlString2 = @"http://virtuzo.in/AhprepaidTestService/AhprepaidAPI_Json.svc/ValidateLoginService?UserName=admin&Pwd=1234";
NSURL *myUrl = [NSURL URLWithString:urlString2];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myUrl];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str is %@",str);
}]resume];
这与邮递员的回复相同。我在我的演示中尝试过。我刚刚用 url.
附加了参数更新 3:
您得到的是 xml 字符串,所以序列化起来有点复杂。
1) 下载XML Parser。拖放您的项目。 (当你构建时,你会在这个 class 中得到错误。每当你在这个 class 中得到错误注释(//)这一行时,这是因为它没有使用 ARC)。
更新代码:
NSString *urlString2 = @"http://virtuzo.in/AhprepaidTestService/AhprepaidAPI_Json.svc/ValidateLoginService?UserName=admin&Pwd=1234";
NSURL *myUrl = [NSURL URLWithString:urlString2];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myUrl];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str is %@",str);
NSError *parseError = nil;
NSDictionary *xmlDictionary = [XMLReader dictionaryForXMLString:str error:&parseError];
NSLog(@" %@", xmlDictionary);
NSString *myfinalString = [[xmlDictionary valueForKey:@"string"]valueForKey:@"text"];
NSLog(@"string is %@",myfinalString);
NSError *jsonError;
NSData *objectData = [myfinalString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData
options:NSJSONReadingMutableContainers
error:&jsonError];
NSLog(@"json is : %@",json);
}]resume];