get和post方法

#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(@"接收数据完毕");

    //如果 不是用allocinit方法进行的初始化 静态创建的话 会造成释放 打印会遇到崩溃

    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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值