iOS-NSURLConnection异步发送 HTTP请求
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
NSString *new = [searchBar.text stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSString *urlString = [NSString stringWithFormat:@"http://192.168.1.104/test.php?cid=%@",new];
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
NSString *strUrld8 = [urlString stringByAddingPercentEscapesUsingEncoding:enc];
//调用http get请求方法
[self sendRequestByGet:strUrld8];
}
//HTTP get请求方法
- (void)sendRequestByGet:(NSString*)urlString
{
NSURL *url=[NSURL URLWithString:urlString];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
//设置请求方式为get
[request setHTTPMethod:@"GET"];
//添加用户会话id
[request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
//连接发送请求
receivedData=[[NSMutableData alloc] initWithData:nil];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[request release];
[conn release];
}
- (void)connection:(NSURLConnection *)aConn didReceiveResponse:(NSURLResponse *)response {
// 注意这里将NSURLResponse对象转换成NSHTTPURLResponse对象才能去
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
if ([response respondsToSelector:@selector(allHeaderFields)]) {
NSDictionary *dictionary = [httpResponse allHeaderFields];
NSLog(@"[email=dictionary=%@]dictionary=%@",[dictionary[/email] description]);
}
}
//接收NSData数据
- (void)connection:(NSURLConnection *)aConn didReceiveData:(NSData *)data {
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)aConn didFailWithError:(NSError *)error{
NSLog(@"[email=error=%@]error=%@",[error[/email] localizedDescription]);
}
//接收完毕,显示结果
- (void)connectionDidFinishLoading:(NSURLConnection *)aConn {
NSString *results = [[NSString alloc]
initWithBytes:[receivedData bytes]
length:[receivedData length]
encoding:NSUTF8StringEncoding];
NSLog(@"results=%@",results);
}
iOS-NSURLConnection异步发送 HTTP请求的更多相关文章
- 使用HttpClient来异步发送POST请求并解析GZIP回应
.NET 4.5(C#): 使用HttpClient来异步发送POST请求并解析GZIP回应 在新的C# 5.0和.NET 4.5环境下,微软为C#加入了async/await,同时还加入新的Syst ...
- iOS使用NSURLSession发送POST请求,后台无法接受到请求过来的参数
iOS中发送POST请求,有时需要设置Content-Type,尤其是上传图片的时候. application/x-www-form-urlencoded: 窗体数据被编码为名称/值对.这是标准的编码 ...
- iOS边练边学--NSURLConnection发送HTTP请求以及NSString和NSData的相互转换
HTTP请求的常见方法 GET 所有参数拼接在URL后面,并且参数之间用&隔开 比如http://520it.com?name=123&pwd=345 传递了2个参数给服务器 name ...
- iOS开发 GET、POST请求方法(NSURLConnection篇)
Web Service使用的主要协议是HTTP协议,即超文本传输协议. HTTP/1.1协议共定义了8种请求方法(OPTIONS.HEAD.GET.POST.PUT.DELETE.TRACE.CONN ...
- iOS中发送HTTP请求的方案
在iOS中,常见的发送HTTP请求的方案有 苹果原生(自带) NSURLConnection:用法简单,最古老最经典的一种方案 NSURLSession:功能比NSURLConnection更加强大, ...
- iOS - NSURLConnection 网络请求
前言 @interface NSURLConnection : NSObject class NSURLConnection : NSObject DEPRECATED: The NSURLConne ...
- 李洪强iOS开发本人集成环信的经验总结_06_发送好友请求
李洪强iOS开发本人集成环信的经验总结_06_发送好友请求 同步好友请求 异步好友请求
- NSURLConnection发送GET请求
// ViewController.m // 04-掌握-NSURLConnection发送GET请求 // // Created by xiaomage on 16/2/22. // Copyrig ...
- Android Studio利用异步任务AsyncTask发送post请求获取json数据
syncTask,是Android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口实现UI进度更新),最后反馈执行的结果给UI主 ...
随机推荐
- Define Interfaces and Share Class Members through Mixins in Dart
In this lesson, we will cover Interfaces and Mixins. Interfaces act as a contract containing propert ...
- redis登录及设置密码
redis服务开启 : ./redis-server /opt/redisConf/redis.conf 1,查询默认密码 127.0.0.1:6379> config get requirep ...
- Linux系统出现hung_task_timeout_secs和blocked for more than 120 seconds的解决方法
Linux系统出现系统没有响应. 在/var/log/message日志中出现大量的 “echo 0 > /proc/sys/kernel/hung_task_timeout_secs" ...
- luogu 3919
主席树 #include <iostream> #include <cstdio> #include <algorithm> #include <cmath& ...
- wepy框架滑动组件使用
https://github.com/dlhandsome/wepy-com-swiper
- 记一次netty的Hadoop和elasticsearch冲突jar包
在一个项目中同时使用hbase和elasticsearch出现netty的jar包冲突的问题 事件: 在同一maven项目中使用hbase的同时又用了es 程序运行后出错 java.lang.NoSu ...
- mysql 修改表结构以支持事务操作
修改表的类型为 INNODB 的 SQL: alter table category_ ENGINE = innodb; 查看表的类型的 SQL show table status from ...
- $.extend和$.fn.extend详解
一.定义 $.extend()属于j全局的Query对象,用于将一个或多个对象合并到目标对象上: $.fn.extend()属于jQuery的原型对象,用于在jQuery原型上扩展实例属性和方法. 二 ...
- c++中的new的应用
代码如下: #include <cstddef> #include <iostream> using namespace std; class CTest{ public: ; ...
- 可视化图表库--goJS
GoJS是Northwoods Software的产品.Northwoods Software创立于1995年,专注于交互图控件和类库.旗下四款产品: GoJS:用于在HTML上创建交互图的纯java ...