NSURLErrorDomain Code -1002 download pdf

I am trying to cache a webpage which I can then show using UIWebView.

I have a corresponding NSURLSessionDataTask inside a for loop (trying to cache 6 web pages) inside another NSURLSessionDataTask termination NSURLSessionDataTask . When I run, I keep getting this error:

Ayy there was error downloading, data:<> response:(null) error:Error Domain=NSURLErrorDomain Code=-1002 "The operation couldn't be completed. (NSURLErrorDomain error -1002.)" UserInfo=0xdd89d30 {NSUnderlyingError=0xdd89ba0 "The operation couldn't be completed. (kCFErrorDomainCFNetwork error -1002.)"}

Here is a snippet of what I call

 for (MAClass *class in [myDictResult objectForKey:@"classes"]) { NSString *PRURL = [[[class assignments] objectAtIndex:[[class assignments] count]-1] assignmentName]; NSLog(@"PRURL is %@", PRURL); NSURLSessionDataTask *progressReportTask = [defaultSession dataTaskWithURL:[NSURL URLWithString:PRURL] completionHandler:^(NSData *progressReportData, NSURLResponse *progressReportResponse, NSError *progressReportError) { if ([progressReportData length] > 0 && progressReportError == nil) { NSLog(@"got dat data"); } else NSLog(@"Error with getting data data:%@\nresponse:%@\nerror:%@", progressReportData, progressReportResponse, progressReportError); }]; [progressReportTask resume]; NSLog(@"After request"); } 

I made sure that the url is valid as it caused other people to get the same error (my urls are like https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/PrintProgressReport/20152193^HS4 , which are valid when I put them in the browser)

What am I doing wrong?

+6
source share
2 answers

-1002 NSURLErrorUnsupportedURL / kCFURLErrorUnsupportedURL . In the future, you can either search the Xcode documentation for NSURLErrorDomain , or use the quick open ( shift + command + O ) so that browsers display headers for determining NSURLErrorDomain . Any of these methods would let you find that -1002 in NSURLErrorDomain is equal to NSURLErrorUnsupportedURL .

The reason for this error is that your URL contains some characters that should be skipped. And web browsers will often do the necessary percentage acceleration for you, so it works there.

You can use stringByAddingPercentEscapesUsingEncoding to convert the URL to an acceptable format:

 NSString *urlString = @"https://mistar.oakland.k12.mi.us/novi/StudentPortal/Home/PrintProgressReport/20152193^HS4"; NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSURLSessionTask *task = [defaultSession dataTaskWithURL:url completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) { ... }]; 

By the way, when comparing the results of a web browser with Charles, it’s very useful. Run the query from the browser and again from the application and compare the results in Charles. If you compared them, you would see that you need the percentage code not to get into the URL.

By the way, you can also refer to Section 2 of RFC 3986 for a technical description of which characters in URLs should be omitted as a percentage.

+16
source

If your urlString contains a query string, also consider using NSURLQueryItem to build a queryString.

It will create the URL in its acceptable format.

Here's an example of how to use it: Creating URLs using NSURLQueryItems and NSURLComponents .

+1
source

Source: https://habr.com/ru/post/970518/


All Articles