【问题标题】:Open a URL from a windows form with credentials使用凭据从 Windows 窗体打开 URL
【发布时间】:2013-02-07 22:22:07
【问题描述】:

我正在尝试使用用户名和密码打开一个外部网站。我有正确的凭据,但是当我尝试以下代码时,我收到“登录失败:未知用户名或密码错误”错误。

        Dim username As String = "username"
        Dim password As New System.Security.SecureString

        'Set value for SecureString type variable   
        Dim plainPassword As String = "pass"
        For Each c As String In plainPassword.ToCharArray
            password.AppendChar(c)
        Next c

        Dim IEprocess As System.Diagnostics.Process = New System.Diagnostics.Process
        IEprocess.StartInfo.FileName = "http://www.website.com/"
        IEprocess.StartInfo.UserName = username
        IEprocess.StartInfo.Password = password
        IEprocess.StartInfo.UseShellExecute = False
        IEprocess.Start()

有什么建议吗?

【问题讨论】:

    标签: vb.net winforms


    【解决方案1】:

    如果你想登录网站,我认为最好使用 HTTP 的 POST 方法,并且你必须定义你的登录页面参数用户和密码(Php、ASP.Net、JAVA 推荐可以使用操作方法)。

    Dim PostData As String = "define your action login" 'example https://login.yahoo.com/config/login_verify2?&.src=ym,username and Password parameters
        Dim bytes() As Byte = ASCIIEncoding.UTF8.GetBytes(PostData)
        Dim httpReg As HttpWebRequest = WebRequest.Create("http://www.website.com/")
        httpReg.Method = "POST"
        httpReg.KeepAlive = True
        httpReg.CookieContainer = mainCookie
        httpReg.ContentType = "application/x-www-form-urlencoded"
        httpReg.Referer = "http://www.website.com/index.html"
        httpReg.ContentLength = bytes.Length
        Dim DtStream As Stream = httpReg.GetRequestStream()
        DtStream.Write(bytes, 0, bytes.Length)
        DtStream.Close()
        Dim httpResponse As HttpWebResponse
        httpResponse = httpReg.GetResponse()
        mainCookie.Add(httpResponse.Cookies)
        Dim reader As New StreamReader(httpResponse.GetResponseStream())
        Dim strSource As String = reader.ReadToEnd
        If strSource.Contains("Selamat Datang") Then
            MessageBox.Show("Sukses Login")
        Else
            MessageBox.Show("Gagal Login")
        End If
    

    参考文章:http://tech.reboot.pro/showthread.php?tid=61

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 1970-01-01
      • 2020-08-21
      • 2014-08-16
      • 1970-01-01
      • 2017-08-27
      • 2017-03-14
      相关资源
      最近更新 更多