2

当我尝试请求此 URL 时,我无法获取 HTML 源代码:http ://www.google.co.in/search?q=search

NSURL *url = [NSURL URLWithString:@"http://www.google.co.in/search?q=search"];

NSMutableURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
NSLog(@"Webdata : %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);

数据始终为 NULL。

4

5 回答 5

6

这有效:

NSURL *url = [NSURL URLWithString:@"http://www.google.co.in/search?q=search"]; 
NSString *webData= [NSString stringWithContentsOfURL:url]; 
NSLog(@"%@",webData);

但是如果你需要使用 NSURLConnection ,你应该异步使用它并实现 willSendRequest 方法来处理重定向。

- (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response;
于 2012-05-11T11:57:31.377 回答
4

简单版:

NSURL *TheUrl = [NSURL URLWithString:@"http://google.com"];

NSString *webData = [NSString stringWithContentsOfURL:TheUrl 
                              encoding:NSASCIIStringEncoding
                              error:nil];

NSLog(@"%@", webData);
于 2013-12-27T09:13:58.640 回答
1
You can get the data in following ways :-

1 .使用 NSUrlConnection 作为异步请求

2. Synchronous NSUrlConnection

NSURL *URL = [NSURL URLwithString:@"http://www.google.com"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:URL]; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

Simply to check
NSURL *URL = [NSURL URLwithString:@"http://google.com"]; 
NSString *webData= [NSString stringWithContentsOfURL:URL]; 
于 2012-05-11T12:07:19.463 回答
1

使用 GCD 的简单异步解决方案:

- (void)htmlFromUrl:(NSString *)url handler:(void (^)(NSString *html, NSError *error))handler
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        NSError *error;
        NSString *html = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSASCIIStringEncoding error:&error];
        dispatch_async(dispatch_get_main_queue(), ^(void){
            if (handler)
                handler(html, error);
        });
    });
}

然后

[self htmlFromUrl:@"http://stackoverflow.com" handler:^(NSString *html, NSError *error) {
    NSLog(@"ERROR: %@ HTML: %@", error, html);
}];
于 2016-07-07T20:08:31.233 回答
0

对于 HTML 源代码的异步获取,我推荐你使用AFNetworking

1)然后子类AFHTTPCLient,例如:

//WebClientHelper.h
#import "AFHTTPClient.h"

@interface WebClientHelper : AFHTTPClient{

}

+(WebClientHelper *)sharedClient;

@end

//WebClientHelper.m
#import "WebClientHelper.h"
#import "AFHTTPRequestOperation.h"

NSString *const gWebBaseURL = @"http://dummyBaseURL.com/";


@implementation WebClientHelper

+(WebClientHelper *)sharedClient
{
    static WebClientHelper * _sharedClient = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:gWebBaseURL]];
    });

    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url
{
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }

    [self registerHTTPOperationClass:[AFHTTPRequestOperation class]];
    return self;
}
@end

2) 异步请求 HTML 源代码,将此代码放在任何相关部分

NSString *testNewsURL = @"http://whatever.com";
    NSURL *url = [NSURL URLWithString:testNewsURL];
    NSURLRequest *request  = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operationHttp =
    [[WebClientHelper sharedClient] HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSString *szResponse = [[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding] autorelease];
         NSLog(@"Response: %@", szResponse );
     }
     failure:^(AFHTTPRequestOperation *operation, NSError *error)
     {
         NSLog(@"Operation Error: %@", error.localizedDescription);
     }];

    [[WebClientHelper sharedClient] enqueueHTTPRequestOperation:operationHttp];
于 2013-02-15T09:24:44.087 回答