11.5. Sending HTTP GET Requests with NSURLConnection
发送get请求并传递一些参数
-(void)testGetHttp
{
// NSString *urlAsString = @"http://www.baidu.com/s";//百度的数据在解析成字符串时失败
NSString *urlAsString =@"http://www.apple.com";
urlAsString = [urlAsStringstringByAppendingString:@"?wd=abc"];
urlAsString = [urlAsStringstringByAppendingString:@"&ie=utf-8"];
NSURL *url = [NSURLURLWithString:urlAsString];
//这里初始化时设定超时
// NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
NSURLRequest *urlRequest = [NSURLRequestrequestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:30.0f];
NSURLResponse *response = nil;
NSError *error = nil;
NSLog(@"will send ");
NSData * data = [NSURLConnectionsendSynchronousRequest:urlRequest returningResponse:&response error:&error];
if ([data length] >0 && error ==nil){
NSLog(@"length=%d,html=%@",data.length,[[NSStringalloc] initWithData:dataencoding:NSUTF8StringEncoding]);
NSLog(@"%@",response.textEncodingName);
/* Append the filename to the documents directory */
NSString * sfilePath = NSTemporaryDirectory();
sfilePath = [sfilePathstringByAppendingString:@"apple.html"];
// NSLog(@"sfilePath = %@",sfilePath);
NSURL * filePath = [NSURLfileURLWithPath:sfilePath];
// NSLog(@"filePath = %@",filePath);
[datawriteToURL:filePath atomically:YES];
// NSLog(@"Successfully saved the file to %@", filePath);
}elseif ([data length] ==0 && error == nil){
NSLog(@"Nothing was downloaded.");
}
else if (error !=nil){
NSLog(@"Error happened = %@", error);
}
NSLog(@"done");
}
2014-06-24 17:14:37.231 cookbook7[1145:a0b] will send
2014-06-24 17:14:38.510 cookbook7[1145:a0b] length=12559,html=<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
……
</head>
<body class="home" id="home">
……
</body>
</html>
2014-06-24 17:14:38.515 cookbook7[1145:a0b] utf-8
2014-06-24 17:14:38.516 cookbook7[1145:a0b] done
11.6. Sending HTTP POST Requests with NSURLConnection
post方法请求
-(void)testPostHttp
{
NSString *urlAsString = @"http://www.baidu.com/s";
urlAsString = [urlAsStringstringByAppendingString:@"?param1=First"];
urlAsString = [urlAsStringstringByAppendingString:@"¶m2=Second"];
NSURL *url = [NSURLURLWithString:urlAsString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequestrequestWithURL:url];
[urlRequestsetTimeoutInterval:30.0f];
[urlRequestsetHTTPMethod:@"POST"];
NSString *body =@"bodyParam1=BodyValue1&bodyParam2=BodyValue2";
[urlRequest setHTTPBody:[bodydataUsingEncoding:NSUTF8StringEncoding]];
NSOperationQueue *queue = [[NSOperationQueuealloc] init];
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if ([data length] >0 && error ==nil){ NSString *html =
[[NSStringalloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@", html);
}
else if ([datalength] == 0 && error ==nil){
NSLog(@"Nothing was downloaded.");
}
else if (error !=nil){
NSLog(@"Error happened = %@", error);
}
}];
}
打印:
2014-06-24 17:45:53.710 cookbook7[1218:4307] HTML = (null)
百度不接收这样的post请求的,结果就这样咯
11.7. Sending HTTP DELETE Requests with NSURLConnection
把11.5或11.6中得方法改下即可
[urlRequestsetHTTPMethod:@"DELETE"];
11.8. Sending HTTP PUT Requests with NSURLConnection
上传资源到服务器
我们就试着给百度发点东西吧,看他会怎样
-(void)testPutHttp
{
NSString *urlAsString =@"http://www.baidu.com/s";
urlAsString = [urlAsStringstringByAppendingString:@"?param1=First"];
urlAsString = [urlAsStringstringByAppendingString:@"¶m2=Second"];
NSURL *url = [NSURLURLWithString:urlAsString];
NSMutableURLRequest *urlRequest =
[NSMutableURLRequestrequestWithURL:url];
[urlRequestsetTimeoutInterval:30.0f];
[urlRequestsetHTTPMethod:@"PUT"];
NSString *body =@"bodyParam1=BodyValue1&bodyParam2=BodyValue2";
[urlRequest setHTTPBody:[bodydataUsingEncoding:NSUTF8StringEncoding]];
NSOperationQueue *queue = [[NSOperationQueuealloc] init];
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:queue
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error) {
if ([data length] >0 && error ==nil){ NSString *html =
[[NSStringalloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@", html);
}
else if ([datalength] == 0 && error ==nil){
NSLog(@"Nothing was downloaded.");
}
else if (error !=nil){
NSLog(@"Error happened = %@", error);
}
}];
}
打印:
2014-06-24 17:50:50.609 cookbook7[1236:1403] Nothing was downloaded.