programmatically login to secure website

varvar

Member
Joined
Jun 8, 2006
Messages
10
Programming Experience
3-5
hi everyone
I'm trying to login in secure site. nike.net. from my windows application.
I am using vb.net. I'm able to catch all the cookies and headers from firtst httpwebresponse.
but when i'm trying to login (i used both webclient and httpwebrequest) i'm getting "System.Net.WebException = {"The remote server returned an error: (500) Internal Server Error."}"
error. I cannnot move forward from here. I tried to deal with credentials. didn't work either.
all cookies and headers i picked up from previous httpwebresponse. i also checked it with httpanalyzer and everything seems right.
may be i'm not using right credentials.
login and password is changed, but if code works we supposed to get the same page with bad login message, but not webexception i'm getting now.
below is the code i'm using. Thank you very much.

sURL = "https://www.nike.net/portal/site/nike/index.jsp?epi-content=LOGIN&epi-process=process_login.jsp"
cookie1 = "b2rTest=; vap_JSESSIONID=FaCo5NAKKlD053eWGdii1qNGMWCtTE2LGx3ofb7V0KFaxdmw4Y80!-1263957463!-71976742"
c.Add(New Cookie("b2rTest", "", "/", ".nike.net"))
Dim postData As String
WebReq = WebRequest.Create(sURL)
WebReq.Referer = "https://www.nike.net/portal/site/nike/index.jsp?epi-content=LOGIN"
WebReq.ContentType = "application/x-www-form-urlencoded"
WebReq.Headers.Add("Cache-Control", "no-cache")
WebReq.CookieContainer = New CookieContainer
WebReq.CookieContainer.Add(c)
WebReq.AllowAutoRedirect = False
WebReq.Headers.Add("UA-CPU", "x86")
WebReq.Method = "POST"
WebReq.Accept = "*/*"
WebReq.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"
WebReq.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-us")
WebReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate")
WebReq.KeepAlive = True
WebReq.Headers.Add("Cookie", cookie1)
Dim data() As Byte
postData = "lang=null&logon=xxxxxx@xxx.com&password=xxxxx&submit_logon=Submit&changeLanguageSelect=en&realm=realm1"
data = ASCIIEncoding.ASCII.GetBytes(HttpUtility.UrlEncode(postData))
WebReq.ContentLength = data.Length
myStream = WebReq.GetRequestStream()
myStream.Write(data, 0, data.Length)
myStream.Close()
WebReq.Credentials = New NetworkCredential("xxxxxx@xxx.com", "xxxxx", www.nike.net)
WebRes = WebReq.GetResponse ' this line throws me System.Net.WebException
sr = New StreamReader(WebRes.GetResponseStream)
strHTML = sr.ReadToEnd
 
i think that session ID in cookie1 will have long expired by now.. Why are you putting it in there? The server wont keep your session forever; youre supposed to get a new one when you log in
 
SessionId is saved in previous request.

thanks for reply.

Actually i'm getting sessionid from the first webresponse. I'm saving it to the string cookie1 and also in cookiecollection c.

Dim sURL As String = ""
Dim WebReq As HttpWebRequest
Dim WebRes As HttpWebResponse
Dim myStream As Stream
Dim sr As StreamReader
Dim strHTML As String
sURL = "https://www.nike.net/portal/site/nike/"
WebReq = WebRequest.Create(sURL)

WebReq.AllowAutoRedirect = False
WebRes = WebReq.GetResponse
If Not WebRes.Headers("Set-Cookie") Is Nothing Then
vap_JSESSIONID = WebRes.Headers("Set-cookie")
vap_JSESSIONID = vap_JSESSIONID.Substring(0, vap_JSESSIONID.IndexOf(
";")).Replace("vap_JSESSIONID=", "")
cookie1 =
"vap_JSESSIONID=" & vap_JSESSIONID
c.Add(
New Cookie("vap_JSESSIONID", vap_JSESSIONID, "/", ".nike.net"))
End If
sr = New StreamReader(WebRes.GetResponseStream)
strHTML = sr.ReadToEnd
WebRes =
Nothing : WebReq = Nothing

sURL = " https://www.nike.net/portal/site/nike/index.jsp?epi-content=LOGIN"
WebReq = WebRequest.Create(sURL)
WebReq.Headers.Add(
"Cookie", cookie1)
WebReq.CookieContainer =
New CookieContainer()
WebReq.CookieContainer.Add(c)
WebRes = WebReq.GetResponse
WebRes =
Nothing : WebReq = Nothing
 
Hidden webbrowser

I had no idea that webbrowser control able to programmatically submit forms (perform button click ,etc). If it's possible please post some code for me. thanks a alot
 
I dont accurately remember.. Its all well documented in the DOM section of msdn.. stuff like

WebBrowser1.Document.Body.Forms("loginForm").GetElementByName("usernamebox").Value = "my username"
WebBrowser1.Document.Body.Forms("loginForm").Submit()

stuff like that, i think...
 
Webbrowser method works. it's slower, but it works. the only problem i have now it's I cannot figure our when the document is fully loaded in webbrowser control. The navigate method is asyncronic, so i need to delay the main thread until document is loaded. otherwise i'm getting errors like control doesnot exist,etc. webbrowser method .IsBusy supposed to do the work, but it doesn't or i'm doing something wrong.
So my question is: how to delay the main thread until document is fully loaded.
thanks a lot
 
DOM document is ready loaded when WebBrowser1.ReadyState = WebBrowserReadyState.Complete, or first time WebBrowser.DocumentCompleted event fires.
 
thanks for answer.
looks like webbrowser.DocumentCompleted doesn't fire when i refresh the browser or navigate.
Same with ReadyState. the loop
while wb.readystate<>WebBrowserReadyState.Complete
become infinite.
I had to use timer to get over this problem. i's done, but done in dirty and fast way.
If anyone has more clever ways to do would be nice to see it.
The Problem. I'm trying to parse docs. in webbrowser control and loop through all web pages.
thx again
 
When you want to post a reply you have just written, to VBDNF.. Do you hit the refresh button?

Posting isnt done by a refresh. Submit the form!
 
Back
Top