【问题标题】:NSURLConnection method needs to be changed to pass authentication challenge需要更改 NSURLConnection 方法以通过身份验证质询
【发布时间】:2016-08-03 08:23:40
【问题描述】:

我有一种 http 连接方法,在我尝试使用无效 ssl 证书的服务器之前,该方法对我来说工作正常。 由于我正在使用

[NSURLConnection sendSynchronousRequest:returningResponse:error]

使用NSURLConnection 委托方法没有机会通过身份验证质询。

现在,我需要尽快更改我的服务调用代码。 我的方法返回从连接接收到的数据,这是我不能轻易改变我的主要问题 NSURLConnectioninitWithRequest:delegate:

我的服务调用方法如下;

-(id)serviceCall:(NSString*)str withURL:(NSString*)serviceUrl withIdentifier:(NSString*)type
{
    globalURL = [[NSURL alloc] initWithString:serviceUrl];
    shouldAllowSelfSignedCert = YES;

// if(ISDEBUG) NSLog(@"%@",serviceUrl);

    NSMutableDictionary* json;
    NSURLResponse* response;
    NSData* responseData = [NSMutableData data];

    NSError* error = nil;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:globalURL];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody: [str dataUsingEncoding: NSUTF8StringEncoding]];
    NSString* msgLength = [[NSString alloc] initWithFormat:@"%lu", (unsigned long)[str length]];
    [request addValue:@"text/json; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    request.timeoutInterval = 180;

     responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];


    if([type isEqualToString:@"image"])
    {
        if(ISDEBUG) NSLog(@"%@",responseData);
        return responseData;
    }
    else
    {
        if(error)
        {
            UIAlertView *message = [[UIAlertView alloc] initWithTitle:NO_WS_CONNECTION message:@"" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];

            if(ISDEBUG) NSLog(@"%@",error);
        }
        else
        {
            if(responseData !=nil)
            {
            json = [NSJSONSerialization
                    JSONObjectWithData:responseData

                    options:kNilOptions
                    error:&error];
            }
            else
            {

            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:NO_WS_CONNECTION delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];
            }
        }

    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

       if(ISDEBUG) NSLog(@"%@",responseString);      
   }

   return json;
}

我希望我足够清楚。

你有什么建议?

谢谢。

【问题讨论】:

    标签: ios objective-c nsurlconnection nsurlconnectiondelegate


    【解决方案1】:

    您应该有充分的理由同步进行,所以,我会尽量在不改变流程的情况下帮助您。

    尝试将请求包装到一个类中,您可以在其中使用initWithRequest:delegate: 实现请求,并使用块使该类返回响应。

    你会得到类似的东西:

    [YourRequestClass requestWithURL:serviceURL callback:^(NSData *yourData, NSError *error){
    
    }];
    

    好的,此时您有了一个新工具,它可以发出异步请求,为您制作身份验证质询并在块上返回结果。

    现在,您可以简单地使用dispatch_semaphore 来阻塞您的线程,直到请求返回响应......

    -(id)serviceCall:(NSString*)str withURL:(NSString*)serviceUrl withIdentifier:(NSString*)type {
    
        __block NSData *myData = nil;
    
        dispatch_semaphore_t sem = dispatch_semaphore_create(0);
    
        [YourRequestClass requestWithURL:serviceUrl callback:^(NSData *yourData, NSError *error){
            //ignoring error (this is an example !)
            myData = yourData;
            dispatch_semaphore_signal(sem);
        }];
    
        dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    
        //Use myData and make yourObject here!
    
        return yourObject;
    }
    

    请注意,这只是一个示例,我只是想为您指出正确的方法……我没有测试此代码,但我相信它应该可以按预期工作!

    【讨论】:

    • 另外,您可以将密码添加到用户的钥匙串中,同步 URL 请求将默认使用这些凭据。话虽如此,从长远来看,转向异步风格绝对是正确的做法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多