iOS开发中的Get请求和POST请求

//Get请求一般为不涉及到用户的账号密码的网络请求,其中Get请求是等请求内容回来之后,才可以进行下一步的操作

- (void)requestWithGet{

    //Get请求:

    

    //1.设置请求路径

    NSString * urlStr = [NSString stringWithFormat:@"http://192.168.1.53:8080/MJServer/login?username=%@&pwd=%@",self.nameTextField.text,self.pawTextField.text];

    //2.创建请求对象,不设置,默认为Get

    NSURL * url = [NSURL URLWithString:urlStr];

    //3.发送请求

}

//POST请求可以用来发送账号密码的登录请求,因为其URL没有显示账号和密码的内容,所以要相对安全些,而且一些图片的异步下载等等

- (void)requestWithPOST{

    //POST请求:

    

    //设置请求路径

    NSURL * url = [NSURL URLWithString:@"http://192.168.1.53:8080/"];

    //创建请求对象

    NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url];

    //设置请求超时为5秒

    request.timeoutInterval=5.0;

    //设置请求方法

    request.HTTPMethod=@"POST";

    //设置请求体

    NSString *param=[NSString stringWithFormat:@"username=%@&pwd=%@",self.nameTextField.text,self.pawTextField.text];

    //把拼接后的字符串转换为data,设置请求体

    request.HTTPBody=[param dataUsingEncoding:NSUTF8StringEncoding];

    //发送请求

    

    

}

 

posted @ 2015-01-23 15:29  那个顾客  阅读(295)  评论(0编辑  收藏  举报