@SanMao
2015-08-09T17:53:44.000000Z
字数 3271
阅读 1446
网络
NSURL
对象,设置请求路径NSURLRequest
对象,设置请求头和请求体NSURLConnection
发送请求
+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error;
+ (void)sendAsynchronousRequest:(NSURLRequest*) request queue:(NSOperationQueue*) queue completionHandler:(void (^)(NSURLResponse* response, NSData* data, NSError* connectionError)) handler;
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate;
+ (NSURLConnection*)connectionWithRequest:(NSURLRequest *)request delegate:(id)delegate;
- (id)initWithRequest:(NSURLRequest *)request delegate:(id)delegate startImmediately:(BOOL)startImmediately;
在startImmediately = NO的情况下,需要调用start方法开始发送请求
- (void)start;
成为NSURLConnection的代理,最好遵守NSURLConnectionDataDelegate
协议
NSURLConnectionDataDelegate协议中的代理方法
// 开始接收到服务器的响应时调用
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
// 接收到服务器返回的数据时调用(服务器返回的数据比较大时会调用多次)
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
// 服务器返回的数据完全接收完毕后调用
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
// 请求出错时调用(比如请求超时)
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
// 设置请求超时等待时间(超过这个时间就算超时,请求失败)
- (void)setTimeoutInterval:(NSTimeInterval)seconds;
// 设置请求方法(比如GET和POST)
- (void)setHTTPMethod:(NSString *)method;
// 设置请求体
- (void)setHTTPBody:(NSData *)data;
// 设置请求头
- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;
NSString *urlStr = [@"http://120.25.226.186:32812/login?username=123&pwd=123" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *urlStr = @"http://120.25.226.186:32812/login";
NSURL *url = [NSURL URLWithString:urlStr];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
request.HTTPMethod = @"POST";
// 请求体
NSString *bodyStr = @"username=123&pwd=123";
request.HTTPBody = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// 创建URL(URL中不能有中文),如果包含中文需要对中文进行百分号编码
NSURL *url = [NSURL URLWithString:@"http://120.25.226.186:32812/login"];
// 对中文的百分号编码方法
url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// 创建request,如果要设置请求体或者其他请求参数,必须使用NSRULRequest的子类NSMutableURLRequest
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
// POST请求体系统内部会自动添加?,POST可以发送中文
request.HTTPBody = [@"username=哈哈哈&pwd=520it&type=JSON" dataUsingEncoding:NSUTF8StringEncoding];
// 发送方式POST,必须大写
request.HTTPMethod = @"POST";
// 设置超时时间
request.timeoutInterval = 10;
// 设置请求头
[request setValue:@"ios 9.0" forHTTPHeaderField:@"User-Agent"];
// 发送请求
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",result);
}];
}