개발/안드로이드

사이트가 HTTPS이지만 WebView에서 ERR_CLEARTEXT_NOT_PERMITED 발생

맨날치킨 2022. 12. 5. 08:05
반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

WebView showing ERR_CLEARTEXT_NOT_PERMITTED although site is HTTPS

사이트가 HTTPS이지만 WebView에서 ERR_CLEARTEXT_NOT_PERMITED 발생

 문제 내용 

I'm starting to work on an app on Android, so I don't have much. All I have is just a WebView so far. I created the project in Android Studio, and my project got set as an Android InstantApp. I'm not sure why/how, but my guess is that I overlooked an option for it when creating the project.

Android에서 앱 작업을 시작했기 때문에 많은 것이 없습니다. 내가 가진 것은 지금까지 WebView뿐입니다. Android Studio에서 프로젝트를 만들었고 프로젝트가 Android InstantApp으로 설정되었습니다. 이유/방법은 잘 모르겠지만 프로젝트를 만들 때 옵션을 간과한 것 같습니다.

 

I was getting an error from the WebView saying net::ERR_CLEARTEXT_NOT_PERMITTED. When I googled the error, I saw that when an app is an InstantApp, WebViews can only load sites that are HTTPS, and cannot load an HTTP site.

WebView에서 net:ERR_CLEARTEXT_NOT_PERMITED이라는 오류가 발생했습니다. Google에서 오류를 검색했을 때 앱이 InstantApp일 때 웹뷰는 HTTPS인 사이트만 로드할 수 있고 HTTP 사이트는 로드할 수 없습니다.

 

The purpose of this app is to be an extremely simple Flash player for only one site. This is to have better performance running a game that requires Flash. This game is at darkorbit.com, which is HTTPS.

이 앱의 목적은 단 하나의 사이트를 위한 매우 간단한 플래시 플레이어가 되는 것입니다. 이는 Flash가 필요한 게임을 실행하는 성능을 향상시키기 위한 것입니다. 이 게임은 HTTPS인 darkorbit.com에 있습니다.

 

MainActivity.java:

package com.tylerr147.darkorbit;

import android.content.ComponentName;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView wv = findViewById(R.id.webView1);
        wv.loadUrl("https://darkorbit.com/");
        wv.setWebViewClient(new CustomWebViewClient());
        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setPluginState(WebSettings.PluginState.ON);

    }
}

 

and CustomWebViewClient.java

package com.tylerr147.darkorbit;

import android.webkit.WebView;
import android.webkit.WebViewClient;

public class CustomWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

 

My question: How can I disable my app as an InstantApp, or how can I get this WebView to display the site?

내 질문: 앱을 InstantApp으로 비활성화하려면 어떻게 해야 합니까? 또는 이 WebView를 통해 사이트를 표시하려면 어떻게 해야 합니까?

 

I feel like it's important I mention a few other details too: In the app, where it is showing the WebView, it also says "The webpage at http://darkorbit.com/" could not be loaded because: net::ERR_CLEARTEXT_NOT_PERMITTED

몇 가지 다른 세부 사항도 언급하는 것이 중요하다고 생각합니다. WebView를 표시하는 앱에서 "http://darkorbit.com/의 웹 페이지"를 로드할 수 없습니다. 이유는 다음과 같습니다. net::ERR_CLEARTEXT_NOT_PERMITTED

 

Notice that is says "... site at http://darkorbit.com/ ...", and not "... site at https://darkorbit.com/ ..." even though the string for the URL is hardcoded, and says "https://darkorbit.com/". Also, I am testing the app on an emulator set up as a Google Pixel 2 running Android 9.

URL의 문자열이 하드코딩되어 있고 "https://darkorbit.com/..."로 표시되어 있더라도 "https://darkorbit.com/..."이 아닌 "...site at http://darkorbit.com/..."로 표시됩니다. 또한 안드로이드 9를 구동하는 구글 픽셀 2로 설정된 에뮬레이터에서 앱을 테스트하고 있다.

 

Any help would be appreciated. Thank you.

어떤 도움이라도 주시면 감사하겠습니다. 감사해요.

 

 

 

 높은 점수를 받은 Solution 

Solution:

솔루션:

 

Add the below line in your application tag:

응용프로그램 태그에 다음 줄을 추가합니다.
android:usesCleartextTraffic="true"

 

As shown below:

아래와 같이:
<application
    ....
    android:usesCleartextTraffic="true"
    ....>

 

UPDATE: If you have network security config such as: android:networkSecurityConfig="@xml/network_security_config"

업데이트: 다음과 같은 네트워크 보안 구성이 있는 경우: Android:networkSecurityConfig="@xml/network_security_config"

 

No Need to set clear text traffic to true as shown above, instead use the below code:

위에 표시된 것처럼 일반 텍스트 트래픽을 true로 설정할 필요가 없습니다. 대신 아래 코드를 사용하십시오.
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        ....
        ....
    </domain-config>

    <base-config cleartextTrafficPermitted="false"/>
</network-security-config>  

 

Set the cleartextTrafficPermitted to true

cleartextTrafficPermitted를 true로 설정합니다.

 

 

 

 가장 최근 달린 Solution 

When you call "https://darkorbit.com/" your server figures that it's missing "www" so it redirects the call to "http://www.darkorbit.com/" and then to "https://www.darkorbit.com/", your WebView call is blocked at the first redirection as it's a "http" call. You can call "https://www.darkorbit.com/" instead and it will solve the issue.

"https://darkorbit.com/"을 호출하면 서버는 "www"가 누락된 것으로 파악하여 호출을 "http://www.darkorbit.com/"으로 리디렉션한 다음 "https://www. darkorbit.com/"에서 WebView 호출은 "http" 호출이므로 첫 번째 리디렉션에서 차단됩니다. 대신 "https://www.darkorbit.com/"에 전화하면 문제가 해결됩니다.

 

 

 

출처 : https://stackoverflow.com/questions/52707918/webview-showing-err-cleartext-not-permitted-although-site-is-https

반응형