通过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);
}
}];
}

最新文章

  1. UIScrollView之轮转图片
  2. AD板层定义介绍(二)
  3. Xcode中的Info.plist字段列表详解
  4. Struts2 的国际化实现
  5. 最全Pycharm教程(28)——Pycharm搜索导航之搜索应用实例
  6. LR12集合点设置和多个负载生成器策略
  7. Rhino学习教程——1.5
  8. Django+Xadmin打造在线教育系统(四)
  9. shiro-5基于url的权限管理
  10. mariadb:SQL日常使用总结
  11. socket通讯---TcpClient
  12. Linux系统目录权限chmod误操作权限修复方法
  13. Html+Ajax+Springmvc+Mybatis,不用JSP【转】
  14. Codeforces Round #310 (Div. 2)
  15. spring ----> aop的两种实现方式
  16. BZOJ 4720 [Noip2016]换教室
  17. mysql show global variables字符超1024会被截断
  18. 【linux】——ubuntu12.04 下安装wine和wine乱码解决方案
  19. 第十一章 springboot + mongodb(简单查询)
  20. R简易安装

热门文章

  1. MVC 二级联动
  2. LeetCode_7.Reverse Integer
  3. [No0000107]C#中 Excel列字母与数字的转换
  4. Vue SSR 配合Java的Javascript引擎j2v8实现服务端渲染2创建Vue2+webpack4项目
  5. Vue SSR配合Java的Javascript引擎j2v8实现服务端渲染1概述
  6. innodb表锁情况
  7. Amazon Aurora: Design Considerations for High Throughput Cloud-Native Relational Databases
  8. hadoop 学习笔记
  9. spark Pair RDD 基础操作
  10. 动态SQL详解