Objective-C-使用NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串
NSString *reqData = @"Data=";
NSData *postDatas = nil;
NSString *urlPath = @"url"; //组json字符串数据
NSMutableDictionary *mulDic = [[[NSMutableDictionary alloc] init] autorelease];
[mulDic setValue:@"P003" forKey:@"type"]; //NSJSONSerialization 组json字符串
if ([NSJSONSerialization isValidJSONObject:mulDic]) { postDatas = [NSJSONSerialization dataWithJSONObject:mulDic options:NSJSONWritingPrettyPrinted error:nil];
NSString *str = [[NSString alloc] initWithData:postDatas encoding:NSUTF8StringEncoding];
reqData = [reqData stringByAppendingString:str];
NSLog(@"reqData:%@",reqData); //NSData *postData = [reqData dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
postDatas = [NSData dataWithBytes:[reqData UTF8String] length:[reqData length]]; } NSString *len = [NSString stringWithFormat:@"%d",(int)[postDatas length]];
NSURL *url = [NSURL URLWithString:urlPath];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
//[request setValue:@"*/*" forKey:@"accept"];
//[request setValue:@"Keep-Alive" forKey:@"connection"];
//[request setValue:@"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)" forKey:@"user-agent"];
//[request setValue:len forHTTPHeaderField:@"Content-Length"];
//[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postDatas]; NSData *retData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *ret = [[NSString alloc] initWithData:retData encoding:NSUTF8StringEncoding];
NSLog(@"%@",ret);
//解析返回的数据JSON格式字符串
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:retData options:NSJSONReadingMutableLeaves error:nil];
NSLog(@"%@",dic);
Objective-C-使用NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串的更多相关文章
- Objective-C——NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串
NSString *reqData = @"Data="; NSData *postDatas = nil; NSString *urlPath = @"url" ...
- andlua,andlua发送http请求,并解析json数据
andlua发送http请求,并解析json实例 import'cjson'import 'http'--导入cjson库url = 'https://www.baidu,com'--设置urlHtt ...
- 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址
在线聊天项目结构图: 多用户登陆效果图: 多用户聊天效果图: 数据库效果图: 重新构建了Server类,使用了Gson方法,通过解析Json字符串,增加Info类,简化判断过程. Server类代码如 ...
- winform c# 请求网站,返回Json字符串
private void callApibjhb() { //输出执行的开始时间 Console.WriteLine(string.Format("Bind {0}", DateT ...
- 请求接口获取的json 字符串 前后不能有 双引号
请求接口获取的json 字符串 前后不能有 双引号 否则JSON.parse 转换会报错
- webService 发送soap请求,并解析返回的soap报文
本例应用场景:要做一个webService测试功能,不局限于任何一种固定格式的webService,所以像axis,cxf等框架就不好用了.只有深入到webService的原理,通过发收soap报文, ...
- ExtJS发送POST请求 参数格式为JSON
背景 这要从我比较懒说起.技术框架ExtJS + resteasy,默认请求方式是ajax get,这后台方法就要写很多@QueryParam来获取参数.我比较喜欢前台用ajax post请求,后台方 ...
- Java发送POST请求,参数为JSON格式,并接收返回JSON数据
原文地址:https://blog.csdn.net/qq_26975307/article/details/82713725 /** * 发送post请求 * @param url 路径 * @pa ...
- Dynemic Web Project中使用servlet的 doGet()方法接收来自浏览器客户端发送的add学生信息形成json字符串输出到浏览器并保存到本地磁盘文件
package com.swift.servlet; import java.io.FileOutputStream;import java.io.IOException;import java.io ...
随机推荐
- 解决Matlab启动时 " Can't check 9.0 VCRTs The application has failed to start because……" 的错误
注:转载或引用请注明出处 今天在winserver 2012 r2 上安装matlab 2016b , 安装完成运行时提示: ERROR: Cnn't check 9.0 VCRTs <star ...
- bin(x) 将整数x转换为二进制字符串
>>> a 122 >>> b 344 >>> c = bin(a) >>> d = bin(b) >>> c ...
- 【Firefly API 新版文档】Package dbentrust
原地址:http://blog.csdn.net/uxqclm/article/details/11969653 该包下面主要是数据库的处理与memcached存储.里面封装了,从memcached数 ...
- 周末“干活”之 Mesos Meetup
周末两天都是大雾霾天,作为运营也不能在家宅,告别了技术就得腿儿勤点儿. 非常感谢 Linker 的 Sam Chen 和 数人科技 的 CTO 共同组织的Mesos Meetup,OneAPM 最帅的 ...
- 关于PIL库的一些概念
关于PIL库的一些概念 pil能处理的图片类型pil可以处理光栅图片(像素数据组成的的块). 通道一个图片可以包含一到多个数据通道,如果这些通道具有相同的维数和深度,Pil允许将这些通道进行叠加 模式 ...
- php or die用法
当前面的函数运行出错时,终止并输入提示.常见的用法如:mysql_connect('locahost','root','') or die('数据库连接失败');
- 【Linux安全】文件或目录权限设置
实例演示: 要求:新建文件夹,名称为secure,要求改文件夹只能被创建者deadmin以及与deadmin同组用户进行读.写.执行的权限, 其他用户均只有读的权限. 查看 一下secure原本的权限 ...
- 使用 jQuery.i18n.properties 实现 Web 前端的国际化
jQuery.i18n.properties 简介 在介绍 jQuery.i18n.properties 之前,我们先来看一下什么是国际化.国际化英文单词为:Internationalization, ...
- ORACLE查看锁(lock)情况
SELECT /*+ RULE */ ls.osuser os_user_name, ls.username user_name, DECODE ...
- BZOJ_3270_博物馆_(高斯消元+期望动态规划+矩阵)
描述 http://www.lydsy.com/JudgeOnline/problem.php?id=3270 \(n\)个房间,刚开始两个人分别在\(a,b\),每分钟在第\(i\)个房间有\(p[ ...