简体   繁体   English

如何在C#中拦截http请求?

[英]How to intercept http request in C#?

I'm trying to develop an C# UWP application. 我正在尝试开发一个C#UWP应用程序。 Its view contains webview control and html contents are all contained in this solution. 它的视图包含webview控件和html内容都包含在此解决方案中。 I want to intercept http request and if it is GET request, return something like json, text, or others. 我想拦截http请求,如果是GET请求,则返回json,text或其他内容。 I could do this when I developed an android application. 当我开发一个Android应用程序时,我可以这样做。 Android supplies some methods "shouldInterceptRequest" and types like "WebRequestResponse". Android提供了一些方法“shouldInterceptRequest”和类似“WebRequestResponse”的类型。 Can I do this in C#(not Xamarin)? 我可以用C#(不是Xamarin)做到这一点吗? And if it is yes, please tell me how to do. 如果是,请告诉我该怎么做。 Sorry for my bad English. 对不起,我的英语不好。 Thanks. 谢谢。

not about WebBrowser, but about WebView. 不是关于WebBrowser,而是关于WebView。

Edit 编辑

You can listen to NavigationStarting event 您可以收听NavigationStarting事件

public MainPage()
{
    this.InitializeComponent();
    MyWebView.NavigationStarting += MyWebViewOnNavigationStarting;
}

private void MyWebViewOnNavigationStarting(WebView sender, WebViewNavigationStartingEventArgs args)
{
    string navigatingUri = args.Uri.ToString();

}

Note that this only capture navigation event inside the webview. 请注意 ,这仅捕获webview中的导航事件。

Original "wrong" answer 原来“错”的答案

  1. Place your HTML file in Asset Folder 将HTML文件放在Asset文件夹中
  2. In your web view, put the Source with ms-appx-web:///Assets/{htmlFileName} 在您的Web视图中,将Sourcems-appx-web:///Assets/{htmlFileName}
<WebView x:Name="MyWebView" Source="ms-appx-web:///Assets/Foo.html"></WebView>

Or via code 或者通过代码

public MainPage()
{
    this.InitializeComponent();
    MyWebView.Navigate(new Uri("ms-appx-web:///Assets/Foo.html"));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM