UWP WebView - 显示网络服务器内容时网络服务器登录问题
UWP WebView - Issues with webserver login when displaying webserver content
我需要为我正在开发的 IoT Core 应用显示来自远程 Apache 网络服务器的一些网页。我对未受保护的页面使用了 WebView class 方法 .Navigate() 并且它工作得非常整洁。然而,对于我的项目,我需要先登录到网络服务器(用户名+密码),然后从页面检索信息,但我不知道如何使用 WebView class。我一无所知。
我找到了一个使用 WebBrowser class Navigate() 的解决方案,将凭据作为 64 位编码字符串发送,但是 WebView 的 Navigate() 只允许 1 个参数,URI,没有其他.我似乎也找不到 WebBrowser class。
我已经尝试将 username/password 嵌入到 URI 中,但它无法正常工作,我知道这样做不是一个好主意。
是否可以使用 WebView 实现此目的?任何 suggestions/ideas?
感谢任何帮助
编辑:我找到了适合我的问题的解决方案,我将其发布以防对遇到类似问题的人有所帮助。
Uri req_uri = new Uri(uri_list[i]);
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.ServerCredential = new PasswordCredential(req_uri.ToString(), "username", "password");
HttpCookieCollection cookiejar = filter.CookieManager.GetCookies(req_uri);
if (cookiejar.Count > 0)
{
foreach (HttpCookie cookie in cookiejar)
{
filter.CookieManager.SetCookie(cookie);
}
}
Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(filter);
Windows.Web.Http.HttpRequestMessage http_req = new Windows.Web.Http.HttpRequestMessage();
http_req.Method = Windows.Web.Http.HttpMethod.Get;
http_req.RequestUri = req_uri;
var clientResponse = client.GetAsync(req_uri);
webView.NavigateWithHttpRequestMessage(http_req);
http_req.Dispose();
client.Dispose();
您也许可以使用 NavigateWithHttpRequestMessage
,这样您就可以使用 HttpRequestMessage
.
导航到页面
您仍然需要使用 HttpClient
验证 out-of-band,获取 cookie 和验证 headers,然后在构建 HttpRequestMessage
时使用它们。
Here 的 Whosebug 应该对此有所帮助
如果您的远程 Web 服务器支持 HTTP 基本身份验证,您可以像这样在 URL 中传递凭据:
https://Username:Password@www.example.com/index.html
我需要为我正在开发的 IoT Core 应用显示来自远程 Apache 网络服务器的一些网页。我对未受保护的页面使用了 WebView class 方法 .Navigate() 并且它工作得非常整洁。然而,对于我的项目,我需要先登录到网络服务器(用户名+密码),然后从页面检索信息,但我不知道如何使用 WebView class。我一无所知。
我找到了一个使用 WebBrowser class Navigate() 的解决方案,将凭据作为 64 位编码字符串发送,但是 WebView 的 Navigate() 只允许 1 个参数,URI,没有其他.我似乎也找不到 WebBrowser class。
我已经尝试将 username/password 嵌入到 URI 中,但它无法正常工作,我知道这样做不是一个好主意。
是否可以使用 WebView 实现此目的?任何 suggestions/ideas?
感谢任何帮助
编辑:我找到了适合我的问题的解决方案,我将其发布以防对遇到类似问题的人有所帮助。
Uri req_uri = new Uri(uri_list[i]);
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.ServerCredential = new PasswordCredential(req_uri.ToString(), "username", "password");
HttpCookieCollection cookiejar = filter.CookieManager.GetCookies(req_uri);
if (cookiejar.Count > 0)
{
foreach (HttpCookie cookie in cookiejar)
{
filter.CookieManager.SetCookie(cookie);
}
}
Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(filter);
Windows.Web.Http.HttpRequestMessage http_req = new Windows.Web.Http.HttpRequestMessage();
http_req.Method = Windows.Web.Http.HttpMethod.Get;
http_req.RequestUri = req_uri;
var clientResponse = client.GetAsync(req_uri);
webView.NavigateWithHttpRequestMessage(http_req);
http_req.Dispose();
client.Dispose();
您也许可以使用 NavigateWithHttpRequestMessage
,这样您就可以使用 HttpRequestMessage
.
您仍然需要使用 HttpClient
验证 out-of-band,获取 cookie 和验证 headers,然后在构建 HttpRequestMessage
时使用它们。
Here 的 Whosebug 应该对此有所帮助
如果您的远程 Web 服务器支持 HTTP 基本身份验证,您可以像这样在 URL 中传递凭据:
https://Username:Password@www.example.com/index.html