Get content of webservice

前端 未结 1 529
忘了有多久
忘了有多久 2021-01-28 07:04

I\'ve got an URL like here. When I type that into Safari\'s address bar I see an result like \"Error\" or \"OK\".

So, how do I properly call that URL from within my code

1条回答
  •  后悔当初
    2021-01-28 07:44

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

    To get the content, you have a few options:

    1. Use NSURLConnection in asynchronous mode: Using NSURLConnection
    2. Use NSURLConnection in synchronous mode:

      // 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];
      
    3. Use [NSString stringWithContentsOfURL:]

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

    Of course, you should use options 2 and 3 only if your content will be really small in size, to maintain responsiveness.

    0 讨论(0)
提交回复
热议问题