How to make POST NSURLRequest with 2 parameters?

前端 未结 2 819
遥遥无期
遥遥无期 2020-11-30 07:57

I want to add 2 parameters to NSURLRequest. Is there a way or should I use AFnetworking?

2条回答
  •  时光说笑
    2020-11-30 08:31

    NSDictionary *params = @{@"firstname": @"John", @"lastname": @"Doe"};
    NSMutableString *str = [NSMutableString stringWithString:@"http://yoururl.com/postname?"];
    NSArray *keys = [params allKeys];
    NSInteger counter = 0;
    for (NSString *key in keys) {
        [str appendString:key];
        [str appendString:@"="];
        [str appendString:params[key]];
        if (++counter < keys.count) { // more params to come...
            [str appendString:@"&"];
        }
    }
    NSURL *url = [NSURL URLWithString:str];
    // should give you: http://yoururl.com/postname?firstname=John&lastname=Doe
    // not tested, though
    

提交回复
热议问题