- (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. 2.4 vue配置(下)

  2. Core DOM、HTML DOM、XML DOM关系

    查看:https://blog.csdn.net/IamChuancey/article/details/78335443

  3. MVC框架和MTV框架

    MVC,全名是Model View Controller,是软件工程中的一种软件架构模式,把软件系统分为三个基本部分:模型(Model).视图(View)和控制器(Controller),具有耦合性低 ...

  4. BZOJ 3636 教义问答手册 (分治)

    题意 一个整数数列,多次询问某段区间[li,ri][l_i,r_i][li​,ri​]内,选出若干个长度为LLL且不相交的连续段使选出来的数和最大. 分析 首先想朴素的区间DPDPDP 设f[i][j ...

  5. vue 配置 TinyMCE

    1.index.html 增加cdn 地址 <script src="//cdn.bootcss.com/tinymce/5.0.16/tinymce.min.js"> ...

  6. MySQL数据库中常用的引擎有几种?有什么区别?

    1.常用的3种  2.InnoDB Myisam Memory 3.InnoDB跟Myisam的默认索引是B+tree,Memory的默认索引是hash 区别: 1.InnoDB支持事务,支持外键,支 ...

  7. java调出cmd窗口长ping某个ip

    package lct.conference.test; import java.io.IOException; public class Test { public static void main ...

  8. Linux 查看实时网卡流量的几种方式

    在工作中,我们经常需要查看服务器的实时网卡流量.通常,我们会通过这几种方式查看Linux服务器的实时网卡流量. 1. sar -n DEV 1 2 sar命令包含在sysstat工具包中,提供系统的众 ...

  9. 三十.数据库服务概述 构建MySQL服务器 、 数据库基本管理 MySQL数据类型

    mysql50:192.168.4.50 1.构建MySQL服务器 安装MySQL-server.MySQl-client软件包 修改数据库用户root的密码 确认MySQL服务程序运行.root可控 ...

  10. 后缀数组 TYVJ P1860 后缀数组

    /*P1860 后缀数组时间: 1000ms / 空间: 131072KiB / Java类名: Main描述 我们定义一个字符串的后缀suffix(i)表示从s[i]到s[length(s)]这段子 ...