ios UIWebView用NSURLSessionDataTask获取请求返回的状态码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cjh16102377/article/details/50324383
// 创建Data Task
    NSURL *url = [NSURL URLWithString:@"http://blog.csdn.net"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
                                                completionHandler:
                                      ^(NSData *data, NSURLResponse *response, NSError *error) {
                                          // 输出返回的状态码,请求成功的话返回为200
                                          [self showResponseCode:response];
                                          
                                          // 在webView中加载数据
                                          [self.webView loadData:data
                                                        MIMEType:@"text/html"
                                                textEncodingName:@"utf-8"
                                                         baseURL:nil];
                                          
                                      }];
    // 使用resume方法启动任务
    [dataTask resume];

/* 输出http响应的状态码 */
- (void)showResponseCode:(NSURLResponse *)response {
    NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
    NSInteger responseStatusCode = [httpResponse statusCode];
    NSLog(@"%d", responseStatusCode);
}


没有更多推荐了,返回首页