第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

UIWebView查看自簽名網(wǎng)站(沒有私人API,不是NSURLConnection)

UIWebView查看自簽名網(wǎng)站(沒有私人API,不是NSURLConnection)

人到中年有點(diǎn)甜 2019-08-30 17:25:22
有很多問題要問:我可以UIWebView查看自簽名的HTTPS網(wǎng)站嗎?答案總是涉及:使用私人api調(diào)用NSURLRequest:allowsAnyHTTPSCertificateForHost使用NSURLConnection代替和委托canAuthenticateAgainstProtectionSpace等對我來說,這些都不行。(1) - 表示我無法成功提交到應(yīng)用商店。(2) - 使用NSURLConnection意味著在加載初始HTML頁面后必須從服務(wù)器獲取的CSS,圖像和其他內(nèi)容。有誰知道如何使用UIWebView查看自簽名的https網(wǎng)頁,這不涉及上述兩種方法?或者 - 如果使用NSURLConnectioncan實(shí)際上可以用來渲染一個(gè)完整的CSS,圖像和其他所有的網(wǎng)頁 - 這將是偉大的!干杯,拉伸。
查看完整描述

3 回答

?
PIPIONE

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超9個(gè)贊

終于我明白了!


你能做的是:


UIWebView正常啟動(dòng)您的請求。然后 - 在webView:shouldStartLoadWithRequest- 我們回復(fù)NO,而是使用相同的請求啟動(dòng)NSURLConnection。


使用NSURLConnection,您可以與自簽名服務(wù)器通信,因?yàn)槲覀兡軌蛲ㄟ^額外的委托方法來控制身份驗(yàn)證UIWebView。因此,使用connection:didReceiveAuthenticationChallenge我們可以對自簽名服務(wù)器進(jìn)行身份驗(yàn)證。


然后,在connection:didReceiveData,我們?nèi)∠鸑SURLConnection請求,并使用UIWebView- 現(xiàn)在可以工作,再次啟動(dòng)相同的請求,因?yàn)槲覀円呀?jīng)通過服務(wù)器身份驗(yàn)證:)


以下是相關(guān)的代碼段。


注意:您將看到的實(shí)例變量具有以下類型: 

UIWebView *_web

NSURLConnection *_urlConnection

NSURLRequest *_request


(我使用實(shí)例var,_request因?yàn)樵谖业那闆r下,它是一個(gè)包含大量登錄詳細(xì)信息的POST,但如果需要,您可以更改為使用傳入的請求作為方法的參數(shù)。)


#pragma mark - Webview delegate


// Note: This method is particularly important. As the server is using a self signed certificate,

// we cannot use just UIWebView - as it doesn't allow for using self-certs. Instead, we stop the

// request in this method below, create an NSURLConnection (which can allow self-certs via the delegate methods

// which UIWebView does not have), authenticate using NSURLConnection, then use another UIWebView to complete

// the loading and viewing of the page. See connection:didReceiveAuthenticationChallenge to see how this works.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

{

    NSLog(@"Did start loading: %@ auth:%d", [[request URL] absoluteString], _authenticated);


    if (!_authenticated) {

        _authenticated = NO;


        _urlConnection = [[NSURLConnection alloc] initWithRequest:_request delegate:self];


        [_urlConnection start];


        return NO;

    }


    return YES;

}



#pragma mark - NURLConnection delegate


- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;

{

    NSLog(@"WebController Got auth challange via NSURLConnection");


    if ([challenge previousFailureCount] == 0)

    {

        _authenticated = YES;


        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];


        [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];


    } else

    {

        [[challenge sender] cancelAuthenticationChallenge:challenge];

    }

}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;

{

    NSLog(@"WebController received response via NSURLConnection");


    // remake a webview call now that authentication has passed ok.

    _authenticated = YES;

    [_web loadRequest:_request];


    // Cancel the URL connection otherwise we double up (webview + url connection, same url = no good!)

    [_urlConnection cancel];

}


// We use this method is to accept an untrusted site which unfortunately we need to do, as our PVM servers are self signed.

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace

{

    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];

}

我希望這能幫助其他人解決我遇到的同樣問題!


查看完整回答
反對 回復(fù) 2019-08-30
  • 3 回答
  • 0 關(guān)注
  • 640 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號