我如何添加一个自定义标题在webview上发布请求 [英] how can i add a custom header to post request on webview
问题描述
我现在在webview上有关于发布请求的问题。
这里是情况:当我的webview加载了一个登录页面,并且里面有一个表单可以发送帖子请求。当我点击提交按钮时,如何添加一个自定义标题。
我遇到了需要自己实现这样一个功能,所以我发布了一个代码片段,供将来遇到同样问题的任何人使用。
我肯定会推荐使用OkHttp,但其原理(在成功回调中发送请求并将html加载到浏览器中)应该与任何其他网络客户端一样。
protected void postURL(final String url,String postData){
Request request = new Request.Builder()
.url(url)
.addHeader(Cache-Control,max-age = 0)
.addHeader(Origin,null)//可选
.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(Reques tBody.create(MediaType.parse(application / x-www-form-urlencoded),postData))
.build();
OkHttpClient()。newCall(request).enqueue(new Callback(){
@Override $ b $ 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);
}
});
}
});
}
请注意,大多数这些标题不是必需的,但可以用作指南重新构建webview本身发布的原始请求
I am now having a problem about post request on webview. Here is the situation: when my webview loaded a login page,and there's a form inside which would make the post request.How can i add a custom header to it when i click submit button.
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
这篇关于我如何添加一个自定义标题在webview上发布请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!