OStack程序员社区-中国程序员成长平台

标题: ios - NSURLSession 操作方法 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 12:57
标题: ios - NSURLSession 操作方法

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

-(NSString *)signInNSString *)username passwordNSString *)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"OST"];
[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)URLSessionNSURLSession *)session dataTask:    (NSURLSessionDataTask *)dataTask didReceiveDataNSData *)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" = "OST, 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";
} }



Best Answer-推荐答案


这行代码不适合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]];

您应该将您的网址与查询分开,然后将查询作为数据发布到正文。

类似:

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];

关于ios - NSURLSession 操作方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34454714/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4