簡體   English   中英

我如何使用post方法將字典參數傳遞給url

[英]How can i pass dictionary parameter to url using post method

我很累這樣做..但是我無法放松。

 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內容類型vale

[為HTTPHeaderField:@“ Content-Type”請求setValue:@“ application / json”];

我希望這會解決..謝謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM