状态代码:400,ios中afnetworking3.0中的标题
这是来自postman客户端的屏幕截图。该api正在发挥作用。我的代码中的问题是如何设置响应和请求。 我使用AFNetworking 3.0在目标c中发出post请求,但请求-响应-错误请求(400) 这就是错误所在状态代码:400,ios中afnetworking3.0中的标题,ios,objective-c,iphone,afnetworking-2,afnetworking-3,Ios,Objective C,Iphone,Afnetworking 2,Afnetworking 3,这是来自postman客户端的屏幕截图。该api正在发挥作用。我的代码中的问题是如何设置响应和请求。 我使用AFNetworking 3.0在目标c中发出post请求,但请求-响应-错误请求(400) 这就是错误所在 { status code: 400, headers { "Cache-Control" = private; "Content-Length" = 1647; "Content-Type" = "text/html"; Date = "Sat,
{ 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
我也试过这样做
-(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)
reponse=={URL:http://virtuzo.in/AhprepaidTestService/AhprepaidAPI_Json.svc/ValidateLoginService }{状态代码:400,标题{
“缓存控制”=专用;
“内容长度”=1647;
“内容类型”=“文本/html”;
日期=“2016年4月14日星期四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[79163:1281702]错误==(空)
原因是您得到的web服务响应实际上不是JSON响应。检查您得到的响应,这将有助于您的代码工作 尝试不同的响应序列化程序。答复的格式是什么?它是字符串还是json格式的?根据该设置响应序列化程序。您可以在不同的工具中检查您的响应,如AdvanceRESTClient、postman,或者您可以询问api或web服务的开发者
更新:
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];
试试这样的。这是没有网络的本地方式。您将在完成处理程序中设置数据作为响应。如果您的响应是字符串,则将该数据转换为字符串。如果响应是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:
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];
您得到的是xml字符串,因此,序列化几乎没有完成
1) 下载。在你的项目中进行除鼠和投放。(当你构建时,你会在这个类中得到错误。每当你在这个类中得到错误注释(//)这一行。这是因为它没有使用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];
我在错误块中出错。如何检查响应RRY dude,我错误地更新了你的答案而不是我的:(嘿,我已经更新了我的答案。检查一下。希望这会有所帮助。我已经使用了u所说的,并发布了带有参数和url的代码。仍然得到相同的错误您在完成处理程序中的数据中得到了什么?您的数据为零吗?按数据检查数据的长度。长度。第二件事完成处理程序中的错误描述是什么?您可以按错误进行检查。localizedDescription。您要发送什么参数以及如何发送?告诉我AlsonDictionary*mapData=[[NSDictionary alloc]initWithObjectsAndKeys:@“admin”@“UserName”@“1234”@“Pwd”,nil];这是我要发送的参数,我的问题中有完整的代码