【问题标题】:Custom WebViewClient with HttpUrlConnection - To add headers for every request (WebView)使用 HttpUrlConnection 自定义 WebViewClient - 为每个请求添加标头 (WebView)
【发布时间】:2016-01-22 11:07:11
【问题描述】:

我有一个网络视图,我想向其中添加自定义标题,我希望它能够满足重定向并查看如何做到这一点,我偶然发现了投票最高的答案 here

但是,此解决方案使用的是现已弃用的方法。我尝试修改解决方案:

 public void webViewUrlConn() {
        new Thread(new Runnable() {

            @Override
            public void run() {

                try {
                    URL url = new URL("https://someUrl.com");

                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();


                    CookieManager cookieManager = CookieManager.getInstance();
                    String cookie = cookieManager.getCookie(url.getHost());
                    urlConnection.setDoOutput(true);
                    urlConnection.setRequestProperty("Cookie", cookie);
                    urlConnection.setRequestProperty("CRM_USER_AGENT", "crm_app");
                    urlConnection.setRequestMethod("POST");

                    InputStream in = new BufferedInputStream(urlConnection.getInputStream());


                    data = new java.util.Scanner(in).useDelimiter("\\A").next();

                    System.out.println("Data:" + data);

                    urlConnection.disconnect();
                } catch (Exception e) {
                    e.printStackTrace();
                }

                getActivity().runOnUiThread(new Runnable() {
                    public void run() {
                        wv.loadData(data, "text/html", "UTF-8");
                        pd.setVisibility(View.INVISIBLE);
                        wv.setVisibility(View.VISIBLE);

                    }
                });


            }
        }).start();
    }

这完美无瑕。但这不是我需要的。我想重写 WebViewClient 类以适应在 webview 上单击的链接,我想出了以下内容:

 WebViewClient webViewClient = new WebViewClient() {

            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url1) {

                try {
                    URL url = new URL(url1);

                    HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();


                    CookieManager cookieManager = CookieManager.getInstance();
                    String cookie = cookieManager.getCookie(url.getHost());
                    urlConnection.setDoOutput(true);
                    urlConnection.setRequestProperty("Cookie", cookie);
                    urlConnection.setRequestProperty("CRM_USER_AGENT", "crm_app");
                    urlConnection.setRequestMethod("POST");

                    InputStream in = new BufferedInputStream(urlConnection.getInputStream());


                    data = new java.util.Scanner(in).useDelimiter("\\A").next();

                    System.out.println("Data:" + data);


                    urlConnection.getContentType();
                    urlConnection.getContentEncoding();
                    urlConnection.getContentEncoding();

                    urlConnection.disconnect();



                    return new WebResourceResponse("text/html","UTF-8", in);

                } catch (Exception e) {
                    e.printStackTrace();

                    return null;
                }


            }
        };


        wv.setWebViewClient(webViewClient);

        pd.setVisibility(View.INVISIBLE);
        wv.setVisibility(View.VISIBLE);
        wv.loadUrl("https://someurl.com");

上述方法不起作用,我无法确定原因 - 我得到一个空白的 webview。当我从data 打印值时,我得到了一些未知字符。

【问题讨论】:

  • 我得到一个空白的 webview。 因为扫描仪消耗流 当我从数据中打印值时可能服务器正在使用压缩...
  • 我会检查评论扫描仪,我不知道 consuming 数据和我的 in 变量无法使用。就压缩而言,我认为其他代码示例也应该是这种情况,但它完美无缺。

标签: android android-webview httpurlconnection


【解决方案1】:

我还尝试向 Webview 请求添加自定义标头,这就是我偶然发现您的问题的原因。我发现另一个问题解决了与an answer that worked for me 类似的问题,因此它可能也可以为您解决问题:

我有同样的问题。如果你删除

urlConnection.disconnect();

它有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-13
    • 1970-01-01
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多