- (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请求的更多相关文章

  1. 使用HttpClient来异步发送POST请求并解析GZIP回应

    .NET 4.5(C#): 使用HttpClient来异步发送POST请求并解析GZIP回应 在新的C# 5.0和.NET 4.5环境下,微软为C#加入了async/await,同时还加入新的Syst ...

  2. iOS使用NSURLSession发送POST请求,后台无法接受到请求过来的参数

    iOS中发送POST请求,有时需要设置Content-Type,尤其是上传图片的时候. application/x-www-form-urlencoded: 窗体数据被编码为名称/值对.这是标准的编码 ...

  3. iOS边练边学--NSURLConnection发送HTTP请求以及NSString和NSData的相互转换

    HTTP请求的常见方法 GET 所有参数拼接在URL后面,并且参数之间用&隔开 比如http://520it.com?name=123&pwd=345 传递了2个参数给服务器 name ...

  4. iOS开发 GET、POST请求方法(NSURLConnection篇)

    Web Service使用的主要协议是HTTP协议,即超文本传输协议. HTTP/1.1协议共定义了8种请求方法(OPTIONS.HEAD.GET.POST.PUT.DELETE.TRACE.CONN ...

  5. iOS中发送HTTP请求的方案

    在iOS中,常见的发送HTTP请求的方案有 苹果原生(自带) NSURLConnection:用法简单,最古老最经典的一种方案 NSURLSession:功能比NSURLConnection更加强大, ...

  6. iOS - NSURLConnection 网络请求

    前言 @interface NSURLConnection : NSObject class NSURLConnection : NSObject DEPRECATED: The NSURLConne ...

  7. 李洪强iOS开发本人集成环信的经验总结_06_发送好友请求

    李洪强iOS开发本人集成环信的经验总结_06_发送好友请求 同步好友请求 异步好友请求

  8. NSURLConnection发送GET请求

    // ViewController.m // 04-掌握-NSURLConnection发送GET请求 // // Created by xiaomage on 16/2/22. // Copyrig ...

  9. Android Studio利用异步任务AsyncTask发送post请求获取json数据

    syncTask,是Android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操作,并提供接口反馈当前异步执行的程度(可以通过接口实现UI进度更新),最后反馈执行的结果给UI主 ...

随机推荐

  1. js中event.preventDefault()和 event.stopPropagation( ) 方法详解

    event.preventDefault() 1.首先event.preventDefault()是通知浏览器不要执行与事件关联的默认动作,例如:  这里a标签的默认事件是跳转,这里我们告诉浏览器取消 ...

  2. 45、[源码]-Spring容器创建-执行BeanFactoryPostProcessor

    45.[源码]-Spring容器创建-执行BeanFactoryPostProcessor 5.invokeBeanFactoryPostProcessors(beanFactory);执行BeanF ...

  3. 2019-2020 ICPC, Asia Jakarta Regional Contest

    目录 Contest Info Solutions A. Copying Homework C. Even Path E. Songwriter G. Performance Review H. Tw ...

  4. <frame>、<iframe>、<embed>、<object> 和 <applet>

    frame frame 必须在 frameset 里,而 frameset 又不能和 body 共存(就是一旦存在 frame,就不能存在 body 了,因此这个基本每人使用) 推荐阅读:https: ...

  5. ckeditor自定义工具栏

    /** * 获取编辑器工具栏自定义参数 * @param type 类型 simple=极简版 basic=基本版 full=完整版 */ function get_ckeditor_toolbar( ...

  6. jenkins之docker安装(jenkins/jenkins:lts)

    建议使用此镜像安装,不要使用官网推荐的jenkinsci/blueocean镜像,使用它构建node程序会出现问题. 1.宿主服务器jenkins_home目录权限 为了方便安装插件,升级,迁移,因此 ...

  7. Dockerfile HEALTHCHECK详解

    Dockerfile中使用HEALTHCHECK的形式有两种: 1.HEALTHCHECK [options] CMD command(本次详细解释) 2.HEALTHCHECK NODE 意思是禁止 ...

  8. 每天一个linux命令:top命令

    top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...

  9. Flutter移动电商实战 --(50)持久化_shared_preferences

    当app关掉了.再进去的时候 ,购物车的内容还是存在. sqflite提供这个来操作SQLite数据库 flutter提供三种持久化的工具 今天要学的就是 shared_preferences 还有一 ...

  10. sql server management studio 连接时指定非默认端口 ,port