2012-01-21 52 views
0

任何人都可以幫助我找到解決此問題的解決方案嗎?下載的內容iws> 2500字節,但轉換後的nsstring返回空。utf-8類型在下載時不接受> 129字節nsurlconnection

NSMutableString *postString = [NSMutableString string]; 
    // NSMutableString *postString = [[NSMutableString alloc] init]; 
[postString appendFormat:@"username=%@",[strUsrName dataUsingEncoding:NSUTF8StringEncoding]]; 
[postString appendFormat:@"&password=%@",[strPasswrd dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postString appendFormat:@"&client_assertion_type=%@",[@"JWT"dataUsingEncoding:NSUTF8StringEncoding]]; 
[postString appendFormat:@"&client_assertion=%@",[strAppTknContent dataUsingEncoding:NSUTF8StringEncoding]]; 
    [postString appendFormat:@"&spEntityID=%@",[@"http://localhost:8080/opensso"dataUsingEncoding:NSUTF8StringEncoding]] ; 

NSHTTPURLResponse *response = nil; 
    NSError *error = nil;   
    NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] initWithURL:nsurl]; 
    NSData* dataRequest = [postString dataUsingEncoding: NSISOLatin1StringEncoding ]; 
    // NSData* dataRequest = [NSData dataWithBytes:strRequest]; 
    [theRequest setHTTPMethod: @"POST"]; 
    [theRequest setHTTPBody: dataRequest]; 
    [theRequest setURL:nsurl]; 

    //// set headers 
    NSString *contentType = [NSString stringWithFormat:@"text/plain"]; 
    [theRequest setValue:contentType forHTTPHeaderField:@"Content-Type"]; 
    NSString *acceptType = [NSString stringWithFormat:@"application/json"]; 
    [theRequest setValue:acceptType forHTTPHeaderField:@"accept"]; 
    [theRequest setTimeoutInterval:3.0]; 
    [theRequest setCachePolicy:NSURLRequestReturnCacheDataElseLoad];   



NSData *responseData =[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error]; 
NSString *strUserResponse =[[NSString alloc]initWithData:responseData encoding:NSISOLatin1StringEncoding]; 
    int status =[response statusCode ] ; 

居留制:204,但strUserResponse是空

+1

dataRequest'實際包含什麼?將字符串附加到「postString」後將字符串轉換爲UTF-8,然後將其轉換爲Latin-1,所以我並不驚訝數據被截斷。 – sbooth

+2

204是「沒有內容」,你怎麼能期待一個responseData? –

回答

2

的204 HTTP響應代碼意味着沒有內容,responseData將包含什麼,以便它遵循strUserResponse將不包含你想要的數據。

的204返回碼的意思是:

服務器收到請求,但沒有任何信息發送回 ,客戶端應該留在同一個文檔視圖。這是 ,主要是爲了允許輸入腳本,而不用同時更改文檔 。

不要忽略http返回碼。

查看HTTP響應的定義:Status Code Definitions

而且,調試這樣的事情,你可以使用調試器和控制檯或NSLog的驗證迴流,在這種情況下,是什麼responseData值。