我已经使用POST方法在我的应用程序中使用标题值和params来调用API。 但在我的后端无法获得我的标头值进行身份验证。即使我的回复也有无效的用户。我附上了我的片段。你能不能帮我解决这个问题。
NSString *post = [NSString stringWithFormat:@"test=Message"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[request setURL:[NSURL URLWithString:@"http://myURL.com/FakeURL"]];
[request setHTTPMethod:@"POST"];
NSString *authStr=[NSString stringWithFormat:@"userName:xxx"];
NSData *authData=[authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *headerValue=[NSString stringWithFormat:@"Basic %@",[authData base64EncodedStringWithOptions:0]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setValue:headerValue forHTTPHeaderField:@"Authorization"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
我的回答是:
2017-04-10 16:43:35.635 Testing[5155:172935] ST :200
2017-04-10 16:43:35.636 API_Testing_AeroVoyce[5155:172935] Error :(null)
2017-04-10 16:43:35.638 API_Testing_AeroVoyce[5155:172935] requestReply :invalid user
答案 0 :(得分:1)
为授权,网址和内容长度创建单个网址请求:
您在Request HeaderField中缺少内容长度。
- (void)postRequest
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
NSString *post = [NSString stringWithFormat:@"test=Message"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
[request setURL:[NSURL URLWithString:@"http://myURL.com/FakeURL"]];
[request setHTTPMethod:@"POST"];
NSString *authStr=[NSString stringWithFormat:@"userName:xxx"];
NSData *authData=[authStr dataUsingEncoding:NSUTF8StringEncoding];
NSString *headerValue=[NSString stringWithFormat:@"Basic %@",[authData base64EncodedStringWithOptions:0]];
[request setValue:headerValue forHTTPHeaderField:@"Authorization"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"requestReply: %@", requestReply);
}];
[task resume];
}
确保您已添加NSURLSessionDataTask以继续。
另请检查您是否在info.Plist中启用了应用传输安全性。
这有助于解决您的问题。
编辑:
尝试这样做:
if(data!= nil)
{
id JSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSLog(@"Response === %@",JSON);
}
而不是:
NSString * requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];