IOS Http POST问题

时间:2015-01-20 11:12:52

标签: ios objective-c

请求数据

NSDictionary *tmp = @{@"name":@"Kousik",@"age":@"24",@"location":@"bangalore"};

NSString *postdata = [NSString stringWithFormat:@"request = %@",tmp];

//now postdata is
//request = {
    "age" = "24";
    "location" = "bangalore";
    "name" = "Kousik";
}

但是我希望这个NSDictionary应该在一个字符串中,这样在服务器中我可以eval它并获取字典。

在这里,我希望像toString()这样的东西是python中的java或str()

这是我的Http请求: -

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:path]];
[request setHTTPMethod:@"POST"];

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];


    NSError *error;

    NSData *preparedPostData = [postdata dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postdata length]];

    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:preparedPostData ]; 


[NSURLConnection sendAsynchronousRequest:request
                                   queue:backgroundQueue
                       completionHandler:^(NSURLResponse response,NSData data,NSError *error){
                           NSString *result = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
                           if(complect)
                               complect(result,error);
                       }
 ];  

更新

我的请求将正常运行,但问题在于数据结构。 当我尝试使用request密钥访问数据时,它会给出错误,因为服务器不支持请求密钥的值,因为此值之间有=个签名。所以我想要的是正确地制作requset数据结构。想要将NSDictionary本身作为字符串发送。

postdata应该是这样的

request =  "{
        age : 24;
        location : bangalore;
        name : Kousik;
    }"

我不想使用application / json。

2 个答案:

答案 0 :(得分:0)

    NSString *stringURL = [NSString stringWithFormat:@"name=%@&age=%@&location=%@",@"Kousik",@"24",@"bangalore"];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerURL,API_SaveContctListToAddressBook]]];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[stringURL dataUsingEncoding:NSUTF8StringEncoding]];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];


    //    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:[NSOperationQueue mainQueue]
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
                               if (!data) {
                                   data = [[NSData alloc] init];
                               }
                               NSDictionary *dic =[ NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&connectionError];

                               completionHandler(dic, NO);


                           }];

答案 1 :(得分:0)

使用NSJSONSerialization将字典转换为NSData,然后将其附加到HTTPBody

NSData *httpBody = [NSJSONSerialization dataWithJSONObject:dict options:0 error:nil];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",ServerURL,APIPath]]];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:httpBody];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//Handling code
                       }];