post NSURLConnection请求网络数据
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self httpSendDataWithUrl:@"http://127.0.0.1/php/login.php" andUserName:@"zhang&san" andPsw:@"zhang"];
}
-(void)httpSendDataWithUrl:(NSString*)url andUserName:(NSString*)username andPsw:(NSString*)psw
{
//get请求的时候URL中的汉字空格,特殊字符不会进行编码,如果不自行编码,会出现错喔
NSString *name=username;
//oc 自带方法只会对空格汉字进行转义,不会转义特殊字符
name=[name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//对特殊字符进行转义
name=[self encodeToPercentEscapeString:name];
NSString *pwd=psw;
NSString *httpUrl=url;
//----------------------Get方法的URL-------------
// NSString *str=[NSString stringWithFormat:@"?username=%@&password=%@",name,pwd];
// httpUrl=[httpUrl stringByAppendingString:str];
// NSURLRequest*request=[NSURLRequest requestWithURL:[NSURL URLWithString:httpUrl]];
//----------------------Get方法的URL-------------
//----------------------Post方法的URL和请求头-----------
NSMutableURLRequest *mRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:httpUrl]];
mRequest.HTTPMethod=@"post";
NSString *strBody=[NSString stringWithFormat:@"username=%@&password=%@",name,pwd];
mRequest.HTTPBody=[strBody dataUsingEncoding:NSUTF8StringEncoding];
//----------------------Post方法的URL和请求头-----------
[NSURLConnection sendAsynchronousRequest:mRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (!connectionError) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200) {
NSError *error=nil;
id json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
if (error) {
NSLog(@"json解析错误!!!!!!");
}
else
{
NSLog(@"%@",json);
}
}else{
NSLog(@"服务器内部错误");
}
}else{
NSLog(@"请求错误%@",connectionError);
}
}];
}
//进行url编码 (但是不对汉字和空格进行编码)
- (NSString *)encodeToPercentEscapeString: (NSString *) input
{
NSString *outputStr = (NSString *) CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)input,NULL,(CFStringRef)@"!*'();:@&=+ $,/?%#[]",kCFStringEncodingUTF8));
return outputStr;
}
//url解码
- (NSString *)decodeFromPercentEscapeString: (NSString *) input
{
return [input
stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding] ;
}
@end
post NSURLConnection请求网络数据的更多相关文章
- Android 手机卫士--构建服务端json、请求网络数据
本文地址:http://www.cnblogs.com/wuyudong/p/5900384.html,转载请注明源地址. 数据的传递 客户端:发送http请求 http://www.oxx.com/ ...
- Android - 使用Volley请求网络数据
Android - 使用Volley请求网络数据 Android L : Android Studio 14 个人使用volley的小记,简述使用方法,不涉及volley源码 准备工作 导入Volle ...
- react-native 项目实战 -- 新闻客户端(4) -- 请求网络数据
1.Home.js /** * 首页 */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Te ...
- HttpURLConnection请求网络数据的Post请求
//--------全局变量----------- //注册Url private String urlPath="http://101.200.142.201:8080/VideoP ...
- HttpURLConnection请求网络数据
//使用线程 new Thread(){ public void run() { try { //先创建出了一 ...
- IOS开发中使用AFNetworking请求网络数据
1.把AFNetworking的文件拖放到项目中(注意不同的版本方法不一样,本历程基于版本2013): 2.使用#import "AFNetworking.h"命令把AFNetwo ...
- angularjs $http请求网络数据并展示
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- HttpClient请求网络数据的Post请求
new Thread(){ public void run() { try { //获得输入框内容 ...
- 解决Volley请求网络数据返回的数据乱码
本人可参考http://tieba.baidu.com/p/4039693566 以往一般我们如下写就可以了 StringRequest request=new StringRequest(url, ...
随机推荐
- Asynchronous fs.stat.isDirectory()
function showFile() { for(var i = 0; i< files.length; i++){ var itemFile = files[i]; fs.stat(__di ...
- Nginx简易配置文件(三)(文件缓存)
server { listen 80; listen 443 ssl; server_name user.17.net userapi.17.net; access_log logs/user/acc ...
- BZOJ1598: [Usaco2008 Mar]牛跑步
传送门 K短路,普遍的算法是采用AStar求解,先建立反向边跑一遍dij,或者spfa什么的.跑出反向边的距离就可以看为估价函数中的$h()$.设$dist$为当前已经走过的距离,那么$f(node) ...
- CSS3中的变形处理
在css3中,可以利用transform功能来实现文字或者图像的旋转.缩放.倾斜.移动这四种类型的变形处理. 旋转 旋转功能使用rotate方法参数中加入角度值,方向为顺时针旋转.示例清单如下: &l ...
- 安全测试及B/S C/S安全性比较
一.用户认证安全的测试要考虑问题: 1. 明确区分系统中不同用户权限 2. 系统中会不会出现用户冲突 3. 系统会不会因用户的权限的改变造成混乱 4. ...
- 如何在网页中添加“QQ交流”
今天在撸码时,想到这个问题,有些网页中会有诸如,那么如何在网页添加"QQ交谈"? 第一步.登录QQ: 第二步.打开网页:QQ推广,启用QQ通讯组件: 第三步.选择组件样式,设置提示 ...
- Authcode()
加密解密函数Authcode(): 1. // 参数解释 2. // $string: 明文 或 密文 3. // $operation:DECODE表示解密,其它表示加密 4. // ...
- css3多列样式
- Shell入门教程:Shell函数详解
Shell函数类似于Shell脚本,里面存放了一系列的指令,不过Shell的函数存在于内存,而不是硬盘文件,所以速度很快,另外,Shell还能对函数进行预处理,所以函数的启动比脚本更快. 1.函数定义 ...
- SVN版本库(访问权限)配置实例笔记
http://blog.csdn.net/zjianbo/article/details/8578297 SVN版本库(访问权限)配置实例笔记 本系列文章由ex_net(张建波)编写,转载请注明出处. ...