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, ...
随机推荐
- IE8的parseInt
踩到一个IE8的parseInt的坑: Chrome:
- <<< 判断提交方式是get还是post
if("GET".equals(request.getMethod())){ System.out.println("提交方式是GET"); }else if( ...
- 友盟ionic多渠道自动签名app
这几天公司的运营部门要求添加流量统计功能,集成了友盟的插件,中间遇到很多的问题,在这里说明记录一下,为遇到相同问题的朋友们减少麻烦 问题一.插件的使用直接将友盟sdk系在放置到项目根目录下plugin ...
- java 判断String 是否为空
StringUtils.isBlank(null) = true StringUtils.isBlank("") = true StringUtils.isBlank(" ...
- Windows中创建桌面快捷方式
Windows中创建桌面快捷方式 -------------- -------------- -------------- --------------
- QQ空间爬虫最新分享,一天 400 万条数据(附代码地址)
http://mp.weixin.qq.com/s?__biz=MzAxMjUyNDQ5OA==&mid=2653552228&idx=1&sn=e476bf23556406c ...
- [Fluent NHibernate]第一个程序
目录 写在前面 Fluent Nhibernate简介 基本配置 总结 写在前面 在耗时两月,NHibernate系列出炉这篇文章中,很多园友说了Fluent Nhibernate的东东,也激起我的兴 ...
- Java 排序算法实现
package test; import java.util.Scanner; public class JavaSort { public static void quickSort(int a[] ...
- Android基础篇(一)
Android体系结构介绍 Android是一个移动开发平台,层次结构:操作系统(OS).中间件(Middle Ware).应用程序(Application) 具体: 操作系统(OS)-->各种 ...
- codevs1540 银河英雄传说
描述 公元五八○一年,地球居民迁移至金牛座α第二行星,在那里发表银河联邦创立宣言,同年改元为宇宙历元年,并开始向银河系深处拓展. 宇宙历七九九年,银河系的两大军事集团在巴米利恩星域爆发战争.泰山压顶集 ...