GET请求和POST请求

来源:互联网 发布:农村淘宝绑定服务站 编辑:程序博客网 时间:2024/05/28 15:07

get请求数据

//    1.设置请求路径2     NSString *urlStr=[NSString stringWithFormat:@"http://192.168.1.53:8080/MJServer/login?username=%@&pwd=%@",self.username.text,self.pwd.text];3     NSURL *url=[NSURL URLWithString:urlStr];4     5 //    2.创建请求对象6     NSURLRequest *request=[NSURLRequest requestWithURL:url];7     8 //    3.发送请求

post请求数据

// 1.设置请求路径     NSURL *URL=[NSURL URLWithString:@"http://192.168.1.53:8080/MJServer/login"];//不需要传递参数 //    2.创建请求对象     NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:URL];//默认为get请求    request.timeoutInterval=5.0;//设置请求超时为5秒    request.HTTPMethod=@"POST";//设置请求方法  //设置请求体     NSString *param=[NSString stringWithFormat:@"username=%@&pwd=%@",self.username.text,self.pwd.text];  //把拼接后的字符串转换为data,设置请求体     request.HTTPBody=[param dataUsingEncoding:NSUTF8StringEncoding];//客户端类型,只能写英文    [request setValue:@"ios+android" forHTTPHeaderField:@"User-Agent"]; //    3.发送请求

比较:相对POST请求而言,GET请求的所有参数都直接暴露在URL中,请求的URL一般会记录在服务器的访问日志中,而服务器的访问日志是黑客攻击的重点对象之一

注意:请求的URL 中不能有汉字

汉字转码:

 //    1.设置请求路径     NSString *urlStr=[NSString stringWithFormat:@"http://192.168.1.53:8080/MJServer/login?username=%@&pwd=%@",self.username.text,self.pwd.text];    //转码    urlStr= [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];     NSURL *url=[NSURL URLWithString:urlStr];     //    2.创建请求对象    NSURLRequest *request=[NSURLRequest requestWithURL:url];

链接:
http://www.cnblogs.com/wendingding/p/3813706.html

0 0
原创粉丝点击