http请求 get post
来源:互联网 发布:淘宝远程装系统骗局 编辑:程序博客网 时间:2024/05/10 09:53
- (void)get {
NSString *name = @"张三";
NSString *pwd = @"zhang";
NSString *strUrl = [NSStringstringWithFormat:@"http://127.0.0.1/php/login.php?username=%@&password=%@",name,pwd];
//对汉字或者空格做百分号转义
strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSetURLQueryAllowedCharacterSet]];
//当地址中出现空格或者汉字 url返回nil
NSURL *url = [NSURLURLWithString:strUrl];
NSURLRequest *request = [NSURLRequestrequestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse *_Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError) {
NSLog(@"连接错误 %@",connectionError);
return;
}
//
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode ==200 || httpResponse.statusCode ==304) {
//解析数据
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@",dic);
}else{
NSLog(@"服务器内部错误");
}
}];
}
@end
........
- (void)post {
NSString *strUrl =@"http://127.0.0.1/php/login.php";
NSURL *url = [NSURLURLWithString:strUrl];
NSMutableURLRequest *request = [NSMutableURLRequestrequestWithURL:url];
//发送post请求
request.HTTPMethod = @"post";
//设置请求体
NSString *body =@"username=123&password=abc";
//把字符串转换成NSData对象
request.HTTPBody = [bodydataUsingEncoding:NSUTF8StringEncoding];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueuemainQueue] completionHandler:^(NSURLResponse *_Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
if (connectionError) {
NSLog(@"连接错误 %@",connectionError);
return;
}
//
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode ==200 || httpResponse.statusCode ==304) {
//解析数据
NSDictionary *dic = [NSJSONSerializationJSONObjectWithData:data options:0 error:NULL];
NSLog(@"%@",dic);
}else{
NSLog(@"服务器内部错误");
}
}];
}
- http post get 请求
- HTTP GET/POST 请求
- http post get请求
- http请求 get post
- http post get请求
- HTTP请求GET,POST
- Http请求 post get
- HTTP GET/POST请求区别
- HTTP GET/POST请求区别
- HTTP GET/POST请求区别
- HTTP GET/POST请求区别
- HTTP GET/POST请求区别
- HTTP GET/POST请求区别
- Android Http get post请求
- Android Http get post请求
- HTTP GET/POST请求区别
- Android Http get post请求
- Android Http get post请求
- 设计模式-iterator 简单模拟ArrayList与LinkList
- IO多路复用之poll总结
- BCGControlBar使用方法
- SQLite模糊查询
- 1082. Read Number in Chinese (25)
- http请求 get post
- Java数据结构----栈(Stack)源码分析和个人简单实现
- 安卓开发:两个活动之间携带数据的跳转
- phpmyadmin导入大文件
- 产生死锁的条件
- IO多路复用之epoll总结
- 面试之“两列布局”/ "三列布局"
- select、poll、epoll之间的区别总结[整理]
- Android事件分发机制完全解析,带你从源码的角度彻底理解(上)