iOS 开发笔记-NSURLConnection的使用
2024-09-13 00:34:22
通过NSURLConnection发送一个HTTP GET请求
//send a GET request to server with some params
-(void)httpGetWithParams{
NSString *urlString = @"http://chaoyuan.sinaapp.com";
urlString = [urlString stringByAppendingString:@"?p=1059"];
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"GET"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if ([data length] > && connectionError == nil) {
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"HTML = %@",html);
}else if([data length] == && connectionError == nil){
NSLog(@"nothing was download.");
}else if(connectionError != nil){
NSLog(@"Error happened = %@",connectionError);
}
}];
}
通过NSURLConnection发送一个HTTP POST请求
//send a POST request to a server with some params
-(void)httpPostWithParams{
NSString *urlAsString = @"http://chaoyuan.sinaapp.com";
urlAsString = [urlAsString stringByAppendingString:@"?param1=First"];
urlAsString = [urlAsString stringByAppendingString:@"¶m2=Second"];
NSURL *url = [NSURL URLWithString:urlAsString];
NSString *body = @"bodyParam1=BodyValue1&bodyParam2=BodyValue2";
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setTimeoutInterval:30.0f];
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
[urlRequest setValue:[NSString stringWithFormat:@"%lu", (unsigned long)[body length]] forHTTPHeaderField:@"Content-Length"];
[urlRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:queue completionHandler:^(NSURLResponse *response, NSData *data,
NSError *error) {
if ([data length] > &&
error == nil){
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; NSLog(@"HTML = %@", html);
// 显示到UIWebView
//[self.bankWebView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];
}
else if ([data length] == &&
error == nil){
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error happened = %@", error);
}
}];
}
最新文章
- UIScrollView之轮转图片
- AD板层定义介绍(二)
- Xcode中的Info.plist字段列表详解
- Struts2 的国际化实现
- 最全Pycharm教程(28)——Pycharm搜索导航之搜索应用实例
- LR12集合点设置和多个负载生成器策略
- Rhino学习教程——1.5
- Django+Xadmin打造在线教育系统(四)
- shiro-5基于url的权限管理
- mariadb:SQL日常使用总结
- socket通讯---TcpClient
- Linux系统目录权限chmod误操作权限修复方法
- Html+Ajax+Springmvc+Mybatis,不用JSP【转】
- Codeforces Round #310 (Div. 2)
- spring ---->; aop的两种实现方式
- BZOJ 4720 [Noip2016]换教室
- mysql show global variables字符超1024会被截断
- 【linux】——ubuntu12.04 下安装wine和wine乱码解决方案
- 第十一章 springboot + mongodb(简单查询)
- R简易安装
热门文章
- MVC 二级联动
- LeetCode_7.Reverse Integer
- [No0000107]C#中 Excel列字母与数字的转换
- Vue SSR 配合Java的Javascript引擎j2v8实现服务端渲染2创建Vue2+webpack4项目
- Vue SSR配合Java的Javascript引擎j2v8实现服务端渲染1概述
- innodb表锁情况
- Amazon Aurora: Design Considerations for High Throughput Cloud-Native Relational Databases
- hadoop 学习笔记
- spark Pair RDD 基础操作
- 动态SQL详解