asp.net – Visual Basic以编程方式将用户名和密码传递给https u
发布时间:2020-12-16 09:43:34 所属栏目:asp.Net 来源:网络整理
导读:使用普通的HTTP,我可以下载上传并导航到路由器,但是当路由器使用HTTPS时,我找不到任何代码来执行任何操作. 要下载我用这个: Try My.Computer.Network.DownloadFile("http://" "180.29.74.70" "/cgi-bin/log.cgi","C:UsersssbDesktoprandomword.txt","us
使用普通的HTTP,我可以下载上传并导航到路由器,但是当路由器使用HTTPS时,我找不到任何代码来执行任何操作.
要下载我用这个: Try My.Computer.Network.DownloadFile("http://" & "180.29.74.70" & "/cgi-bin/log.cgi","C:UsersssbDesktoprandomword.txt","username","password") WebBrowser1.Refresh() Catch ex As Exception MessageBox.Show("Router not sufficient for operation Return for Inspection cannot download log file") End Try 要上传文件我使用: My.Computer.Network.UploadFile("C:UsersssbDesktoptomtn.txt","http://" & "180.29.74.70" & "/cgi-bin/updateconfig.cgi","password") 要导航到HTTP上的网页,我使用: WebBrowser1.Navigate("https://username:password@180.29.74.70 ") 但是当我使用HTTPS时: WebBrowser1.Navigate("https://username:password@180.29.74.70 ") 我得到这个安全警报: 然后我点击是,它转到页面 – 但我需要代码绕过这些安全问题. 解决方法
尽管它们之间存在松散关系,但您在这里提出了两个不同的问题.
>当我使用WebBrowser控件通过HTTPS加载页面时,为什么调用失败? 首先,您需要消除代码失败的可能性.使用已知可正常工作的公共HTTPS URL尝试上述两项任务. 如果您发现问题的根源是您的私有URL,您可能需要考虑是否要忽略WebBrowser控件中的SSL错误. 您可以使用this blog post中的(未经测试,已翻译为VB)代码执行此操作: Partial Public Class Form1 Inherits Form Private WithEvents WebBrowser As New WebBrowser Private Sub WebBrowser_DocumentCompleted(Sender As Object,e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser.DocumentCompleted If e.Url.ToString() = "about:blank" Then 'create a certificate mismatch WebBrowser.Navigate("https://74.125.225.229/") End If End Sub End Class <Guid("6D5140C1-7436-11CE-8034-00AA006009FA")> <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> <ComImport> Public Interface UCOMIServiceProvider <PreserveSig> Function QueryService(<[In]> ByRef guidService As Guid,<[In]> ByRef riid As Guid,<Out> ByRef ppvObject As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer End Interface <ComImport> <ComVisible(True)> <Guid("79eac9d5-bafa-11ce-8c82-00aa004ba90b")> <InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> Public Interface IWindowForBindingUI <PreserveSig> Function GetWindow(<[In]> ByRef rguidReason As Guid,<[In],Out> ByRef phwnd As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer End Interface <ComImport> <ComVisible(True)> <Guid("79eac9d7-bafa-11ce-8c82-00aa004ba90b")> <InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> Public Interface IHttpSecurity 'derived from IWindowForBindingUI <PreserveSig> Function GetWindow(<[In]> ByRef rguidReason As Guid,Out> ByRef phwnd As IntPtr) As <MarshalAs(UnmanagedType.I4)> Integer <PreserveSig> Function OnSecurityProblem(<[In],MarshalAs(UnmanagedType.U4)> dwProblem As UInteger) As Integer End Interface Public Class MyWebBrowser Inherits WebBrowser Public Shared IID_IHttpSecurity As New Guid("79eac9d7-bafa-11ce-8c82-00aa004ba90b") Public Shared IID_IWindowForBindingUI As New Guid("79eac9d5-bafa-11ce-8c82-00aa004ba90b") Public Const S_OK As Integer = 0 Public Const S_FALSE As Integer = 1 Public Const E_NOINTERFACE As Integer = &H80004002 Public Const RPC_E_RETRY As Integer = &H80010109 Protected Overrides Function CreateWebBrowserSiteBase() As WebBrowserSiteBase Return New MyWebBrowserSite(Me) End Function Private Class MyWebBrowserSite Inherits WebBrowserSite Implements UCOMIServiceProvider Implements IHttpSecurity Implements IWindowForBindingUI Private myWebBrowser As MyWebBrowser Public Sub New(myWebBrowser As MyWebBrowser) MyBase.New(myWebBrowser) Me.myWebBrowser = myWebBrowser End Sub Public Function QueryService(ByRef guidService As Guid,ByRef riid As Guid,ByRef ppvObject As IntPtr) As Integer Implements UCOMIServiceProvider.QueryService If riid = IID_IHttpSecurity Then ppvObject = Marshal.GetComInterfaceForObject(Me,GetType(IHttpSecurity)) Return S_OK End If If riid = IID_IWindowForBindingUI Then ppvObject = Marshal.GetComInterfaceForObject(Me,GetType(IWindowForBindingUI)) Return S_OK End If ppvObject = IntPtr.Zero Return E_NOINTERFACE End Function Public Function GetWindow(ByRef rguidReason As Guid,ByRef phwnd As IntPtr) As Integer Implements IHttpSecurity.GetWindow,IWindowForBindingUI.GetWindow If rguidReason = IID_IHttpSecurity OrElse rguidReason = IID_IWindowForBindingUI Then phwnd = myWebBrowser.Handle Return S_OK Else phwnd = IntPtr.Zero Return S_FALSE End If End Function Public Function OnSecurityProblem(dwProblem As UInteger) As Integer Implements IHttpSecurity.OnSecurityProblem 'ignore errors 'undocumented return code,does not work on IE6 Return S_OK End Function End Class End Class 关于问题#2:看起来你可能会混淆WebBrowser和DownloadFile().您可能已经发现,WebBrowser控件不会下载文件.但是,您可以使用this technique模拟行为: Partial Public Class Form2 Inherits Form Private Sub WebBrowser_Navigating(Sender As Object,e As WebBrowserNavigatingEventArgs) Handles WebBrowser.Navigating Dim sFilePath As String Dim oClient As Net.WebClient ' This can be any conditional criteria you wish ' If (e.Url.Segments(e.Url.Segments.Length - 1).EndsWith(".pdf")) Then SaveFileDialog.FileName = e.Url.Segments(e.Url.Segments.Length - 1) e.Cancel = True If SaveFileDialog.ShowDialog() = DialogResult.OK Then sFilePath = SaveFileDialog.FileName oClient = New Net.WebClient AddHandler oClient.DownloadFileCompleted,New AsyncCompletedEventHandler(AddressOf DownloadFileCompleted) oClient.DownloadFileAsync(e.Url,sFilePath) End If End If End Sub Private Sub DownloadFileCompleted(Sender As Object,e As AsyncCompletedEventArgs) MessageBox.Show("File downloaded") End Sub Private WithEvents SaveFileDialog As New SaveFileDialog Private WithEvents WebBrowser As New WebBrowser End Class 无论如何,解决这个问题的第一步是弄清楚它是您的代码还是导致您的问题的私有URL. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ASP.NET AJAX – AJAX控件工具包 – jQuery – 哦,我的?
- 显示在文本框,标签后面的菜单. ASP.NET/C#
- 在ASP.NET中更改doctype
- asp.net-mvc – 是否有一种方法来创建一个ActionLink与HTML
- asp.net-core – 针对每个记录器定位不同的提供程序
- asp.net – 如何使用.net用户控件创建Umbraco内容节点?
- asp.net-mvc – 用于MVC 3的Castle Windsor依赖关系解析器
- ASP.NET Web API控制器专用串行器
- asp.net – 限制关于AJAX调用的视图状态信息
- asp.net-mvc – ASP MVC 5项目’New Scaffolded Item …’在
推荐文章
站长推荐
- asp.net – WCF服务问题 – 不支持协议“http”.
- asp.net-mvc – ASP.Net MVC输出缓存:指令或配置
- asp.net-mvc – IIS没有提供.dat文件
- entity-framework – Entity Framework EntityTy
- 如何动态更改整个ASP.NET应用程序的主题?
- asp.net-mvc – 在MVC 3中跨请求保持查询字符串
- asp.net-mvc – 使用NHibernate.AspNet.Identity
- asp.net – Form .NET中的输入元素名称
- asp.net – Xdocument不打印声明
- asp.net – 如何获取Silverlight XAP复制到客户端
热点阅读