如何使用 webview 发出帖子请求? [英] How to make post requests with webview?

查看:19
本文介绍了如何使用 webview 发出帖子请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 webview 发出 http post 请求.

I want to make an http post request using webview.

webView.setWebViewClient(new WebViewClient(){


            public void onPageStarted(WebView view, String url,
                Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            }

            public boolean shouldOverrideUrlLoading(WebView view,
                String url) {

            webView.postUrl(Base_Url, postData.getBytes());

            return true;
            }

        });

上面的代码片段加载了网页.我想访问此请求的响应.

The above code snippet loads the webpage. I want to access the response of this request.

如何使用 webview 获取 http post 请求的响应?

How can i obtain the response of an http post request using webview?

提前致谢

推荐答案

WebView 不允许您访问 HTTP 响应的内容.

The WebView does not let you access the content of the HTTP response.

为此,您必须使用 HttpClient,然后使用 loadDataWithBaseUrl 并指定基本 url,以便用户可以使用 webview继续在网站中导航.

You have to use HttpClient for that, and then forward the content to the view by using the function loadDataWithBaseUrl and specifying the base url so that the user can use the webview to continue navigating in the website.

示例:

// Executing POST request
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.setEntity(postContent);
HttpResponse response = httpclient.execute(httppost);

// Get the response content
String line = "";
StringBuilder contentBuilder = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
while ((line = rd.readLine()) != null) { 
    contentBuilder.append(line); 
}
String content = contentBuilder.toString();

// Do whatever you want with the content

// Show the web page
webView.loadDataWithBaseURL(url, content, "text/html", "UTF-8", null);

这篇关于如何使用 webview 发出帖子请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆