Java 在android中使用okhttp在webview中发送每个请求的授权标头
我正在使用WebView显示网页,但服务器希望来自我的WebView的每个请求都有一个授权令牌。有人知道这是否可能吗? 我在#SO中引用了这篇文章。但是我不能得到结果 这是我的代码(与我的编码标准完全一致,我是初学者) 在stacktrace中找到了这个,在stacktrace中没有更多Java 在android中使用okhttp在webview中发送每个请求的授权标头,java,android,webview,retrofit2,okhttp3,Java,Android,Webview,Retrofit2,Okhttp3,我正在使用WebView显示网页,但服务器希望来自我的WebView的每个请求都有一个授权令牌。有人知道这是否可能吗? 我在#SO中引用了这篇文章。但是我不能得到结果 这是我的代码(与我的编码标准完全一致,我是初学者) 在stacktrace中找到了这个,在stacktrace中没有更多 E/DataReductionProxySettingListener: No DRP key due to exception:java.lang.ClassNotFoundException: com.a
E/DataReductionProxySettingListener: No DRP key due to exception:java.lang.ClassNotFoundException: com.android.webview.chromium.Drp
最后,这将纠正html呈现问题(对不起,我之前没有注意到这一点) 更改中的内容类型
return new WebResourceResponse(response.header("content-type", response.body().contentType().type()), // You can set something other as default content-type
response.header("content-encoding", "utf-8"), // Again, you can set another encoding as default
response.body().byteStream());
到text/html
,因此新代码是
return new WebResourceResponse(response.header("text/html", response.body().contentType().type()), // You can set something other as default content-type
response.header("content-encoding", "utf-8"), // Again, you can set another encoding as default
response.body().byteStream());
如果我的解决方案需要任何修改,请随意编辑。总是接受更好的解决方案。快乐的编码…感谢大家#随时准备提供帮助。Sudheesh的回答很好,以下是更新版本,因为
应该重写分配(WebView,String url)
在API 21:
webView.setWebViewClient(new WebViewClient() {
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest webResourceRequest) {
// Filter out any requests we not interested in
if (webResourceRequest.getUrl().getHost() == null ||
!webResourceRequest.getUrl().getHost().equals(MyAPI.getServer().toHost())) {
return super.shouldInterceptRequest(view, webResourceRequest);
}
try {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(webResourceRequest.getUrl().toString())
.addHeader(HttpConnector.DefaultConnector().getAuthorizationHeaderKey(), HttpConnector.DefaultConnector().getAuthorizationHeaderValue())
.build();
Response response = okHttpClient.newCall(request).execute();
return new WebResourceResponse(response.header("text/html", response.body().contentType().type()), // You can set something other as default content-type
response.header("content-encoding", "utf-8"), // Again, you can set another encoding as default
response.body().byteStream());
} catch (ClientProtocolException e) {
//return null to tell WebView we failed to fetch it WebView should try again.
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
});
这可能不能回答你的问题,但我认为这对其他有类似问题的人会有帮助 如果服务器资源需要这样的基本身份验证头
Authorization:basic dXNlcm5hbWU6cGFzc3dvcmQ=
,那么您应该像这样重写WebViewClient中的onReceiveDhtPauthRequest
方法
webView.webViewClient = object : WebViewClient() {
override fun onReceivedHttpAuthRequest(view: WebView?, handler: HttpAuthHandler?, host: String?, realm: String?) {
handler?.proceed("username", "password")
}
}
还请注意,如果可能,您应该始终避免拦截WebView请求。阅读更多信息。请发布您收到的任何日志、错误。可能需要再次检查服务器是否希望使用带有身份验证值的前缀(“承载者”)。@asadmshah到目前为止没有错误,我只是重新编辑了代码,现在在webview中得到了纯HTML,现在服务器得到了我的身份验证令牌头。请参阅中的编辑question@asadmshah只有登录的用户才能访问该网页,我的php后端对用户使用承载身份验证。所以我必须通过“Bearer”+tokenValue才能访问该网页。嗯
text/html
是一种内容类型,而不是它自己的标题。当然,他应该将内容类型标题设置为text/html
?shouldInterceptRequest
,url作为参数,现在已被弃用。这会将所有请求转换为get
@mrid,并查看链接(重复)问题的许多答案。不适用于我
webView.setWebViewClient(new WebViewClient() {
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest webResourceRequest) {
// Filter out any requests we not interested in
if (webResourceRequest.getUrl().getHost() == null ||
!webResourceRequest.getUrl().getHost().equals(MyAPI.getServer().toHost())) {
return super.shouldInterceptRequest(view, webResourceRequest);
}
try {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(webResourceRequest.getUrl().toString())
.addHeader(HttpConnector.DefaultConnector().getAuthorizationHeaderKey(), HttpConnector.DefaultConnector().getAuthorizationHeaderValue())
.build();
Response response = okHttpClient.newCall(request).execute();
return new WebResourceResponse(response.header("text/html", response.body().contentType().type()), // You can set something other as default content-type
response.header("content-encoding", "utf-8"), // Again, you can set another encoding as default
response.body().byteStream());
} catch (ClientProtocolException e) {
//return null to tell WebView we failed to fetch it WebView should try again.
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
});
webView.webViewClient = object : WebViewClient() {
override fun onReceivedHttpAuthRequest(view: WebView?, handler: HttpAuthHandler?, host: String?, realm: String?) {
handler?.proceed("username", "password")
}
}