gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:57:07 36 4
gpt4 key购买 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

import 'package: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);

@override
State<StatefulWidget> createState() {
return new _AddShipment();
}
}

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

@override
void initState() {
super.initState();

flutterWebviewPlugin.close();
}

@override
void dispose() {

flutterWebviewPlugin.dispose();

super.dispose();
}


@override
Widget build(BuildContext context) {
return new WebviewScaffold(
appBar: new AppBar(
title: new Text("WebView"),
centerTitle: true,
backgroundColor: Colors.blue[900],
elevation: 0.0,
),
url: widget.url,
withJavascript: true,
);
}
}

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

static Future<Response> 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});
}

最佳答案

你也可以试试我的插件flutter_inappbrowser (编辑:已重命名为 flutter_inappwebview)。

initialHeaders 属性中使用 Authorization: Basic ... header 的示例如下所示:

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

import 'package:flutter/material.dart';

import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Future main() async {
runApp(new MyApp());
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {

@override
void initState() {
super.initState();
}

@override
void dispose() {
super.dispose();
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: InAppWebViewPage()
);
}
}

class InAppWebViewPage extends StatefulWidget {
@override
_InAppWebViewPageState createState() => new _InAppWebViewPageState();
}

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

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("InAppWebView")
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: 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';

import 'package:flutter/material.dart';

import 'package:flutter_inappbrowser/flutter_inappbrowser.dart';

Future main() async {
runApp(new MyApp());
}

class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {

@override
void initState() {
super.initState();
}

@override
void dispose() {
super.dispose();
}

@override
Widget build(BuildContext context) {
return MaterialApp(
home: InAppWebViewPage()
);
}
}

class InAppWebViewPage extends StatefulWidget {
@override
_InAppWebViewPageState createState() => new _InAppWebViewPageState();
}

class _InAppWebViewPageState extends State<InAppWebViewPage> {
InAppWebViewController webView;

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("InAppWebView")
),
body: Container(
child: Column(children: <Widget>[
Expanded(
child: 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
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com