#import <UIKit/UIKit.h>
@interface liViewController : UIViewController<NSURLConnectionDataDelegate>
{
NSMutableString *_pResultStr;
}
- (IBAction)buttonPressed:(id)sender;
@property (retain, nonatomic) IBOutlet UITextField *textFiledQQ;
@property (retain, nonatomic) IBOutlet UILabel *labelResult;
@end
#import "liViewController.h"
@interface liViewController ()
@end
@implementation liViewController
- (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.
}
//让键盘弹回
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.view endEditing:YES];
}
- (void)dealloc {
[_textFiledQQ release];
[_labelResult release];
[super dealloc];
}
- (IBAction)buttonPressed:(id)sender {
//get请求
/*
//获取当前的qq号码
NSString *pStr = self.textFiledQQ.text;
//将其拼接成字符串 把qq号添加在网址的后边
NSString *pStrUrl = [@"http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline?qqCode=" stringByAppendingFormat:@"%@",pStr];
//将获取的路径转化为URL
NSURL *pURL = [NSURL URLWithString:pStrUrl];
//创建一个请求 不规定的话 是get请求
NSURLRequest *pRequest = [NSURLRequest requestWithURL:pURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
//向服务器发送请求
[NSURLConnection connectionWithRequest:pRequest delegate:self];
*/
//post请求
//与get请求的第一个区别点 不带参数
NSString *postStr = @"http://webservice.webxml.com.cn/webservices/qqOnlineWebService.asmx/qqCheckOnline";
//转换为URL
NSURL *postURL = [NSURL URLWithString:postStr];
//第二个不同点是 请求是(NSMutableURLRequest)
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:postURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60];
//将参数做成一个字符串 注意@"qqCode=%@"不可以加空格 网址要求
NSString *postStr1 = [NSString stringWithFormat:@"qqCode=%@",self.textFiledQQ.text];
//转换为NSData
NSData *postData = [postStr1 dataUsingEncoding:NSUTF8StringEncoding];
//第三个区别是将参数作为body体
[request setHTTPBody:postData];
//必须手动声明当前的请求是post
[request setHTTPMethod:@"POST"];
//向服务器发送请求
[NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"服务器响应");
_pResultStr = [[NSMutableString alloc]init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSLog(@"服务器接收数据");
//NSLog(@"data:%@",data);
//将data通过UTF8的编码方式转换为字符串
NSString *pStr = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
[_pResultStr appendString:pStr];
NSLog(@"pStr %@",_pResultStr);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"接收数据完毕");
//如果 不是用alloc和init方法进行的初始化 静态创建的话 会造成释放 打印会遇到崩溃
NSString *tempStr = [_pResultStr substringWithRange:NSMakeRange(78, 1)];
NSLog(@"tempStr = %@",tempStr);
[self resulet:tempStr];
}
- (void)resulet:(NSString *)string
{
if (NSOrderedSame == [string compare:@"Y"])
{
NSLog(@"在线");
self.labelResult.text = @"用户在线";
}
else if(NSOrderedSame == [string compare:@"V"])
{
NSLog(@"超出免费用户数量");
self.labelResult.text = @"超出免费用户数量";
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"接收数据错误 错误原因是:%@",[error localizedDescription]);
}
@end