UTF-8 类型在下载 NSURL 连接时不接受 >129 字节



有人能帮我找到这个问题的解决方案吗?下载的内容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为空

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

204返回码的意思是:

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

不要忽略http返回代码。

请参阅http响应定义:状态代码定义

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

最新更新