NSMutableURLRequest是NSURLRequest的子类,常用方法有
//1、设置请求超时等待时间(超过这个时间就算超时,请求失败)
- (void)setTimeoutInterval:(NSTimeInterval)seconds;
//2、设置请求方法(比如GET和POST)
- (void)setHTTPMethod:(NSString*)method;
//3、设置请求体
- (void)setHTTPBody:(NSData*)data;
//4、设置请求头
- (void)setValue:(NSString*)value forHTTPHeaderField:(NSString*)field;
创建GET请求
NSString *urlStr = [@"http://xxxx/login?username=123&pwd=123" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL URLWithString:urlStr];
创建POST请求
NSString *urlStr = @"http://xxxx/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];
设置请求头,用POST请求,给服务器发送JSON数据
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
网友评论