UI网络笔记(二):UI网络之Post同步和异步的密文请求

一、Post相关内容


1、有同步、异步的方式;

2、get(明文)浏览器里看,post(密文)把参数变成NSData

3post可变字符串需要两个:


一个拼接URL+interface

req的时候只把URL+interface的字符串放进来


一个拼接key和value

然后把key和value的字符串变成data


然后单独设置data(key和value)


要强调一下这次请求的方式是POST



二、Post同步请求(以登录界面为例)


2.1、LogInViewController.m按钮方法(其他的都和Get一样)


-(void)btnDown

{

    //酱油们

    UITextField *count = (UITextField*)[self.view viewWithTag:5000]; 

    UITextField *pass = (UITextField*)[self.view viewWithTag:5001];    

    [count resignFirstResponder];

    [pass resignFirstResponder];    

    //请求们

    NSDictionary *dic = [PostSyn postSynWithURL:@"http://10.0.8.8/sns" andInterface:@"/my/login.php" andKeyArr:@[@"username",@"password"] andValueArr:@[count.text,pass.text] andTarget:self];    

    if([dic respondsToSelector:@selector(stringWithFormat:)])

    {//error了,直接退出

        return;

    }   

    if([dic[@"code"] isEqualToString:@"login_success"])

    {

        //push

        MainViewController *main = [[MainViewController alloc] init];

        [self.navigationController pushViewController:main animated:YES];

        [main release];

    }

    else

    {

        //警报

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警报" message:dic[@"message"] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

        [alert show];

        [alert release];

    }

}


2.2、PostSyn.h——Post同步请求封装类


#import <Foundation/Foundation.h>


@interface PostSyn : NSObject

+(id)postSynWithURL:(NSString*)url andInterface:(NSString*)interface andKeyArr:(NSArray*)keyArr andValueArr:(NSArray*)valueArr andTarget:(id)targetVC;

@end


2.3、PostSyn.m——Post同步请求封装类


#import "PostSyn.h"

#import <UIKit/UIKit.h>


@implementation PostSyn


+(id)postSynWithURL:(NSString *)url andInterface:(NSString *)interface andKeyArr:(NSArray *)keyArr andValueArr:(NSArray *)valueArr andTarget:(id)targetVC

{

    //4、准备两个可变字符串,一个拼地址、接口,一个拼keyvalue

    NSMutableString *urlStr = [NSMutableString stringWithCapacity:0];//拼地址和接口

    NSMutableString *contentStr = [NSMutableString stringWithCapacity:0];//keyvalue    

    //5、拼地址、接口

    [urlStr appendFormat:@"%@%@",url,interface];

    //6、拼keyvalue

    for(int i = 0;i<keyArr.count;i++)

    {

        [contentStr appendFormat:@"%@=%@",keyArr[i],valueArr[i]];

        if(i<keyArr.count-1)

        {

            [contentStr appendString:@"&"];

        }

    }

    //7、准备请求体

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10];

    //到目前为止请求体里有地址和接口,但是没有参数    

    //8、设置请求体的内容,里面的小括号把字符串转成了data当参数

    [req setHTTPBody:[contentStr dataUsingEncoding:NSUTF8StringEncoding]];

    //9、设置请求方式

    [req setHTTPMethod:@"POST"];

    

    NSError *error = nil;

    //10、开始同步请求

    NSData *backData = [NSURLConnection sendSynchronousRequest:req returningResponse:nil error:&error];

    if(error)

    {

        NSLog(@"%@",error);

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警报" message:@"error" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

        [alert show];

        [alert release];

        return @"error";

    }

    else

    {

        id dic = [NSJSONSerialization JSONObjectWithData:backData options:NSJSONReadingMutableLeaves error:&error];

        if(error)

        {

            NSLog(@"%@",error);

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警报" message:@"解析出错了" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

            [alert show];

            [alert release];

            return @"error1";

        }

        else

        {

            return dic;

        }

    }

}

@end


三、Post异步请求(以登录界面为例)


3.1、LogInViewController.m四个协议方法(其他的都和Get一样)


-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    MBProgressHUD *mb = (MBProgressHUD*)[self.view viewWithTag:12345];

    [mb removeFromSuperview];

    mb = nil;

    

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警报" message:@"您的网络故障,不是我的事儿" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];

    [alert show];

    [alert release];

    

    NSLog(@"%@",error);

}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    //大小   

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [self.data appendData:data];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    MBProgressHUD *mb = (MBProgressHUD*)[self.view viewWithTag:12345];

    [mb removeFromSuperview];

    mb = nil;

    

    NSError *error = nil;

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableLeaves error:&error];

    if(error)

    {

        NSLog(@"%@",error);

        

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警报" message:@"解析有误" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

        [alert show];

        [alert release];

    }

    else

    {

        if([dic[@"code"] isEqualToString:@"login_success"])

        {

            MainViewController *main = [[MainViewController alloc] init];

            [self.navigationController pushViewController:main animated:YES];

            [main release];

        }

        else

        {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警报" message:dic[@"message"] delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];

            [alert show];

            [alert release];

        }

    }

}


3.2、PostAsyn.h——Post异步请求封装类


#import <Foundation/Foundation.h>


@interface PostAsyn : NSObject

+(void)postAsynWithURL:(NSString*)url andInterface:(NSString*)interface andKeyArr:(NSArray*)keyArr andValueArr:(NSArray*)valueArr andDelegateTarget:(id)targetVc;

@end


3.3、PostAsyn.m——Post异步请求封装类


#import "PostAsyn.h"


@implementation PostAsyn


+(void)postAsynWithURL:(NSString *)url andInterface:(NSString *)interface andKeyArr:(NSArray *)keyArr andValueArr:(NSArray *)valueArr andDelegateTarget:(id)targetVc

{

    //4、两个可变字符串

    NSMutableString *urlStr = [NSMutableString stringWithCapacity:0];

    NSMutableString *contentStr = [NSMutableString stringWithCapacity:0];   

    //5、拼地址接口

    [urlStr appendFormat:@"%@%@",url,interface];

    //6、拼keyvalue

    for(int i = 0;i<keyArr.count;i++)

    {

        [contentStr appendFormat:@"%@=%@",keyArr[i],valueArr[i]];

        if(i < keyArr.count-1)

        {

            [contentStr appendString:@"&"];

        }

    }    

    //7、准备请求体

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlStr] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:10];    

    [req setHTTPBody:[contentStr dataUsingEncoding:NSUTF8StringEncoding]];

    [req setHTTPMethod:@"POST"];   

    //8、准备connection

    NSURLConnection *con = [NSURLConnection connectionWithRequest:req delegate:targetVc];

    [con start];

}

@end


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值