首页 > 移动平台 > 详细

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

时间:2015-01-23 15:53:24      阅读:290      评论:0      收藏:0      [点我收藏+]

//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];

    //发送请求

    

    

}

 

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

原文:http://www.cnblogs.com/zhouyantongiOSDev/p/4244320.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!