https上的NSURLConnection同步请求

Ani*_*nis 19 iphone authentication synchronous

谁能告诉我如何与https服务器进行同步调用?我可以使用以下委托方法在https服务器上执行异步请求.

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
Run Code Online (Sandbox Code Playgroud)

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
Run Code Online (Sandbox Code Playgroud)

但我需要做同步.

小智 24

//编码请求

NSData *postData = [xmlText dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

**//Calculating length of request**
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:requestUrlString]];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];
NSURLResponse* response;
NSError* error = nil;

//Capturing server response
NSData* result = [NSURLConnection sendSynchronousRequest:request  returningResponse:&response error:&error];
Run Code Online (Sandbox Code Playgroud)

  • 如果有数据也具有国际字符支持,则应首选使用NSUTF8StringEncoding,例如:德语,意大利语等.否则上述方法绝对完美. (2认同)

anq*_*anq 13

+ (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
Run Code Online (Sandbox Code Playgroud)

in NSUrlConnection应该可以正常使用https.

如果您想提供凭据,他们需要成为网址的一部分:( https://username:password@domain.tld/api/user.json).

无法提供NSURLConnection委托,因此如果您需要一些非标准的身份验证处理,则需要异步执行.


Lol*_*Run 6

这就是我做到的方式:而不是

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]
Run Code Online (Sandbox Code Playgroud)

我在包含类上创建了相同的方法实例,因为我们需要一个委托.并且不要使它成为单例,因此每个连接都有自己的独立变量,因为如果我们不这样做,并且在另一个连接完成之前恰好调用了两个连接,那么接收到的数据和循环的处理将无法相继交织在一起.

[[ClassNameHere new] sendSynchronousRequest:request returningResponse:&response error:&error]
Run Code Online (Sandbox Code Playgroud)

这样我就可以创建NSUrl连接并处理它(以同步方式,我们将看到如何),所以我不必更改任何以前编写的代码.

- (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse *__strong*)response error:(NSError *__strong*)error
{
    _finishedLoading=NO;
    _receivedData=[NSMutableData new];
    _error=error;
    _response=response;

    NSURLConnection*con=[NSURLConnection connectionWithRequest:request delegate:self];
    [con start];
    CFRunLoopRun();

    return _receivedData;
}


- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 
{
    //handle the challenge
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    *_response=response;
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    *_error=error;
    CFRunLoopStop(CFRunLoopGetCurrent());
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    CFRunLoopStop(CFRunLoopGetCurrent());
}
Run Code Online (Sandbox Code Playgroud)

诀窍在CFRunLoopRun()和CFRunLoopStop(CFRunLoopGetCurrent())我希望它可以帮助其他人在未来.

  • 我不会问为什么人们会回答一个250年前的问题,尤其是那个问题;但更重要的是,我想提一下你的代码没有正确验证,也不安全.所以,如果这只是测试代码,以摆脱证书,这是可以的.对于生产,它存在安全风险. (2认同)