Get Web Service Content

I have a url like here . When I enter it into the Safari address bar, I see a result, such as "Error" or "OK."

So, how to properly call this URL from my code and get this result as a string?

I tried it with NSURLConnection, NSURLRequestand NSURLResponse, but my answer object is always nil.

0
source share
1 answer

The "response" in these classes refers to the protocol response (HTTP headers, etc.), and not to the content.

To get content, you have several options:

  • NSURLConnection : NSURLConnection
  • NSURLConnection :

    // Error checks omitted
    NSURL *URL = [NSURL URLwithString:@"http://www.myserver.com/myservice.php?param=foobar"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];
    NSData *data = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:nil
                                                     error:nil];
    
  • [NSString stringWithContentsOfURL:]

    NSURL *URL = [NSURL URLwithString:@"http://www.myserver.com/myservice.php?param=foobar"];
    NSString *content = [NSString stringWithContentsOfURL:URL];
    

, 2 3 , , .

+1

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


All Articles