NSURLConnection

<!--StartFragment -->
异步请求:
NSURL *_url=[NSURL URLWithString:@ http://192.168.1.150/test/index.jsp];
NSMutableURLRequest *_request= [[NSMutableURLRequest alloc] initWithURL:_url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];//[[NSMutableURLRequest alloc] initWithURL:_url ];
 [_request setHTTPMethod:@"GET"];
 [_request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
 
 _receivedata=[[NSMutableData alloc] initWithData:nil];
  NSURLConnection *_connection=[[NSURLConnection alloc] initWithRequest:_request delegate:self];
 while(!finished) {
  
  [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  
 }
 

 [_request release];
 
 //[_connection release];
 
要实现的代码方法:


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{

NSHTTPURLResponse *_response=(NSHTTPURLResponse *)response;

[_receivedata setLength:0];

}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[_receivedata appendData:data];

}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
finished=TRUE;

[connection release];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

[connection release];
NSLog(@"connection falid! Error-%@ ",[error localizedDescription]);
}

同步方法:

NSURL *_url=[NSURL URLWithString:@http://192.168.1.150/test/index.jsp];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];         
// 设置URL
[request setURL:_url];
// 设置HTTP方法
[request setHTTPMethod:@"GET"];
// 发 送同步请求, 这里得returnData就是返回得数据了
NSData *returnData = [NSURLConnection sendSynchronousRequest:request 
returningResponse:nil error:nil]; 
// 释放对象
[request release];

 

你可能感兴趣的:(html,jsp)