sách gpt4 ăn đã đi

webview - 具有基本身份验证的 Flutter WebView

In lại 作者:IT王子 更新时间:2023-10-29 06:57:07 36 4
mua khóa gpt4 giày nike

我正在加载一个网页,我想使用基本身份验证登录,我有使用 Swift 的经验并且能够像下面那样执行基本身份验证,但我无法为我的应用程序的 Flutter 版本实现基本身份验证。

---- swift 代码 ---

func configureView() {
let username = "user"
let password = "pass"
let userPasswordString = "\(user):\(pass)"
let userPasswordData = userPasswordString.data(using: String.Encoding.utf8)
let base64EncodedCredential = userPasswordData!.base64EncodedString(options:[])
let authString = "Basic \(base64EncodedCredential)"
let url = URL(string: "http://myurl")
var request = URLRequest(url: url!)
request.setValue(authString, forHTTPHeaderField: "Authorization")
webView.scalesPageToFit = true
webView.loadRequest(request)
}

--- Flutter WebView ---> 使用 URL 加载 webview

nhập 'gói:flutter/material.dart';
import 'dart:convert';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

class AddShipment extends StatefulWidget {

final String url;

AddShipment(this.url);

@ghi đè
State createState() {
return new _AddShipment();
}
}

class _AddShipment extends State {
final flutterWebviewPlugin = new FlutterWebviewPlugin();

@ghi đè
initState không có giá trị() {
super.initState();

flutterWebviewPlugin.close();
}

@ghi đè
void dispose() {

flutterWebviewPlugin.dispose();

super.dispose();
}


@ghi đè
Xây dựng tiện ích (BuildContext context) {
return new WebviewScaffold(
appBar: AppBar mới(
title: new Text("WebView"),
centerTitle: true,
backgroundColor: Colors.blue[900],
độ cao: 0.0,
),
url: widget.url,
withJavascript: true,
);
}
}

创建 urlRequest 的正确方法是什么?我试过:

static Future getURL(
final String username, final String password) {
final String url = 'http://myurl';
final String auth =
'Basic ' + base64Encode(utf8.encode('$username:$password'));
return http.get(url, headers: {'Authorization': auth});
}

câu trả lời hay nhất

你也可以试试我的插件flutter_inappbrowser (biên tập:已重命名为 flutter_inappwebview)。

hiện hữu initialHeaders 属性中使用 Authorization: Basic ... header 的示例如下所示:

import 'dart:async';
import 'dart:convert';

nhập 'gói:flutter/material.dart';

import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
runApp(MyApp mới());
}

class MyApp extends StatefulWidget {
@ghi đè
_MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State {

@ghi đè
initState không có giá trị() {
super.initState();
}

@ghi đè
void dispose() {
super.dispose();
}

@ghi đè
Xây dựng tiện ích (BuildContext context) {
trả về MaterialApp(
home: InAppWebViewPage()
);
}
}

class InAppWebViewPage extends StatefulWidget {
@ghi đè
_InAppWebViewPageState createState() => new _InAppWebViewPageState();
}

class _InAppWebViewPageState extends State {
InAppWebViewController webView;
String username = "USERNAME";
String password = "PASSWORD";

@ghi đè
Xây dựng tiện ích (BuildContext context) {
trả về Giàn giáo(
appBar: Thanh ứng dụng(
title: Text("InAppWebView")
),
body: Container(
child: Column(children: [
Expanded(
con: Container(
child: InAppWebView(
initialUrl: "http://myurl",
initialHeaders: {
'Authorization': 'Basic ' + base64Encode(utf8.encode('$username:$password'))
},
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
debuggingEnabled: true,
)
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {

},
onLoadStop: (InAppWebViewController controller, String url) {

},
),
),
),
]))
);
}
}

相反,这是一个使用 onReceivedHttpAuthRequest 事件的示例:

import 'dart:async';
import 'dart:convert';

nhập 'gói:flutter/material.dart';

import 'package:flutter_inappbrowser/flutter_inappbrowser.dart';

Future main() async {
runApp(MyApp mới());
}

class MyApp extends StatefulWidget {
@ghi đè
_MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State {

@ghi đè
initState không có giá trị() {
super.initState();
}

@ghi đè
void dispose() {
super.dispose();
}

@ghi đè
Xây dựng tiện ích (BuildContext context) {
trả về MaterialApp(
home: InAppWebViewPage()
);
}
}

class InAppWebViewPage extends StatefulWidget {
@ghi đè
_InAppWebViewPageState createState() => new _InAppWebViewPageState();
}

class _InAppWebViewPageState extends State {
InAppWebViewController webView;

@ghi đè
Xây dựng tiện ích (BuildContext context) {
trả về Giàn giáo(
appBar: Thanh ứng dụng(
title: Text("InAppWebView")
),
body: Container(
child: Column(children: [
Expanded(
con: Container(
child: InAppWebView(
initialUrl: "http://myurl",
initialOptions: InAppWebViewWidgetOptions(
inAppWebViewOptions: InAppWebViewOptions(
debuggingEnabled: true,
)
),
onWebViewCreated: (InAppWebViewController controller) {
webView = controller;
},
onLoadStart: (InAppWebViewController controller, String url) {

},
onLoadStop: (InAppWebViewController controller, String url) {

},
onReceivedHttpAuthRequest: (InAppWebViewController controller, HttpAuthChallenge challenge) async {
return HttpAuthResponse(username: "USERNAME", password: "PASSWORD", action: HttpAuthResponseAction.PROCEED);
},
),
),
),
]))
);
}
}

关于webview - 具有基本身份验证的 Flutter WebView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56062671/

36 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress