浏览代码

Add headers when loading url

Joe Park 7 年之前
父节点
当前提交
26e4329f2d

+ 3 - 0
android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

@@ -81,6 +81,7 @@ public class FlutterWebviewPlugin implements MethodCallHandler {
         boolean clearCookies = call.argument("clearCookies");
         boolean withZoom = call.argument("withZoom");
         boolean withLocalStorage = call.argument("withLocalStorage");
+        Map<String, String> headers = call.argument("headers");
 
         if (webViewManager == null || webViewManager.closed == true) {
             webViewManager = new WebviewManager(activity);
@@ -96,6 +97,7 @@ public class FlutterWebviewPlugin implements MethodCallHandler {
                 clearCookies,
                 userAgent,
                 url,
+                headers,
                 withZoom,
                 withLocalStorage
         );
@@ -163,6 +165,7 @@ public class FlutterWebviewPlugin implements MethodCallHandler {
                     false,
                     "",
                     url,
+                    null,
                     false,
                     false
             );

+ 8 - 2
android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

@@ -13,6 +13,8 @@ import android.webkit.WebView;
 import android.webkit.WebViewClient;
 import android.widget.FrameLayout;
 
+import java.util.Map;
+
 import io.flutter.plugin.common.MethodCall;
 import io.flutter.plugin.common.MethodChannel;
 
@@ -68,7 +70,7 @@ class WebviewManager {
         webView.clearFormData();
     }
 
-    void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, boolean withZoom, boolean withLocalStorage) {
+    void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> headers, boolean withZoom, boolean withLocalStorage) {
         webView.getSettings().setJavaScriptEnabled(withJavascript);
         webView.getSettings().setBuiltInZoomControls(withZoom);
         webView.getSettings().setSupportZoom(withZoom);
@@ -90,7 +92,11 @@ class WebviewManager {
             webView.getSettings().setUserAgentString(userAgent);
         }
 
-        webView.loadUrl(url);
+        if (headers != null) {
+            webView.loadUrl(url, headers);
+        } else {
+            webView.loadUrl(url);
+        }
     }
 
     void close(MethodCall call, MethodChannel.Result result) {

+ 7 - 1
ios/Classes/FlutterWebviewPlugin.m

@@ -120,7 +120,13 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
                     @throw @"not available on version earlier than ios 9.0";
                 }
             } else {
-                NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
+                NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
+                NSDictionary *headers = call.arguments[@"headers"];
+                
+                if (headers != nil) {
+                    [request setAllHTTPHeaderFields:headers];
+                }
+                
                 [self.webview loadRequest:request];
             }
         }

+ 10 - 3
lib/src/base.dart

@@ -61,6 +61,7 @@ class FlutterWebviewPlugin {
   Stream<WebViewStateChanged> get onStateChanged => _onStateChanged.stream;
 
   /// Start the Webview with [url]
+  /// - [headers] specify additional HTTP headers
   /// - [withJavascript] enable Javascript or not for the Webview
   ///     iOS WebView: Not implemented yet
   /// - [clearCache] clear the cache of the Webview
@@ -77,7 +78,8 @@ class FlutterWebviewPlugin {
   /// - [withLocalUrl]: allow url as a local path
   ///     Allow local files on iOs > 9.0
   Future<Null> launch(String url,
-      {bool withJavascript,
+      {Map<String, String> headers,
+      bool withJavascript,
       bool clearCache,
       bool clearCookies,
       bool hidden,
@@ -99,6 +101,11 @@ class FlutterWebviewPlugin {
       "withLocalStorage": withLocalStorage ?? true,
       "withLocalUrl": withLocalUrl ?? false
     };
+
+    if (headers != null) {
+      args["headers"] = headers;
+    }
+
     if (rect != null) {
       args["rect"] = {
         "left": rect.left,
@@ -131,10 +138,10 @@ class FlutterWebviewPlugin {
   /// Navigates forward on the Webview.
   /// This is only available on Android for now.
   Future goForward() => _channel.invokeMethod("forward");
-  
+
   // Hides the webview
   Future hide() => _channel.invokeMethod("hide");
-  
+
   // Shows the webview
   Future show() => _channel.invokeMethod("show");
 

+ 5 - 0
lib/src/webview_scaffold.dart

@@ -6,6 +6,7 @@ import 'package:flutter/material.dart';
 import 'base.dart';
 
 class WebviewScaffold extends StatefulWidget {
+
   final PreferredSizeWidget appBar;
   final String url;
   final bool withJavascript;
@@ -20,10 +21,13 @@ class WebviewScaffold extends StatefulWidget {
   final bool withLocalStorage;
   final bool withLocalUrl;
 
+  final Map<String, String> headers;
+
   WebviewScaffold(
       {Key key,
       this.appBar,
       @required this.url,
+      this.headers,
       this.withJavascript,
       this.clearCache,
       this.clearCookies,
@@ -62,6 +66,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
     if (_rect == null) {
       _rect = _buildRect(context);
       webviewReference.launch(widget.url,
+          headers: widget.headers,
           withJavascript: widget.withJavascript,
           clearCache: widget.clearCache,
           clearCookies: widget.clearCookies,