2013-12-18 43 views
2

我想在NSMutableURLRequest加載一個網址如下GET請求:GET請求使用太多的參數 - 的iOS

NSString *serverAddress = [NSString stringWithFormat:@"http://test.us.to:8080/api/offers?altId=%@&provider=%@&otherId=%@&tenantId=%@&createdAt=%@&otherOfferId=%@&postId=%@&pageId=%@&sourceUrl=%@&name=%@&description=%@&text=%@&category=%@&caption=%@&startTime=%@&expirationTime=%@&minPurchase=%@&numPurchases=%@&value=%@&percent=%@&count=%@&currency=%@&terms=%@&campaignId=%@&partnerId=%@&tenantIdAtPartner=%@&issuerName=%@&claimLimit=%@&onePerUser=%@&emailTemplateFile=%@",@"test",@"Facebook",@"",@"test",@"null",@"test",@"",@"",@"http://test.us.to/offers/harvester_summer13.html",@"Harvester",@"Harvester 2for1 TakeAway",@"Enjoy!",@"voucher",@"Harvester 2for1 TakeAway",[NSNumber numberWithInt:0],@"test",[NSNumber numberWithInt:0],[NSNumber numberWithInt:0],@"1000",[NSNumber numberWithInt:0],[NSNumber numberWithInt:0],@"GBP",@"",[NSNumber numberWithInt:1259],@"null" ,@"null" ,@"null",[NSNumber numberWithInt:100],[NSNumber numberWithBool:false],@"templates/test.vm"]; 

NSURL *url = [NSURL URLWithString:[serverAddress urlEncodeUsingEncoding:NSUTF8StringEncoding]]; 

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                 cachePolicy:NSURLRequestReloadRevalidatingCacheData timeoutInterval:15.0]; 

NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"testuser", @"testpwd"]; 
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding]; 
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedStringWithOptions:0]]; 
[request setValue:authValue forHTTPHeaderField:@"Authorization"]; 



[request setHTTPMethod: @"GET"]; 

NSError *requestError; 
NSURLResponse *urlResponse = nil; 

NSData *response1 = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError]; 

的NSLog(@ 「輸出數據%@」,響應1);

我猜我加載使用URL字符串太多的參數。有沒有更好的方法來傳遞GET請求的參數?電流輸出爲空的響應1

+2

你怎麼知道你有太多的參數? – rmaddy

+0

GET請求沒有限制嗎? 256個字符? –

+0

它看起來像你編碼整個serverAddress字符串。你不應該只是在URL地址之後編碼參數嗎? – Scampbell

回答

4

它看起來對我來說,問題是,你是用編碼某種編碼方法的整個網址,但你真的只需要它是否包含某些字符,每一個參數分別編碼。例如:

NSString *serverAddress = @"http://test.us.to:8080/api/offers"; 

NSString *altIdParameter = [@"Escape?This?String?" urlEncodeUsingEncoding:NSUTF8StringEncoding]; 
NSString *aUrlParameter = [@"http://test.us.to/offers/harvester_summer13.html" urlEncodeUsingEncoding:NSUTF8StringEncoding]; 

NSString *getRequestUrl = [NSString stringWithFormat:%@?altId=%@&urlParam=%@", serverAddress, altIdParameter, aUrlParameter]; 

NSURL *url = [NSURL URLWithString:getRequestUrl];