gpt4 book ai didi

android - 如何防止Origin :null in android webview

转载 作者:行者123 更新时间:2023-12-05 07:41:06 25 4
gpt4 key购买 nike

我正在尝试在 android 代码中执行 webview.postUrl

它会自动在 header 中添加 Origin:null

如何防止这种情况?

下面是我的代码

import butterknife.Bind;
.....
@Bind(R.id.webview)
WebView webview;
....
....
protected final void loadUrl(String url) {
webview.postUrl("http://localhost:8080/myapp/login", mypostdata);
}

最佳答案

Origin 设置为 null 可能是因为在您请求的 API 上启用了 CORS(跨源资源共享)。

Check this page for more information

据我所知,postUrl方法不允许更新 Origin。

我使用以下解决方法 成功发送了自定义来源 URL:

val originURL = "https://www.origin.url/"
val htmlContent = """
<html>
<body onload='form1.submit()'>
<form id='form1' action='http://localhost:8080/myapp/login' method='POST'>
<input name='POST_PARAM_1' type='hidden' value='VALUE_OF_PARAM1' />
<input name='POST_PARAM_2' type='hidden' value='VALUE_OF_PARAM2' />
<input name='POST_PARAM_3' type='hidden' value='VALUE_OF_PARAM3' />
<input name='POST_PARAM_4' type='hidden' value='VALUE_OF_PARAM4' />
</form>
</body>
</html>
""".trimIndent()

webView.loadDataWithBaseURL(originURL , htmlContent , "text/html" , "UTF-8" , null)

我没有使用不支持发送自定义 header 的 postUrl,而是调用了 loadDataWithBaseURL ,此方法需要一个 baseURL(即原始 URL)。

基于文档:

Loads the given data into this WebView, using baseUrl as the base URLfor the content. The base URL is used both to resolve relative URLsand when applying JavaScript's same-origin policy. The historyUrl isused for the history entry.

WebView 将加载一个 HTML 页面,该页面会自动将参数 POST 到您的 URL。

关于android - 如何防止Origin :null in android webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45593403/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com