IOS Http请求
来源:互联网 发布:域名注册阿里云 编辑:程序博客网 时间:2024/05/22 04:41
ViewController.h文件
#import <UIKit/UIKit.h>@interface ViewController : UIViewController<NSURLConnectionDelegate> //get 异步请求需要继承这个协议- (IBAction)GetRequest:(id)sender;- (IBAction)PostRequest:(id)sender;- (IBAction)GetRequest2:(id)sender;@property (weak, nonatomic) IBOutlet UILabel *result;//接收从服务器返回数据。@property (strong,nonatomic) NSMutableData *datas;@end
Viewcontroller.m 文件
#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib.}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}//get 同步请求- (IBAction)GetRequest:(id)sender { NSString *strURL = [[NSString alloc] initWithFormat:@"http://192.168.0.105:8080/iosServer/index.jsp?action=%@&username=%@&pwd=%@", @"testiosloign",@"liu",@"123"]; NSURL *url=[NSURL URLWithString:strURL]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSLog(@"请求完成..."); // NSDictionary *resDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];// NSString* aStr= [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@"\n test return resrul:%@",aStr); self.result.text = aStr;}- (IBAction)PostRequest:(id)sender{ NSString *strURL = [[NSString alloc] initWithFormat:@"http://192.168.0.105:8080/iosServer/index.jsp"]; NSURL *url=[NSURL URLWithString:strURL]; NSString *post = [NSString stringWithFormat:@"action=%@&username=%@&pwd=%@", @"postrequest",@"JSON",@"query"]; NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:postData]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (connection) { _datas = [NSMutableData new]; } }//get 异步请求- (IBAction)GetRequest2:(id)sender{ NSString *strURL = [[NSString alloc] initWithFormat:@"http://192.168.0.105:8080/iosServer/index.jsp?action=%@&username=%@&pwd=%@", @"testiosloign",@"liu",@"123"]; NSURL *url=[NSURL URLWithString:strURL]; NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url]; NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (connection) { _datas = [NSMutableData new]; } }#pragma mark- NSURLConnection get/post异步请回调方法- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{ [_datas appendData:data]; NSString* aStr= [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@"\n test return resrul:%@",aStr); self.result.text = aStr;}-(void) connection:(NSURLConnection *)connection didFailWithError: (NSError *)error { NSLog(@"%@",[error localizedDescription]);}- (void) connectionDidFinishLoading: (NSURLConnection*) connection{ NSDictionary* dict = [NSJSONSerialization JSONObjectWithData:_datas options:NSJSONReadingAllowFragments error:nil]; NSLog(@"请求完成...%@",dict); }@end
0 0
- ios http请求
- IOS之HTTP请求
- ios http请求
- ios HTTP post请求
- ios http请求
- iOS HTTP请求
- IOS Http请求
- iOS http 请求
- IOS-HTTP请求
- ios 添加 http 请求
- iOS发送http请求
- iOS 允许HTTP请求
- iOS网络--HTTP请求
- IOS http网络请求
- iOS iOS9 HTTP请求失败
- iOS NsSession http请求错误
- iOS HTTP请求与解析
- iOS http请求记录一下
- mysql 遇到的问题
- Lucene入门实例。
- debian “更换介质:请把标有…… DVD 的盘片插入驱动器“/media/cdrom/”再按回车键“ 解决方法
- curl学习(三):使用POST的处理方法
- C/C++ 程序代码覆盖率统计工具Gcov
- IOS Http请求
- zookeeper+kafka安装以及kafka+spark streaming 的简单整合
- 关于arguments 和 repla
- 基于IOS平台的游戏之小拼图
- Android C程序打印logcat日志
- 公有、私有、保护总结
- CSAPP LAB---buflab-handout(缓冲区溢出实验)
- 获取自定义标题栏组件要设置setContentView
- 【Echarts】——上手Demo