POST&GET

2016-10-26  本文已影响18人  nothing_c

- (void)viewDidLoad {

[super viewDidLoad];

//注册接口:http://localhost:8080/Login/NewServlet?name=ooo&age=3&trueName=555&pwd=111&command=1

//登录接口:http://localhost:8080/Login/NewServlet?name=ooo&pwd=111&command=2

//@"http://192.168.1.134:8080/Login/NewServlet?name=aaa&age=18&pwd=111&trueName=bbb&command=1"

//发送GET请求

//[self sendGetRequest:@"54" age:@"12" trueName:@"dcefr" pwd:@"45"];

//发送POST请求

[self sendPostRequest:@"54" pwd:@"45"];

}

#pragma mark ---发送POST请求

- (void)sendPostRequest:(NSString *)name  pwd:(NSString *)pwd {

//设置请求路径

NSURL* url = [NSURL URLWithString:@"http://localhost:8080/Login1/NewServlet"];

//创建请求对象

NSMutableURLRequest * postRequest = [NSMutableURLRequestrequestWithURL:url];

//设置请求方式

[postRequest setHTTPMethod:@"POST"];

//添加请求参数

NSString * str = [NSString stringWithFormat:@"name=%@&pwd=%@&command=2",name,pwd];

[postRequest setHTTPBody:[str dataUsingEncoding:NSUTF8StringEncoding]];

//发送请求

[NSURLConnection sendAsynchronousRequest:postRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *_Nullableresponse,NSData *_Nullabledata,NSError *_NullableconnectionError) {

NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"---%@",str);

//父类的指针接收子类的对象

NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *)response;

//获取状态

//httpResponse.statusCode

//httpResponse.allHeaderFields  header字典

NSLog(@"POST httpResponse.statusCode-----%ld",httpResponse.statusCode);

NSLog(@"POST httpResponse.expectedContentLength-----%lld",httpResponse.expectedContentLength);

NSLog(@"POST httpResponse.allHeaderFields-----%@",httpResponse.allHeaderFields);

NSLog(@"POST httpResponse-----%@",httpResponse);

}];

}

#pragma mark ---发送GET请求

- (void) sendGetRequest:(NSString *)name age:(NSString *)age trueName:(NSString *)trueName pwd:(NSString *)pwd {

NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8080/Login1/NewServlet?name=%@&age=%@&trueName=%@&pwd=%@&command=1",name,age,trueName,pwd]];

//创建请求对象

NSMutableURLRequest *getRequest = [NSMutableURLRequest requestWithURL:url];

//设置请求方式(不设置请求方式,默认是GET请求) ----设置请求方式,要用NSMutableURLRequest创建请求对象

[getRequest setHTTPMethod:@"GET"];

[NSURLConnection sendAsynchronousRequest:getRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *_Nullableresponse,NSData *_Nullabledata,NSError *_NullableconnectionError) {

NSString * str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

NSLog(@"---%@",str);

//父类的指针接收子类的对象

NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse *)response;

//获取状态

//httpResponse.statusCode

//httpResponse.allHeaderFields  header字典

NSLog(@"httpResponse.statusCode-----%ld",httpResponse.statusCode);

NSLog(@"httpResponse.expectedContentLength-----%lld",httpResponse.expectedContentLength);

NSLog(@"httpResponse.allHeaderFields-----%@",httpResponse.allHeaderFields);

NSLog(@"httpResponse-----%@",httpResponse);

}];

}

上一篇下一篇

猜你喜欢

热点阅读