ios - 如何使用 post 方法将字典参数传递给 url

标签 ios objective-c

我做这个很累..但我无法得到休息..

 NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys:@"access_name", @"about us", nil];
    NSDictionary *finalDict = [[NSDictionary alloc] initWithObjectsAndKeys:dict, @"cmscontent", nil];

    NSString * post =[NSString stringWithFormat:@"%@",finalDict];

    NSData *postdata= [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength=[NSString stringWithFormat:@"%lu",(unsigned long)[postdata length]];
    NSMutableURLRequest *request= [[NSMutableURLRequest alloc]init];


    NSString *str=@"http://my url...";
    [request setURL:[NSURL URLWithString:str]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postdata];
    NSError *error;
    NSURLResponse *response;

    NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    NSString *returnstring=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
    NSMutableDictionary *dict12=[returnstring JSONValue];

    NSLog(@"%@",dict12);

我的 json 字典参数是..

{
  "cmscontent": 
    {
          "access_name": "about us"
    }
}

所以请告诉我怎样才能得到回复?? 提前致谢。

最佳答案

更改 forHTTPHeaderField 内容类型值

[请求 setValue:@"application/json"for HTTPHeaderField:@"Content-Type"];

我希望这会解决.. 谢谢

关于ios - 如何使用 post 方法将字典参数传递给 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35551462/

相关文章:

ios - 在 iOS 上测试蓝牙

ios - 如何将核心数据中每个对象的小数值相加得出总数?

iphone - 更改 UITableViewCell 的编辑控件

objective-c - NSTabView Item 在 TabViewItem 中添加图标

iphone - 防止 View Controller 被重置 - UINavcontroller + Storyboard + Segue's

ios - 自定义 UITableViewCell 上的 UIGestureRecognizer。轻敲多个细胞

objective-c - 有没有人可以将 iOS 核心数据与外部库同步

iphone - 释放和自动释放

ios - 如何在动态 TableView 中有一个静态单元格?

objective-c - didSelectRowAtIndexPath : how to get old indexPath to deselect checkmark?