背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互

2023-09-15 21:05
-->

[源码下载]

背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互

作者:webabcd

介绍
背水一战 Windows 10 之 控件(WebView)

  • 加载指定 HttpMethod 的请求
  • 自定义请求的 http header
  • app 与 js 的交互

示例
1、演示 WebView 如何加载指定 HttpMethod 的请求以及如何自定义请求的 http header
WebApi/Controllers/WebViewPostController.cs

/*
* 用于 WebView 演示“如何加载指定 HttpMethod 的请求,以及如何自定义请求的 http header”
*/ using www.plazaua.com;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http; namespace WebApi.Controllers
{
public class WebViewPostController : ApiController
{
[HttpPost]
public async Task Post()
{
Stream stream = await this.Request.Content.ReadAsStreamAsync();
StreamReader sr = new StreamReader(stream);
string postData = sr.ReadToEnd();
sr.Dispose(); string myHeader = this.Request.Headers.GetValues("myHeader").FirstOrDefault(); HttpResponseMessage result = new HttpResponseMessage
{
Content = new StringContent($"post data: {postData}
myHeader: {myHeader}", Encoding.UTF8, "text/html")
}; return result;
}
}
}

Controls/WebViewDemo/WebViewDemo3.xaml

x:Class="Windows10.Controls.WebViewDemo.WebViewDemo3"
xmlns="http://www.plazaua.com/winfx/2006/xaml/presentation"
xmlns:x="http://www.plazaua.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.WebViewDemo"
xmlns:d="http://www.plazaua.com/expression/blend/2008"
xmlns:mc="http://www.plazaua.com/markup-compatibility/2006"
mc:Ignorable="d">


Controls/WebViewDemo/WebViewDemo3.xaml.cs

/*
* WebView - 内嵌浏览器控件(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo/)
*
*
* 本例用于演示
* 1、WebView 如何加载指定 HttpMethod 的请求
* 2、WebView 如何自定义请求的 http header
*/ using System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.Web.Http; namespace Windows10.Controls.WebViewDemo
{
public sealed partial class WebViewDemo3 : Page
{
public WebViewDemo3()
{
this.InitializeComponent(); this.Loaded += WebViewDemo3_Loaded;
} private void WebViewDemo3_Loaded(object sender, RoutedEventArgs e)
{
// 实例化 HttpRequestMessage(可以指定请求的 HttpMethod 以及自定义请求的 http header)
HttpRequestMessage httpRequestMessage = new HttpRequestMessage(www.plazaua.com, new Uri("http://localhost:44914/api/webviewpost")); // 构造 post 数据
httpRequestMessage.Content = new HttpStringContent("hello webabcd"); // 自定义 http header
httpRequestMessage.Headers.Append("myHeader", "hello header"); // 通过 NavigateWithHttpRequestMessage 加载指定的 HttpRequestMessage 对象
webView.NavigateWithHttpRequestMessage(httpRequestMessage);
}
}
}

2、演示 app 与 js 的交互
MyRuntimeComponent/JS2WRC.cs

/*
* 用于演示 javascript 调用 windows runtime component 中定义的方法
*/ using Windows.Foundation.Metadata; namespace MyRuntimeComponent
{
[AllowForWeb] // 允许 js 调用
public sealed class JS2WRC // 注意:在 winrc 中用 c# 写的类必须是 sealed 的
{
public string hello(string name)
{
return $"hello: {name}";
}
}
}

Controls/WebViewDemo/WebViewJavaScript.html




i am title











Controls/WebViewDemo/WebViewDemo4.xaml

x:Class="Windows10.Controls.WebViewDemo.WebViewDemo4"
xmlns="http://www.plazaua.com/winfx/2006/xaml/presentation"
xmlns:x="http://www.plazaua.com/winfx/2006/xaml"
xmlns:local="using:Windows10.Controls.WebViewDemo"
xmlns:d="http://www.plazaua.com/expression/blend/2008"
xmlns:mc="http://www.plazaua.com/markup-compatibility/2006"
mc:Ignorable="d">