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, ...
随机推荐
- Alpha阶段第八次Scrum Meeting
情况简述 Alpha阶段第八次Scrum Meeting 敏捷开发起始时间 2016/10/31 00:00 敏捷开发终止时间 2016/10/32 00:00 会议基本内容摘要 跟助教进行了交流,明 ...
- sql语法:inner join on, left join on, right join on详细使用方法
inner join(等值连接) 只返回两个表中联结字段相等的行 left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有 ...
- 【原】fiddler修改线上的内容
摘要:当我们线上的代码出bug了,咋办呢?有时候本地的代码跟线上的代码还是运行环境还是有区别的.比如有些封装的方法需要运动到手机上可以调试,而浏览器是无法调试的.如果不想每次修改完再放上到测试环境看效 ...
- php桥接设计模式
<?php //桥接模式 abstract class info{ protected $send=null; public function __construct($send){ $this ...
- 面试题目——《CC150》排序与查找
面试题11.1:给定两个排序后的数组A和B,其中A的末端有足够的缓冲空间容纳B.编写一个方法,将B合并入A并排序. package cc150.sort_search; public class Me ...
- 收藏一些好用的fifo
1.Nordic库中的 E:\nRF52_SDK_0.9.2_dbc28c9\components\libraries\fifo app_fifo.c /* Copyright (c) 2013 No ...
- 【转】段错误调试神器 - Core Dump详解
from:http://www.embeddedlinux.org.cn/html/jishuzixun/201307/08-2594.html 段错误调试神器 - Core Dump详解 来源:互联 ...
- jaxb
一.简介 JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实 ...
- 微信小程序实质是什么? Hybrid App
微信小程序是一种不需要下载安装即可使用的应用,用户扫一扫或者搜一下即可打开应用.微信小程序实质是Hybrid技术的应用.Hybrid App(混合模式移动应用). 小程序能够更多的可以更多的调用手机本 ...
- java中的wait(),notify(),notifyAll(),synchronized方法
wait(),notify(),notifyAll()三个方法不是Thread的方法,而是Object的方法.意味着所有对象都有这三个方法,因为每个对象都有锁,所以自然也都有操作锁的方法了.这三个方法 ...