Ios 需要更改NSURLConnection方法以通过身份验证挑战
我有一个http连接的方法,在我尝试使用无效ssl证书的服务器之前,它对我来说工作正常。 因为我正在使用Ios 需要更改NSURLConnection方法以通过身份验证挑战,ios,objective-c,nsurlconnection,nsurlconnectiondelegate,Ios,Objective C,Nsurlconnection,Nsurlconnectiondelegate,我有一个http连接的方法,在我尝试使用无效ssl证书的服务器之前,它对我来说工作正常。 因为我正在使用 [NSURLConnection sendSynchronousRequest:returningResponse:error] 使用NSURLConnection委托方法无法通过身份验证挑战 现在,我需要尽快更改我的服务呼叫代码。 我的方法返回从连接接收到的数据,这是我无法轻松更改的主要问题 NSURLConnectiontoinitWithRequest:delegate: 我的服务调
[NSURLConnection sendSynchronousRequest:returningResponse:error]
使用NSURLConnection
委托方法无法通过身份验证挑战
现在,我需要尽快更改我的服务呼叫代码。
我的方法返回从连接接收到的数据,这是我无法轻松更改的主要问题
NSURLConnection
toinitWithRequest: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;
}
我希望我足够清楚
你有什么建议
谢谢。您应该有一个很好的理由同步执行此操作,因此,我将尝试在不更改流程的情况下帮助您 尝试将请求包装到一个类中,在该类中可以使用
initWithRequest:delegate:
实现请求,并使该类使用block返回响应
您将有类似于:
[YourRequestClass requestWithURL:serviceURL callback:^(NSData *yourData, NSError *error){
}];
好的,现在您有了一个新的工具,它可以发出异步请求,为您进行身份验证挑战,并在块上返回结果
现在,您只需使用dispatch\u信号量
阻塞线程,直到请求返回响应
-(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请求将使用这些凭据。尽管如此,从长远来看,转向异步风格绝对是正确的。