我在 iOS 中看到的执行简单 GET HTTP 连接的标准示例是:
NSString *finalURLstring = @"http://www.somesite.com?value=2";
NSURL *url = [NSURL URLWithString:finalURLstring];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
然后实现这些功能:
-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
-(void) connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
-(void) connectionDidFinishLoading:(NSURLConnection*)connection
如果你有多个连接,你只需创建多个 NSURLConnections 并在 connectionDidFinishLoading 中比较它们:
if (connection == urlConnection1)
// do something
else if (connection == urlConnection2)
// do something else
这个问题是你每次都创建一个到服务器的新连接,对吗?是否可以创建您的 NSURLConnection 并重用它?即保持连接打开,这样您就可以进行多次调用,而不是在每次调用服务器时创建新连接?
谢谢你的帮助,
-大卫