HttpWebRequest to login.aspx via winform


S

SP

Hi ,
what's wrong???
I try this code, but no authentication can be done, and always the
Login page is return.
It's right the set cookie?
Have you got some suggestion?


Imports System.Web
Imports System.Security
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
Dim oCookies As CookieContainer
Dim headerCookies As WebHeaderCollection = New WebHeaderCollection
Dim cookieCache As CookieContainer = New CookieContainer


#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As
Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form
Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(208, 104)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(48, 16)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(176, 24)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(96, 56)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Button2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim url1 As String = "https://www.mysite.com/Login.aspx"

oCookies = New CookieContainer
Dim page1 As HttpWebResponse = callHttp(url1, Nothing)

Dim enc As Encoding = System.Text.Encoding.GetEncoding(1252)

Dim loResponseStream As StreamReader = New
StreamReader(page1.GetResponseStream(), enc)

Dim lcHtml As String = loResponseStream.ReadToEnd()
Dim urlLogin As String =
"https://www.mysite.com/Login.aspx?ReturnUrl=/MyUrl.aspx"

page1.Close()
loResponseStream.Close()


Dim postData As New ArrayList

Dim viewState As String = ExtractViewState(lcHtml)
postData.Add(New String() {"__VIEWSTATE", viewState})

postData.Add(New String() {"txtUserId", "user"})
postData.Add(New String() {"txtPassword", "pwd"})
postData.Add(New String() {"LoginButton", "Login"})


Dim page2 As HttpWebResponse = callHttp(urlLogin, postData)

loResponseStream = New StreamReader(page2.GetResponseStream(),
enc)

lcHtml = loResponseStream.ReadToEnd()
page2.Close()
loResponseStream.Close()



End Sub
Public Function callHttp(ByVal url As String, ByVal postData As
ArrayList) As HttpWebResponse
Dim loHttp As HttpWebRequest = CType(WebRequest.Create(url),
HttpWebRequest)

loHttp.CookieContainer = Me.cookieCache
loHttp.Headers.Add(Me.headerCookies)

'*** Send any POST data
If (Not postData Is Nothing) Then
Dim key As String
Dim lcPostData As String = ""
Dim i = 0
For i = 0 To postData.Count - 1
Dim nameValuePair As String() = postData.Item(i)
lcPostData += nameValuePair(0) + "=" + nameValuePair(1)
+ "&"
Next
If (lcPostData <> "") Then
lcPostData = lcPostData.Substring(0, lcPostData.Length
- 1)
End If
loHttp.ContentType = "application/x-www-form-urlencoded"
loHttp.Method = "POST"
Dim requestWriter As StreamWriter = New
StreamWriter(loHttp.GetRequestStream())
requestWriter.Write(lcPostData)
requestWriter.Close()
'Dim loPostData As Stream = loHttp.GetRequestStream()
'loPostData.Write(lbPostBuffer, 0, lbPostBuffer.Length)
'loPostData.Close()
Else
loHttp.Method = "GET"
End If
loHttp.AllowAutoRedirect = True

loHttp.KeepAlive = True
loHttp.Headers.Set("Pragma", "no-cache")
loHttp.Timeout = 5000
Dim loWebResponse As HttpWebResponse =
CType(loHttp.GetResponse(), HttpWebResponse)
ReadCookies(loWebResponse)




Return loWebResponse
End Function

Private Function ReadCookies(ByVal resp As HttpWebResponse)
Dim found As Boolean = False
Dim cache As CookieCollection =
cookieCache.GetCookies(resp.ResponseUri)
Dim cookie As Cookie
For Each cookie In resp.Cookies

found = False
Dim oldCookie As Cookie
For Each oldCookie In cache
If oldCookie.Name.Equals(cookie.Name) Then

oldCookie.Value = cookie.Value
found = True

End If
If (Not found) Then
Me.cookieCache.Add(cookie)
End If
Next oldCookie


Next cookie

Dim key As String
For Each key In resp.Headers.Keys

If (key.ToLower() = "set-cookie") Then
Me.headerCookies.Add("Cookie",
resp.Headers(key).ToString())
End If
Next key
End Function

Private Function ExtractViewState(ByVal s As String) As String

Dim viewStateNameDelimiter As String = "__VIEWSTATE"
Dim valueDelimiter As String = "value="""

Dim viewStateNamePosition As Integer =
s.IndexOf(viewStateNameDelimiter)
Dim viewStateValuePosition As Integer =
s.IndexOf(valueDelimiter, viewStateNamePosition)

Dim viewStateStartPosition As Integer = viewStateValuePosition
+ valueDelimiter.Length
Dim viewStateEndPosition As Integer = s.IndexOf("""",
viewStateStartPosition)

Return s.Substring(viewStateStartPosition, viewStateEndPosition
- viewStateStartPosition)
End Function







End Class
 
Ad

Advertisements

J

Joerg Jooss

Thus wrote SP,
Hi ,
what's wrong???
I try this code, but no authentication can be done, and always the
Login page is return.
[...]

Don't get me wrong, but try to post just the relevant part of your code...
nobody is going to wade through reams of Windows Forms code that are completely
irrevelant (unless one is paid to do so, I guess).

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top