IOS 网络连接 总结

NSMutableData* buf = [[NSMutableData alloc] initWithLength:0];
NSURLConnection* connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
 
// 收到响应时, 会触发
- (void)connection:(NSURLConnection *)aConnection didReceiveResponse:(NSURLResponse *)aResponse;
// 你可以在里面判断返回结果, 或者处理返回的http头中的信息
 
// 每收到一次数据, 会调用一次
- (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data;
// 因此一般来说,是
- (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data
{
    [buf appendData:data];
}
// 当然buffer就是前面initWithRequest时同时声明的.
 
// 网络错误时触发
- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error;
 
// 全部数据接收完毕时触发
- (void)connectionDidFinishLoading:(NSURLConnection *)aConn;


// 初始化請求
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];         
// 設置URL
        [request setURL:[NSURL URLWithString:urlStr]];
// 設置HTTP方法
        [request setHTTPMethod:@"GET"];
// 發送同步請求, 這裡得returnData就是返回得數據楽
        NSData *returnData = [NSURLConnection sendSynchronousRequest:request 
                                                   returningResponse:nil error:nil]; 
// 釋放對象
        [request release];
===================================================================
2、

你可能感兴趣的:(IOS 网络连接 总结)