下载nsurlconnection时,utf-8类型不接受> 129字节

时间:2012-01-21 13:19:06

标签: objective-c web-services ios5

有人可以帮我找到解决此问题的方法吗?下载的内容iws> 2500字节,但转换后的nsstring返回空。

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

staus:204但strUserResponse为空

1 个答案:

答案 0 :(得分:2)

204 http响应代码表示没有内容,responseData将不包含任何内容,因此strUserResponse将不包含您想要的数据。

204返回代码表示:

  

服务器已收到请求,但没有要发送的信息   返回,客户端应保持在同一文档视图中。这是   主要是允许输入脚本而不更改文档   同一时间。

不要忽略http返回码。

请参阅http响应定义:Status Code Definitions

此外,对于这样的调试,您可以使用调试器和控制台或NSLog来验证回流,在这种情况下,responseData的值是多少。