how can i add a custom header to post request on webview
I ran into needing to implement such a feature myself so I'm posting a code snippet for anyone running into the same issue in the future.
I'd definitely recommend using OkHttp but the principle (make a request and load the html into the browser in the success callback) should be the same with any other network client.
protected void postURL(final String url, String postData) {
Request request = new Request.Builder()
.url(url)
.addHeader("Cache-Control", "max-age=0")
.addHeader("Origin", "null") //Optional
.addHeader("Upgrade-Insecure-Requests", "1")
.addHeader("User-Agent", webView.getSettings().getUserAgentString())
.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
.addHeader("Accept-Language", Locale.getDefault().getLanguage())
.addHeader("Cookie", CookieManager.getInstance().getCookie(url))
.addHeader("X-Requested-With", BuildConfig.APPLICATION_ID)
.post(RequestBody.create(MediaType.parse("application/x-www-form-urlencoded"), postData))
.build();
new OkHttpClient().newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Timber.e(e.getMessage());
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
final String htmlString = response.body().string();
webView.post(new Runnable() {
@Override
public void run() {
webView.clearCache(true);
webView.loadDataWithBaseURL(url, htmlString, "text/html", "utf-8", null);
}
});
}
});
}
Note that most of those headers are not required but can be used as a guideline to reconstruct an original request issued by the webview itself
Send authorization header with every request in webview using okhttp in android
Finally this will rectify the html rendering issue (Pardon me, I didn't notice this earlier).
change content-type
in
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());
to text/html
, so the new code is
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());
If my solution needs any modifications, feel free to edit. Always accept better solutions. Happy coding...And Thanks everyone #SO ready to help.
Related Topics
Android Studio Gradle Dsl Method Not Found: 'Android()' -- Error(17,0)
Change Actionbar Color Programmatically More Than Once
Checkselfpermission Method Is Not Working in Targetsdkversion 22
Findfragmentbyid for Supportmapfragment Returns Null in Android Studio
Android Launching Music Player Using Intent
How to Run an Android App in Background
Differencebetween Activity and Context
Adb Is Not Recognized as Internal or External Command on Windows
App Not Run in Visual Studio Emulator for Android in Visual Studio 2015
How to Change Textcolor in Alertdialog
Android Get Screenshot of All Listview Items
The Import Com.Google.Android.Gms Cannot Be Resolved
Change Color of the Overflow Button on Actionbar
Android: Radiogroup - How to Configure the Event Listener
Volley JSONobjectrequest Post Parameters No Longer Work
Firebase @Propertyname Doesn't Work
How to Include a Username When Storing Email and Password Using Firebase (Baas) in an Android App