通常我们的URL请求可能是一组,并不是一个,而且我们的URL请求也是会不停的根据流程在变化的,所以我们要仍然使用NSURLRequest这个对象是不能随时的进行一个URL的变化的,因此我们的NSmutableURLRequest这个对象,可以用来解决我们这个问题。
在分配和初始化了请求之后修改请求url的timeout:
NSString *urlAsString = @"";
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
在分配和初始化了请求之后设置请求的URL和timeout:
NSString *urlAsString = @"";
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *request = [NSMutableURLRequest new];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setURL:url];
通过NSURLConnection发送一个HTTP GET 请求
你想通过Http协议向服务器发送一个Get的包装请求,并在这个请求中添加了一些请求参数
NSString *urlAsString = @"";
urlAsString = [urlAsString stringByAppendingString:@"?param1 = First"];
urlAsString = [urlAsString stringByAppendingString:@"¶m2 = Second"];
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandle:^(NSURLResponse *response,NSData *data,NSError *error){
if([data length]>0 && error == nil){
NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8String];
NSLog(@"HTML = %@",html);
}else if([data length] ==0 && error == nil ){
NSLog(@"Nothing was downloaded.");
}else if(error != nil){
NSLog(@"Error happened = %@",error);
}
}];
编译效果:
html =
<html>
<head>
<title>IOS 5 Programming Cookbook GET Example</title>
</head>
<body>
<b>HTTP Method</b>=GET<br>Query String</b> =Array([param1] =>First [param2]=>Second)
</body>
</html>
通过NSURLConnection发送一个POST表单请求
NSString *urlAsString = @"";
urlAsString = [urlAsString stringByAppendingString:@"?param1 = First"];
urlAsString = [urlAsString stringByAppendingString:@"¶m2 = Second"];
NSURL *url = [NSURL URLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"POST"];
NSString *body = @"bodyParam1 = BodyValue1&bodyParam2 = BodyValue2";
[urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
NSOperationQueue *queue = [[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandle:^(NSURLResponse *response,NSData *data,NSError *error){
if([data length]>0 && error == nil){
NSString *html = [[NSString alloc]initWithData:data encoding:NSUTF8String];
NSLog(@"HTML = %@",html);
}else if([data length] ==0 && error == nil ){
NSLog(@"Nothing was downloaded.");
}else if(error != nil){
NSLog(@"Error happened = %@",error);
}
}];
编译效果:
html =
<html>
<head>
<title>IOS 5 Programming Cookbook POST Example</title>
</head>
<body>
<b>HTTP Method</b>=POST<br>Query String</b> =Array([param1] =>First [param2]=>Second)
<b>Body parameters</b> = Array([bodyParam1]=>BodyValue1 [bodyParam2]=>BodyValue2)
</body>
</html>
通过NSURLConnection发生HTTP DELETE 请求
通过HTTP协议向服务器发送一个删除资源的一个URL链接,同时你也可以给这个请求添加参数
将[urlRequest setHTTPMethod:@"POST"];
改为[urlRequest setHTTPMethod:@"DELETE"];
通过NSURLConnection发送HTTP PUT请求
就像发送POST,GET,DELETE请求一样。将相应位置设置成PUT;
把Array和Dictionaries序列化成JSON对象
通过NSJSONSerialization这个类的dataWithJSONObject:options:error:方法来实现
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc]init];
[dictionary setValue:@"Anthony" forKey:@"First Name"];
[dictionary setValue:@"Robbins" forKey:@"Last Name"];
[dictionary setValue:[NSNumber numberWithUnsignedInteger:51] forKey:@"Age"];