[关闭]
@SanMao 2015-08-09T17:53:44.000000Z 字数 3271 阅读 1446

NSURLConnection

网络


常用类

NSURLConnection的使用

NSURLConnection发送请求

  1. + (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;
  1. + (void)sendAsynchronousRequest:(NSURLRequest*) request queue:(NSOperationQueue*) queue completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handler;
  1. - (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate;
  2. + (NSURLConnection*)connectionWithRequest:(NSURLRequest *)request delegate:(id)delegate;
  3. - (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately;
  1. // 开始接收到服务器的响应时调用
  2. - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
  3. // 接收到服务器返回的数据时调用(服务器返回的数据比较大时会调用多次)
  4. - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
  5. // 服务器返回的数据完全接收完毕后调用
  6. - (void)connectionDidFinishLoading:(NSURLConnection *)connection;
  7. // 请求出错时调用(比如请求超时)
  8. - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;

NSMutableURLRequest

  1. // 设置请求超时等待时间(超过这个时间就算超时,请求失败)
  2. - (void)setTimeoutInterval:(NSTimeInterval)seconds;
  3. // 设置请求方法(比如GET和POST)
  4. - (void)setHTTPMethod:(NSString *)method;
  5. // 设置请求体
  6. - (void)setHTTPBody:(NSData *)data;
  7. // 设置请求头
  8. - (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;

创建GET和POST请求

  1. NSString *urlStr = [@"http://120.25.226.186:32812/login?username=123&pwd=123" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  2. NSURL *url = [NSURL URLWithString:urlStr];
  3. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  1. NSString *urlStr = @"http://120.25.226.186:32812/login";
  2. NSURL *url = [NSURL URLWithString:urlStr];
  3. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  4. request.HTTPMethod = @"POST";
  5. // 请求体
  6. NSString *bodyStr = @"username=123&pwd=123";
  7. request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];

实战代码

  1. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
  2. // 创建URL(URL中不能有中文),如果包含中文需要对中文进行百分号编码
  3. NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
  4. // 对中文的百分号编码方法
  5. url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  6. // 创建request,如果要设置请求体或者其他请求参数,必须使用NSRULRequest的子类NSMutableURLRequest
  7. NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
  8. // POST请求体系统内部会自动添加?,POST可以发送中文
  9. request.HTTPBody = [@"username=哈哈哈&pwd=520it&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
  10. // 发送方式POST,必须大写
  11. request.HTTPMethod = @"POST";
  12. // 设置超时时间
  13. request.timeoutInterval = 10;
  14. // 设置请求头
  15. [request setValue:@"ios 9.0" forHTTPHeaderField:@"User-Agent"];
  16. // 发送请求
  17. [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
  18. NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  19. NSLog(@"%@",result);
  20. }];
  21. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注