使用WebRequest登录网站

时间:2015-10-07 15:28:48

标签: vb.net post login httpwebrequest

VB2012:我正在尝试登录我的公司网站来解析一些信息。它有一个典型的页面

  

portal.mycompany.com

最终重定向到

  

security.mycompany.com/login.jsp?TYPE=xxx&METHOD=GET& {more parameters}

在那里我们会看到用户名和密码的文本框。我有Fiddler在运行,并且在设置POST时要注意什么。我的例子看起来与各种编码网站相同。我主要是寻找有关Fiddler中寻找内容的帮助,以此作为POST请求的基础,以编程方式登录我的网站。

我查看了一些Fiddler条目,并添加了我认为是enrty的凭据。但是当我尝试将其添加到POST请求时,它只是回复了原始页面。

    Dim cookieJar As New Net.CookieContainer()
    Dim request As Net.HttpWebRequest
    Dim response As Net.HttpWebResponse
    Dim strURL As String

    Try
        'Get Cookies
        strURL = "http://portal.mycompany.com"
        request = Net.HttpWebRequest.Create(strURL)
        request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
        request.Method = "GET"
        request.CookieContainer = cookieJar
        response = request.GetResponse()

        For Each tempCookie As Net.Cookie In response.Cookies
            cookieJar.Add(tempCookie)
        Next

        'Send the post data now
        request = Net.HttpWebRequest.Create(strURL)
        request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3"
        request.Method = "POST"
        request.AllowAutoRedirect = True
        request.CookieContainer = cookieJar

        Dim writer As StreamWriter = New StreamWriter(request.GetRequestStream())
        writer.Write("email=username&pass=password") 'where do I get this in Fiddler?
        writer.Close()
        response = request.GetResponse()

        'Get the data from the page
        Dim stream As StreamReader = New StreamReader(response.GetResponseStream())
        Dim data As String = stream.ReadToEnd()
        response.Close()

        If data.Contains("<title>MyCompany") = True Then
            'LOGGED IN SUCCESSFULLY
        End If

    Catch ex As Exception
        MsgBox(ex.Message)
    End Try

0 个答案:

没有答案