You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何仅使用WebView发送携带登录凭证的HTTP POST请求并加载响应页面

How to Send HTTP POST Request with Login Credentials in Android WebView

Got it, let's tweak your code to send a POST request with login credentials to your API, then load the response page and keep using the web app smoothly. Here's the step-by-step solution:

Step 1: Prepare URL-Encoded POST Parameters

First, construct your login credentials as URL-encoded key-value pairs. This prevents issues with special characters (like spaces, @, or &) breaking the request.

Step 2: Use postUrl() Instead of loadUrl()

Android's WebView has a built-in postUrl() method specifically for sending POST requests. Replace your existing loadUrl() call with this method, passing your encoded parameters as a UTF-8 byte array.

Modified Full Code

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

// ... your existing activity setup code ...

wv.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
        if (!Common.connectionAvailable(WebActivity.this)) {
            Toast.makeText(getApplicationContext(),"Check Internet",Toast.LENGTH_LONG).show();
            finish();
        }
        pd = new ProgressDialog(WebActivity.this, R.style.AppTheme_Dark_Dialog);
        pd.setMessage("Loading APP");
        pd.show();
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        if (pd != null) {
            pd.dismiss();
        }
    }

    @Override
    public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
        super.onReceivedError(view, request, error);
        finish();
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return super.shouldOverrideUrlLoading(view, url);
    }
});

// Prepare and send the POST request
try {
    // Replace with your actual credential keys and user input values
    String postData = "username=" + URLEncoder.encode("user123", "UTF-8") +
                      "&password=" + URLEncoder.encode("pass456", "UTF-8");
    
    // Send POST to your login API
    wv.postUrl("http://myapp/api/login", postData.getBytes(StandardCharsets.UTF_8));
} catch (Exception e) {
    e.printStackTrace();
    // Handle encoding errors or other exceptions here (e.g., show error toast)
}

Important Notes:

  • Replace "user123" and "pass456" with actual user input (like values from EditText fields) or your app's specific credential values.
  • The URLEncoder ensures your parameters are formatted correctly for HTTP requests.
  • After sending the POST, the WebView will automatically load the API's response page. It will also maintain the login session via cookies, so all subsequent navigation within the web app will stay authenticated.
  • Don't forget to confirm you have the internet permission in your AndroidManifest.xml: <uses-permission android:name="android.permission.INTERNET" />

内容的提问来源于stack exchange,提问作者Rishabh Gusain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:20:55