#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请求网络数据的更多相关文章

  1. Android 手机卫士--构建服务端json、请求网络数据

    本文地址:http://www.cnblogs.com/wuyudong/p/5900384.html,转载请注明源地址. 数据的传递 客户端:发送http请求 http://www.oxx.com/ ...

  2. Android - 使用Volley请求网络数据

    Android - 使用Volley请求网络数据 Android L : Android Studio 14 个人使用volley的小记,简述使用方法,不涉及volley源码 准备工作 导入Volle ...

  3. react-native 项目实战 -- 新闻客户端(4) -- 请求网络数据

    1.Home.js /** * 首页 */ import React, { Component } from 'react'; import { AppRegistry, StyleSheet, Te ...

  4. HttpURLConnection请求网络数据的Post请求

    //--------全局变量----------- //注册Url    private String urlPath="http://101.200.142.201:8080/VideoP ...

  5. HttpURLConnection请求网络数据

    //使用线程 new Thread(){            public void run() {                try {                    //先创建出了一 ...

  6. IOS开发中使用AFNetworking请求网络数据

    1.把AFNetworking的文件拖放到项目中(注意不同的版本方法不一样,本历程基于版本2013): 2.使用#import "AFNetworking.h"命令把AFNetwo ...

  7. angularjs $http请求网络数据并展示

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. HttpClient请求网络数据的Post请求

    new Thread(){            public void run() {                                try { //获得输入框内容          ...

  9. 解决Volley请求网络数据返回的数据乱码

    本人可参考http://tieba.baidu.com/p/4039693566 以往一般我们如下写就可以了 StringRequest request=new StringRequest(url, ...

随机推荐

  1. 【BZOJ-1123】BLO Tarjan 点双连通分量

    1123: [POI2008]BLO Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 970  Solved: 408[Submit][Status][ ...

  2. shell知识点小结

    目录 引言 shell中的数组 数组的定义 数组的使用 实际的例子 shell中大小的比较 shell中的括号 shell中函数的定义 杂项知识点 字符串转数组 常用判断标志 linux后台运行相关 ...

  3. usr/include/dispatch - dispatch_source

    博文一部分摘自:Parse分析,以下简称博文1(LeanCloud工程师针对Parse使用GCD的分析) 博文一部分摘自:GCD入门,以下简称博文2 建议先了解一下:BSD基础知识 在Dispatch ...

  4. 一个ERP项目实施工程师的若干体会

    本人在多年的工作中,参与了ERP的研发和实施,对ERP有较深的认识.在这里,根据自已的实施过程中的一些经历,把自已在实践中的一些体会贡献出来和大家共享,由于时间和精力所限,内容难免有不当之处,挂一漏万 ...

  5. 10月30日下午 PHP精确查询(模糊查询、模糊+关键字共同查询)

    1.一个条件的模糊查询 <body> <br /> <form action="main.php" method="post"&g ...

  6. [Unity3d]向量的过度方法以及拖尾效果

    Vector3.RotateTowards() 用法 public static function RotateTowards(current: Vector3, target: Vector3, m ...

  7. 电商总结(五)移动M站建设

    最近在一直在搞M站,也就是移动web站点.由于是第一次,也遇到了很多问题,所以把最近了解到的东西总结总结.聊一聊什么是移动M站,它有啥作用和优势. 也有人会问,M站和APP有什么不同? 1. APP ...

  8. go智能提示(重要)

    使用VIM开发go程序时,智能提示是一个大问题. 最终解决方案是使用 YCM,它是使用 gocode 来进行智能提示的.一切配置好之后,你会发现标准库和第三方库都可以智能提示,但自己写的包却不能,猜想 ...

  9. 快速傅里叶(FFT)的快速深度思考

    关于按时间抽取快速傅里叶(FFT)的快速理论深度思考 对于FFT基本理论参考维基百科或百度百科. 首先谈谈FFT的快速何来?大家都知道FFT是对DFT的改进变换而来,那么它究竟怎样改进,它改进的思想在 ...

  10. Flex Builder快捷键

    Flex几个最重要的快捷键 代码助手:Ctrl+Space(简体中文操作系统是Alt+/) 快速修正:Ctrl+1 单词补全:Alt+/ 打开外部Java文档:Shift+F2 显示搜索对话框:Ctr ...