NSURLSession 操作指南

NSURLSession how-to

我正在尝试发送 post 请求以登录服务。我让它与 NSURLConnection 一起工作,但后来发现它已贬值,所以我尝试切换到 NSURLSession,但我不知道该怎么做,也不知道为什么我的代码不起作用。请注意,出于安全原因,部分代码已被删除。

-(NSString *)signIn:(NSString *)username password:(NSString *)password{
NSString *api_sig = [NSString stringWithFormat:@"%@methodauth.getMobileSessionpassword%@username%@thisismyapisignature", LASTFM_API_KEY, password, username];//API signature

NSString *post = [NSString stringWithFormat:@"https://ws.audioscrobbler.com/2.0/?method=auth.getMobileSession&api_key=apikey&format=json&username=%@&password=%@&api_sig=%@", username, password, [api_sig md5]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:post]
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

[request setHTTPMethod:@"POST"];
[request setHTTPBody:[post dataUsingEncoding:NSUTF8StringEncoding]];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSLog(@"response is %@", response);
}];
[task resume];

if (session) {
    NSLog(@"SUCESS");
}else{
    NSLog(@"FAILURE");
}
return nil;
}

-(void)URLSession:(NSURLSession *)session dataTask:    (NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data{
 NSLog(@"data is %@", data);
}

我在控制台中得到这个

2015-12-24 15:04:35.624 [21173:761903] SUCESS
2015-12-24 15:04:36.445 [21173:762056] response is <NSHTTPURLResponse: 0x7f8f3d0bd9d0> { URL: https://ws.audioscrobbler.com/2.0/?method=auth.getMobileSession&api_key=apikey&format=json&username=username&password=password&api_sig=apisig } { status code: 400, headers {
"Access-Control-Allow-Methods" = "POST, GET, OPTIONS";
"Access-Control-Allow-Origin" = "*";
"Access-Control-Max-Age" = 86400;
Connection = "keep-alive";
"Content-Length" = 58;
"Content-Type" = "application/json";
Date = "Thu, 24 Dec 2015 15:04:35 GMT";
Server = "openresty/1.7.7.2";
} }

这行代码不适合 post。

NSString *post = [NSString stringWithFormat:@"https://ws.audioscrobbler.com/2.0/?method=auth.getMobileSession&api_key=apikey&format=json&username=%@&password=%@&api_sig=%@", username, password, [api_sig md5]];

您应该将 url 与查询分开,然后 post 将查询作为数据发送到正文。

类似于:

NSURL * url = [NSURL URLWithString:@"https://ws.audioscrobbler.com/2.0/"];
NSString * post = [NSString stringWithFormat:@"method=auth.getMobileSession&api_key=someapikey&format=json&username=%@&password=%@&api_sig=%@", username, password, [api_sig md5]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:60.0];