在Android的WebView中添加cookie标头后,重写的shouldInterceptRequest()无法正常工作

时间:2019-01-18 22:50:35

标签: java android cookies

我在活动中具有WebView组件,并且在使用loadUrl(initialUrl)时,我想添加一个自定义cookie。添加cookie后,它将返回一个html页面(例如A)的响应,并且在页面A内,它将cookie作为隐藏的变量张贴到页面A中的另一个URL。这是代码:

public class WebViewActivity extends FragmentActivity {
    private WebView mWebView;

    String test_url = "https://myexample.com/oamfed/idp/initiatesso";
    String cookieString = "cookieName=cookieValue" + "; path=/";


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mWebView = (WebView) findViewById(R.id.webViewId);

        loadData();

    }

    private void loadData(){
        mWebView.invalidate();
        mWebView.getSettings().setJavaScriptEnabled(true);

        mWebView.setWebViewClient(new WebViewClient(){
            // Handle API until level 21
            @SuppressWarnings("deprecation")
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
                return getNewResponse_new(url);
                /*if(url.contains(test_url)) {
                    return getNewResponse_new(url);
                }else{
                    return super.shouldInterceptRequest(view, url);
                }*/
            }

            // Handle API 21+
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {

                String url = request.getUrl().toString();
                WebResourceResponse new_response = getNewResponse_new(url);
                return new_response;
                /*if(url.contains(test_url)) {
                    return getNewResponse_new(url);
                }else{
                    return super.shouldInterceptRequest(view, url);
                }*/
            }

            private WebResourceResponse getNewResponse_new(String url) {
                String payload = "";
                try {
                    //==
                    // Configure the GET request
                    //OkHttpClient httpClient = getHttpClient(url);
                    OkHttpClient httpClient = new OkHttpClient();

                    // Configure headers
                    Headers.Builder hb = new Headers.Builder();
                    hb.add("Content-Type", "application/JSON");
                    hb.add("Accept", "application/JSON");

                    hb.add("Cookie",cookieString);

                    System.out.println("------- WebViewActivity - calling : url => " + url);
                    Request httpGet = new Request.Builder()
                            .url(url)
                            .headers(hb.build())
                            .build();

                    Response httpResponse = httpClient.newCall(httpGet).execute();

                    if (!httpResponse.isSuccessful())
                        throw new IOException("Unexpected code: " + httpResponse);

                    payload = httpResponse.body().string();

                    WebResourceResponse new_response =                    
                       new WebResourceResponse(
                            httpResponse.header("content-type","text/plain"),
                            httpResponse.header("content-encoding", "utf-8"),
                            httpResponse.body().byteStream()
                    );

                    return  new_response;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }
            }

        });
        mWebView.loadUrl(balance_url);        
    }

    ...

}

在调试时,“有效载荷”表明从加载的URL返回了正确的响应,这表示已成功添加cookie。但是在执行完此覆盖方法之后:

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {

                String url = request.getUrl().toString();
                WebResourceResponse new_response = getNewResponse_new(url);
                return new_response;
                /*if(url.contains(test_url)) {
                    return getNewResponse_new(url);
                }else{
                    return super.shouldInterceptRequest(view, url);
                }*/
            }

它在控制台中抛出此错误:

E / InputStreamUtil:在对从ShouldInterceptRequest返回的InputStream上调用available()时得到异常。这将导致相关请求失败。                     java.io.IOException:已关闭                        在okio.RealBufferedSource $ 1.available(RealBufferedSource.java:438)                        在org.chromium.android_webview.InputStreamUtil.available(InputStreamUtil.java:44)

在shouldInterceptRequest()中使用WebResourceResponse时出现错误吗?我完全正确使用了吗?

谢谢

肖恩

0 个答案:

没有答案