使用凭据从Windows窗体中打开URL

时间:2013-02-07 22:22:08

标签: vb.net winforms

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

        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()

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

如果你想登录网站我认为最好从HTTP使用POST Methode,你必须定义你的登录页面参数用户和密码(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

Referance Articel:http://tech.reboot.pro/showthread.php?tid=61