2012-02-05 71 views
2

雖然有很多相關的問題,但我沒有看到一個將多個鍵/值對添加到NSURLRequest的地址。將鍵/值對添加到NSMutableURLRequest

我想爲請求添加一個簡單的用戶名和密碼。我不確定如何添加多個對以及編碼。我得到了有效的連接和響應,但響應表明它無法正確解釋請求。

這是我得到的。提前致謝。

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]; 

回答

5

您沒有使用正確NSString(你myRequestString,事實上,將顯示 「用戶名=」)。相反,試試這個:

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

而且這個偉大的答案,只是一個典型的例子代碼:

-(NSString *)buildKeyValuePostString 
    { 
    NSString *username = @"[email protected]"; 
    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 
    } 

希望它有助於某人。

+0

OK - 謝謝。但即使修復後我的請求仍然沒有正確解釋。用戶名和密碼的設置是否正確完成並編碼? – 2012-02-05 03:24:29

+0

嗯,除了'NSMutableString'部分,其餘的代碼看起來應該像你期望的那樣運行。我的代碼非常類似於您所寫的工作正常。 – donkim 2012-02-05 03:33:11

+0

NSString * myRequestString = @「h = 123」; NSData * myRequestData = [NSData dataWithBytes:[myRequestString UTF8String] length:[myRequestString length]];我們可以使用NSMutableURLRequest * request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@「http:// localhost:8082/45」]]; [請求setHTTPMethod:@「POST」]; [request setHTTPBody:myRequestData]; [request setValue:@「application/x-www-form-urlencoded」forHTTPHeaderField:@「content-type」]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; – donkim 2012-02-05 03:34:02

0

假設你的意思是你想HTTP報頭字段添加到請求,請使用:

-addValue:forHTTPHeaderField: