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字符串的更多相关文章

  1. Objective-C-使用NSMutableURLRequest发送POST请求,使用NSJSONSerialization解析JSON字符串

    NSString *reqData = @"Data="; NSData *postDatas = nil; NSString *urlPath = @"url" ...

  2. andlua,andlua发送http请求,并解析json数据

    andlua发送http请求,并解析json实例 import'cjson'import 'http'--导入cjson库url = 'https://www.baidu,com'--设置urlHtt ...

  3. 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址

    在线聊天项目结构图: 多用户登陆效果图: 多用户聊天效果图: 数据库效果图: 重新构建了Server类,使用了Gson方法,通过解析Json字符串,增加Info类,简化判断过程. Server类代码如 ...

  4. winform c# 请求网站,返回Json字符串

    private void callApibjhb() { //输出执行的开始时间 Console.WriteLine(string.Format("Bind {0}", DateT ...

  5. 请求接口获取的json 字符串 前后不能有 双引号

    请求接口获取的json 字符串 前后不能有 双引号 否则JSON.parse 转换会报错

  6. webService 发送soap请求,并解析返回的soap报文

    本例应用场景:要做一个webService测试功能,不局限于任何一种固定格式的webService,所以像axis,cxf等框架就不好用了.只有深入到webService的原理,通过发收soap报文, ...

  7. ExtJS发送POST请求 参数格式为JSON

    背景 这要从我比较懒说起.技术框架ExtJS + resteasy,默认请求方式是ajax get,这后台方法就要写很多@QueryParam来获取参数.我比较喜欢前台用ajax post请求,后台方 ...

  8. Java发送POST请求,参数为JSON格式,并接收返回JSON数据

    原文地址:https://blog.csdn.net/qq_26975307/article/details/82713725 /** * 发送post请求 * @param url 路径 * @pa ...

  9. NSJSONSerialization 组json字符串

    抄的网上的. 主要是组织列表部分 NSDictionary *song = [NSDictionary dictionaryWithObjectsAndKeys:",@"lengt ...

随机推荐

  1. Emit技术使用实例及应用思路

    System.Reflection.Emit提供了动态创建类并生成程序集的功能. 适用于.NET Framework 2.0及其以后的版本. 动态生成类在对于O/R Mapping来说有很大的作用,在 ...

  2. 1228.1——计算器(未使用MVC设计模式)

    #import "ViewController.h"typedef enum{    kStausNum,    kStausOperation}kStaus; typedef e ...

  3. hdu1161Eddy's mistakes

    Problem Description Eddy usually writes articles ,but he likes mixing the English letter uses, for e ...

  4. 给Array添加删除重复元素函数

    给Array本地对象增加一个原型方法,它用于删除数组中重复的元素(可能有多个重复), 返回值是一个包含被删除的重复条目的新数组. Array.prototype.distinct = function ...

  5. javascript获取页面文档内容

    <html> <head> <title>JavaScript基础</title> </head> <body> <p&g ...

  6. QAction系列详解

    QAction系列详解 一.QAction类详解 [详细描述] QAction类提供了抽象的用户界面action,这些action可以被放置在窗口部件中.        应用程序可以通过菜单,工具栏按 ...

  7. 八大排序c++可运行精简版,一目了然

    #include <iostream> using namespace std; // 插入排序开始===================== void insert_sort(int a ...

  8. linux命令行常用快捷键

    方向          <-前               后 ->删除ctrl + d      删除光标所在位置上的字符相当于VIM里x或者dlctrl + h      删除光标所在 ...

  9. C语言随笔_return答疑

    1. 例子,看实例2-2.这道题有同学会问,那个return有什么用?这么讲吧,return是个英文单词,中文意思是“返回”,用在程序里也是返回的意思,返回啥呢?返回一个值.在func函数中,retu ...

  10. UESTC_Can You Help God Wu CDOJ 582

    There is a boy named God Wu in UESTC ACM team. One day he is asked to finish a task. The task is tha ...