1.
connecttion = [ NSURLConnection connectionWithRequest:[ NSURLRequest requestWithURL:[ NSURL URLWithString:@"http://www.baidu.com" ] ]
delegate:self ];
receivedData=[[NSMutableData data] retain];
2.
#pragma mark NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// this method is called when the server has determined that it
// has enough information to create the NSURLResponse
// it can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.
// receivedData is declared as a method instance elsewhere
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
NSString *stringFromUnichar = [NSString stringWithCharacters:(unichar*)[data bytes] length:[data length] / sizeof(unichar)];
NSLog(stringFromUnichar);
return;
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Succeeded! Received %d bytes of data",[receivedData length]);
NSString *stringFromUnichar = [NSString stringWithCString:(char*)[receivedData bytes] length:[receivedData length] / sizeof(char)];
NSLog(stringFromUnichar);
[ receivedData release ];
}