delight-im / Android-AdvancedWebView

Enhanced WebView component for Android that works as intended out of the box
MIT License
2.39k stars 574 forks source link

Sending custom header with every request in webview #103

Closed sudheeshde closed 7 years ago

sudheeshde commented 7 years ago

Hi, consider this as a question, I succeeded to pass my custom header that contains a token for accessing the webpage into advancedwebview. It initiall yloads the page, but when using POST with another url in the webview, my header (token) actually didn't passed to server. Why is that? Is my code is broken? I also referred issue #27. But couldn't catch the idea. Please help me. I'm attaching my code here.

`public class TableViewTest extends AppCompatActivity implements AdvancedWebView.Listener {

    SharedPreferences pref;
    boolean preventCaching = true;

    private static final String URL = "my-url";
    private AdvancedWebView mWebView;

    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_table_view_test);

        pref = getSharedPreferences("LoginActivity", Context.MODE_PRIVATE);
        final String acToken = pref.getString("token", "DEFAULT");

       //used this method to add headers with every request, not working
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "Bearer " + acToken);
        //mWebView.loadUrl(your url, headers);

        //webView = (WebView) findViewById(R.id.webView1Id);

        mWebView = (AdvancedWebView) findViewById(R.id.webview);
        mWebView.setListener(this, this);
        mWebView.setGeolocationEnabled(false);
        mWebView.setMixedContentAllowed(true);
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        //mWebView.setCookiesEnabled(true);
////////////////////////////////////////////////////////////////////////////////////////////
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
        mWebView.getSettings().setUseWideViewPort(true);

        mWebView.getSettings().setAppCacheEnabled(false);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        mWebView.clearCache(true);
////////////////////////////////////////////////////////////////////////////////
        //mWebView.setThirdPartyCookiesEnabled(true);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            if (0 != (getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE))
            { WebView.setWebContentsDebuggingEnabled(true); }
        }

        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public void onPageFinished(WebView view, String url) {
                Toast.makeText(TableViewTest.this, "Finished loading", Toast.LENGTH_SHORT).show();

/////////////////////////////////////////////////////////////////////////////////////////////
                mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NORMAL);
                mWebView.getSettings().setUseWideViewPort(true);

                mWebView.getSettings().setAppCacheEnabled(false);
                mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
                mWebView.clearCache(true);
                ////////////////////////////////////////////////////////////////////////
            }

        });
        mWebView.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onReceivedTitle(WebView view, String title) {
                super.onReceivedTitle(view, title);
                Toast.makeText(TableViewTest.this, title, Toast.LENGTH_SHORT).show();
            }

        });
       //default method for adding header for initial method in AdvancedWebview
        mWebView.addHttpHeader("Authorization", "Bearer " + acToken);
        mWebView.loadUrl(URL, headers);

//        webView.getSettings().setJavaScriptEnabled(true);
//        webView.getSettings().setBuiltInZoomControls(true);
//        webView.getSettings().setDomStorageEnabled(true);
//        webView.getSettings().setUseWideViewPort(true);

    }

    @SuppressLint("NewApi")
    @Override
    protected void onResume() {
        super.onResume();
        mWebView.onResume();
        // ...
    }

    @SuppressLint("NewApi")
    @Override
    protected void onPause() {
        mWebView.onPause();
        // ...
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mWebView.onDestroy();
        // ...
        super.onDestroy();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        mWebView.onActivityResult(requestCode, resultCode, intent);
        // ...
    }

    @Override
    public void onBackPressed() {
        if (!mWebView.onBackPressed()) { return; }
        // ...
        super.onBackPressed();
    }

    @Override
    public void onPageStarted(String url, Bitmap favicon) {
        mWebView.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onPageFinished(String url) {
        mWebView.setVisibility(View.VISIBLE);
        mWebView.clearCache(true);
        mWebView.getSettings().setAppCacheEnabled(false);
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    }

    @Override
    public void onPageError(int errorCode, String description, String failingUrl) {
        Toast.makeText(TableViewTest.this, "onPageError(errorCode = "+errorCode+",  description = "+description+",  failingUrl = "+failingUrl+")", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onDownloadRequested(String url, String suggestedFilename, String mimeType, long contentLength, String contentDisposition, String userAgent) {
        Toast.makeText(TableViewTest.this, "onDownloadRequested(url = "+url+",  suggestedFilename = "+suggestedFilename+",  mimeType = "+mimeType+",  contentLength = "+contentLength+",  contentDisposition = "+contentDisposition+",  userAgent = "+userAgent+")", Toast.LENGTH_LONG).show();

        /*if (AdvancedWebView.handleDownload(this, url, suggestedFilename)) {
            // download successfully handled
        }
        else {
            // download couldn't be handled because user has disabled download manager app on the device
        }*/
    }

    @Override
    public void onExternalPageRequest(String url) {
        Toast.makeText(TableViewTest.this, "onExternalPageRequest(url = "+url+")", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        webView.saveState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        webView.restoreState(savedInstanceState);
    }
}`
ocram commented 7 years ago

Don't worry, that problem is not your fault. But I'm afraid this also means there is no easy solution. Sorry!

It's explained in the Javadoc of the method that you're calling:

https://github.com/delight-im/Android-AdvancedWebView/blob/ab994097de27c1aa3b85ffc0b220c8fd83a446fd/Source/library/src/main/java/im/delight/android/webview/AdvancedWebView.java#L315

Unfortunately, there's no simple way to fix this, technically.

sudheeshde commented 7 years ago

Okay, thank you for the reply.

thorkous commented 6 years ago

@sudheeshde can you post the code to catch the headers?

giacomomasseron commented 6 years ago

Same issue here, anyone solved?