【发布时间】:2016-08-03 08:23:40
【问题描述】:
我有一种 http 连接方法,在我尝试使用无效 ssl 证书的服务器之前,该方法对我来说工作正常。 由于我正在使用
[NSURLConnection sendSynchronousRequest:returningResponse:error]
使用NSURLConnection 委托方法没有机会通过身份验证质询。
现在,我需要尽快更改我的服务调用代码。
我的方法返回从连接接收到的数据,这是我不能轻易改变我的主要问题
NSURLConnection 到 initWithRequest: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