// http 同步GET请求,将参数直接写在访问路径上。操作简单,不过容易被外界看到,安全性不高,地址最多255字节;
-(void)httpSyncroGet
{
NSString *path = @"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo?mobileCode=13898319555&userID=";
NSURL* url = [NSURL URLWithString:path];
NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10];
NSData *received = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *str = [[NSString alloc]initWithData:received encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}
// 同步POST请求,将参数放到body里面。POST请求操作相对复杂,需要将参数和地址分开,不过安全性高,参数放在body里面,不易被捕获。
-(void)httpSyncroPost
{
NSString *postPath = @"http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
NSURL *url = [NSURL URLWithString:postPath];
//创建http请求
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
//设置请求方式 默认是GET
[request setHTTPMethod:@"POST"];
//设置请求体
[request setHTTPBody:[@"mobileCode=15652590820&userID=" dataUsingEncoding:NSUTF8StringEncoding]];
//发出HTTP请求并且得到服务器的返回数据
NSData *resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:Nil error:Nil];
//把返回数据转成字符串
NSString *resultXMLString = [[NSString alloc]initWithData:resultData encoding:NSUTF8StringEncoding];
NSLog(@"%@",resultXMLString);
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self httpSyncroGet];
[self httpSyncroPost];
}