机器人的WebView POST请求与自定义页眉 [英] Android WebView Post Request with Custom Headers
本文介绍了机器人的WebView POST请求与自定义页眉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以看到有在Android的文档两个不同的方法来发布的数据,并添加标题。
I could see there are two separate methods in Android docs to post the data and add the headers.
For setting Headers
public void loadUrl (String url, Map<String, String> additionalHttpHeaders)
For setting Post Data
public void postUrl (String url, byte[] postData)
但我真正需要的是发布的数据以及头。 (意味着我想这确实既任务一个单一的方法?)
But what I really required is to post the data along with headers. ( Means I want a single method which does both the task ? )
有人可以请帮我出这一点。
Can somebody please help me out with that.
感谢:)
推荐答案
我撞到同样的问题,最近和之后几个小时就解决了。
I've bumped on same problem recently and after couple of hours solved it.
下面是我的一些意见code片断:
Here is my code snippet with some comments:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(getPostUrl());
httpPost.addHeader("Referer", getReferer()); // example of adding extra header referer
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
for (PostItem postItem : getPostItems()) { // key value post pairs
postParameters.add(new BasicNameValuePair(postItem.getKey(), postItem.getValue())); // add post parameters in array list
}
HttpResponse response = null;
try {
mWebView.getSettings().setJavaScriptEnabled(true);
httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
response = httpclient.execute(httpPost);
BasicResponseHandler responseHandler = new BasicResponseHandler();
String htmlString = responseHandler.handleResponse(response);
mWebView.loadDataWithBaseURL(getPostUrl(), htmlString, "text/html", "utf-8", null); // important!! is to fill base url
}catch (Exception e){
// handle errors
}
这篇关于机器人的WebView POST请求与自定义页眉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文