【问题标题】:Add key/value pairs to NSMutableURLRequest向 NSMutableURLRequest 添加键/值对
【发布时间】:2012-02-05 03:09:08
【问题描述】:

虽然有很多相关的问题,但我没有看到一个解决向 NSURLRequest 添加多个键/值对的问题。

我想在请求中添加一个简单的用户名和密码。我不确定如何添加多对以及编码。我得到了有效的连接和响应,但响应表明它无法正确解释请求。

这就是我所拥有的。提前致谢。

NSURL *authenticateURL = [[NSURL alloc] initWithString:@"https://www.the website.com/authenticate"];
NSMutableURLRequest *authenticateRequest = [[NSMutableURLRequest alloc] initWithURL:authenticateURL];
[authenticateRequest setHTTPMethod:@"POST"];
NSString *myRequestString = @"username=";
[myRequestString stringByAppendingString:username];
[myRequestString stringByAppendingString:@"&"];
[myRequestString stringByAppendingString:@"password="];
[myRequestString stringByAppendingString:password];
NSData *requestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]];
[authenticateRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"content-type"];
[authenticateRequest setHTTPBody: requestData];
[authenticateRequest setTimeoutInterval:30.0];

connection = [[NSURLConnection alloc] initWithRequest:authenticateRequest delegate:self]; 

【问题讨论】:

    标签: ios cocoa-touch


    【解决方案1】:

    您没有正确使用NSString(实际上,您的myRequestString 将读取“用户名=”)。相反,试试这个:

    NSMutableString *myRequestString = [NSMutableString stringWithString:@"username="];
    [myRequestString appendString:username];
    [myRequestString appendString:@"&password="];
    [myRequestString appendString:password];
    

    除了这个很好的答案,只是一个典型的示例代码:

    -(NSString *)buildKeyValuePostString
        {
        NSString *username = @"boss@apple.com";
        NSString *password = @"macintosh";
    
        NSMutableString *r = [NSMutableString stringWithString:@""];
    
        [r appendString:@"command=listFileNames"];
        [r appendString:@"&"];
    
        [r appendString:@"name=blah"];
        [r appendString:@"&"];
    
        [r appendString:@"user="];
        [r appendString: [username stringByUrlEncoding] ];
        [r appendString:@"&"];
    
        [r appendString:@"password="];
        [r appendString: [password stringByUrlEncoding] ];
    
        return r;
        }
    

    这是完成 url 编码的困难/烦人工作的类别

    -(NSString *)stringByUrlEncoding
        {
        return (NSString *)CFBridgingRelease(
                 CFURLCreateStringByAddingPercentEscapes(
                    NULL,
                    (CFStringRef)self,
                    NULL,
                    (CFStringRef)@"!*'();:@&=+$,/?%#[]",
                    kCFStringEncodingUTF8)
                    );
    
        // with thanks to http://www.cocoanetics.com/2009/08/url-encoding/
        // modified for ARC use 2014
        }
    

    希望对某人有所帮助。

    【讨论】:

    • 好的 - 谢谢。但即使在修复之后,我的请求仍然没有被正确解释。用户名和密码的设置是否正确并编码?
    • 嗯,除了NSMutableString 部分之外,其余代码看起来应该可以按预期运行。我的代码与您编写的代码非常相似。
    • NSString *myRequestString = @"h=123"; NSData *myRequestData = [NSData dataWithBytes:[myRequestString UTF8String] 长度:[myRequestString 长度]]; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"localhost:8082/45"]]; [request setHTTPMethod: @"POST"]; [request setHTTPBody:myRequestData]; [request setValue:@"application/x-www- form-urlencoded" forHTTPHeaderField:@"content-type"]; [[NSURLConnection alloc] initWithRequest:request delegate:self];
    • 抱歉格式化.. 猜它不适用于 cmets?无论如何,这是我的代码。你确定你的字符串是按预期创建的吗?我会做一个 NSLog 只是为了确保..
    • 你是对的。我使用了无效的方法来附加字符串。应该一直在使用 appendString。它现在正在工作。非常感谢。
    【解决方案2】:

    假设您的意思是要在请求中添加 HTTP 标头字段,请使用:

    -addValue:forHTTPHeaderField:
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-25
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多