473,289 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

HTTP POST + Webrequest

Hi,

I want to login to a password protected website and fetch the content of the
page behind. I have based my code on
http://weblogs.asp.net/jdennany/arch...23/403971.aspx. When I use
tools like ieHTTPHeaders v1.6, and I perform a normal login (using the
normal website), I see that the viewstate is
__VIEWSTATE=dDwxMzU4OTE3NTA2Ozs%2BTK88jS63JXN181X3 N8zKivua8co%3D&txt_username=xxx&txt_password=xxxxx x&btn_login=Login.
When I verify my program, this is exactly the same.

In the HTML code, this is the relevant part:
<form name="_ctl0" method="post" action="loginpage.aspx"
language="javascript" onsubmit="if (!ValidatorOnSubmit()) return false;"
id="_ctl0">
<input type="hidden" name="__VIEWSTATE"
value="dDwxMzU4OTE3NTA2Ozs+TK88jS63JXN181X3N8zKivu a8co=" />
<input name="txt_username" type="text" id="txt_username" />
<input name="txt_password" type="password" id="txt_password" />
<input type="submit" name="btn_login" value="Login" onclick="if
(typeof(Page_ClientValidate) == 'function') Page_ClientValidate(); "
language="javascript" id="btn_login" />

But the problem is that it does not give me the correct page, it always
refers me to the original login page, which clearly shows me sth is wrong.
So, I have used Ethereal to see the HTTP post and below is what I get when I
use the normal website procedure as well as when I use my program. With my
program it always says in the get (after the post) the following: "Invalid
character in a Base-64 string". Maybe this is the problem?

Another problem is the viewstates. They are slightly different, depending on
using ieHttpHeaders or Etheral.

Anyone can help me? (For those interested I inserted my code below)

Using the normal website login:

HTTP POST => Hypertext Transfer Protocol
POST /loginpage.aspx HTTP/1.1\r\n
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg,
application/x-shockwave-flash, application/vnd.ms-excel,
application/vnd.ms-powerpoint, application/msword, */*\r\n
Referer: http://www.cleverpc.be/loginpage.aspx\r\n
Accept-Language: nl-be\r\n
Content-Type: application/x-www-form-urlencoded\r\n
Accept-Encoding: gzip, deflate\r\n
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET
CLR 1.1.4322)\r\n
Host: www.cleverpc.be\r\n
Content-Length: 118\r\n
Connection: Keep-Alive\r\n
Cache-Control: no-cache\r\n
Cookie: ASP.NET_SessionId=lhjccg45vfhx3eixl1d3ye55\r\n
\r\n
Line-based text data: application/x-www-form-urlencoded
__VIEWSTATE=dDwxMzU4OTE3NTA2Ozs%2BTK88jS63JXN181X3 N8zKivua8co%3D&txt_username=wom&txt_password=waute rs&btn_login=Login

HTTP GET => Hypertext Transfer Protocol
HTTP/1.1 302 Found\r\n
Date: Sun, 02 Oct 2005 20:24:32 GMT\r\n
Server: Microsoft-IIS/6.0\r\n
X-Powered-By: ASP.NET\r\n
X-AspNet-Version: 1.1.4322\r\n
Location: /adminpages/adminindex.aspx\r\n
Cache-Control: private\r\n
Content-Type: text/html; charset=iso-8859-1\r\n
Content-Length: 144\r\n
\r\n
Line-based text data: text/html
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href='/adminpages/adminindex.aspx'>here</a>.</h2>
</body></html>
Using my program:

HTTP POST => Hypertext Transfer Protocol
POST /loginpage.aspx HTTP/1.1\r\n
Content-Type: application/x-www-form-urlencoded\r\n
Content-Length: 117\r\n
Expect: 100-continue\r\n
Host: www.cleverpc.be\r\n
\r\n
HTTP GET => Hypertext Transfer Protocol
GET /adminpages/adminindex.aspx HTTP/1.1\r\n
Host: www.cleverpc.be\r\n
\r\n

Hypertext Transfer Protocol
HTTP/1.1 500 Internal Server Error\r\n
Date: Sun, 02 Oct 2005 20:27:40 GMT\r\n
Server: Microsoft-IIS/6.0\r\n
X-Powered-By: ASP.NET\r\n
X-AspNet-Version: 1.1.4322\r\n
Cache-Control: private\r\n
Content-Type: text/html; charset=iso-8859-1\r\n
Content-Length: 4476\r\n
\r\n
Line-based text data: text/html
<html>
<head>
<title>Invalid character in a Base-64 string.</title>
<style>
\tbody {font-family:"Verdana";font-weight:normal;font-size:
..7em;color:black;}
\tp
{font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
\tb
{font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
\tH1 {
font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
\tH2 {
font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
\tpre {font-family:"Lucida Console";font-size: .9em}
\t.marker {font-weight: bold; color: black;text-decoration:
none;}
\t.version {color: gray;}
\t.error {margin-bottom: 10px;}
\t.expandable { text-decoration:underline; font-weight:bold;
color:navy; cursor:hand; }
</style>
</head>

<body bgcolor="white">

<span><H1>Server Error in '/' Application.<hr width=100%
size=1 color=silver></H1>

<h2> <i>Invalid character in a Base-64 string.</i>
</h2></span>

<font face="Arial, Helvetica, Geneva, SunSans-Regular,
sans-serif ">

<b> Description: </b>An

Program code

Public Function ForceLogin() As Boolean
Try
Dim loginDlgUri, username, password, mainConsoleUri, baseUri As String
baseUri = "http://www.test.com/"
loginDlgUri = baseUri & "loginpage.aspx"
mainConsoleUri = baseUri & "adminpages/adminindex.aspx"
username = "xxx"
password = "xxxxxx"
Dim cookies As CookieContainer = New CookieContainer
' perform the first http request against the asp.net application login
dialog.
Dim request As HttpWebRequest = WebRequest.Create(loginDlgUri)
'get the response object, so that we may get the session cookie.
Dim response As HttpWebResponse
response = request.GetResponse()
' populate the cookie container.
request.CookieContainer = cookies
response.Cookies = request.CookieContainer.GetCookies(request.Request Uri)

'read the incoming stream containing the login dialog page.
Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
Dim loginDlgPage = reader.ReadToEnd()
reader.Close()
' extract the viewstate value from the login dialog page. We need to post
this back,
' along with the username and password
Dim viewState As String = GetViewState(loginDlgPage)
' build postback string
' This string will vary depending on the page. The best
' way to find out what your postback should look like is to
' monitor a normal login using a utility like TCPTrace.
Dim postback As String = String.Format("__VIEWSTATE={0}&txt_username={1} &
txt_password={2}&btn_login=Login", viewState, username, password)
' our second request is the POST of the username / password data.
Dim request2 As HttpWebRequest = WebRequest.Create(loginDlgUri)
request2.Method = "POST"
request2.ContentType = "application/x-www-form-urlencoded"
request2.CookieContainer = cookies
' write our postback data into the request stream
Dim writer As StreamWriter = New StreamWriter(request2.GetRequestStream())
writer.Write(postback)
writer.Close()
'request2.GetResponse().Close()
'our third request is for the actual webpage after the login.
Dim request3 As HttpWebRequest = WebRequest.Create(mainConsoleUri)
request3.CookieContainer = cookies
reader = New StreamReader(request3.GetResponse().GetResponseStr eam())
' and read the response
Dim page As String = reader.ReadToEnd()
MessageBox.Show(page)
reader.Close()
' our webpage data is in the 'page' string.
Catch WebExcp As WebException
MessageBox.Show(WebExcp.ToString)
End Try
End Function
' extract the viewstate data from a page.
Private Function GetViewState(ByVal s As String) As String
Dim viewStateNameDelimiter As String = "__VIEWSTATE"
Dim valueDelimiter As String
Dim viewStateNamePosition, viewStateValuePosition, viewStateStartPosition,
viewStateEndPosition As Integer
valueDelimiter = "value="
viewStateNamePosition = s.IndexOf(viewStateNameDelimiter)
viewStateValuePosition = s.IndexOf(valueDelimiter, viewStateNamePosition)
viewStateStartPosition = viewStateValuePosition + Len(valueDelimiter)
viewStateEndPosition = s.IndexOf("""", viewStateStartPosition + 1)
Return httpUtility.urlencodenicode(s.Substring(viewStateS tartPosition,
viewStateEndPosition - viewStateStartPosition))
End Function


Nov 21 '05 #1
0 3826

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Juan Dent | last post by:
Hi, The Request.SaveAs method stores in a file the complete HTTP request including the HttpVersion. However when I try to reproduce in memory the same thing, I cannot find the "HTTP/1.1"...
1
by: LD | last post by:
Hi, I'm pulling my hair out!! My problem is, I need to automatically upload a zip file along with 3 other pieces of text data to a web server and wait for it's xml response. Basically a...
4
by: Piotr Strycharz | last post by:
Hi all I do have a problem. How can I transfer user to another server using POST. The problem is that Server.Transfer (preserves form data) works just in current server. Response.Redirect -...
0
by: Owen | last post by:
Hello everyone, I am using VS.NET 2003(Trandition Chinese) Edition, and httpLook software for checking http requests. I found a problem that the following programs don't really "POST". These...
3
by: Patrick Fogarty | last post by:
I am programming what is to be a web service client that will use an HTTP-POST to request and retrieve data. The remote server (written in java for what it's worth) requires basic authentication...
1
by: Dan W. | last post by:
Does anyone have any experience with this problem. I am trying to post about 20 fields of information to another server using System.Net.WebClient.UploadData. This works fine for some servers but...
0
by: Glenn Venzke | last post by:
I am trying to invoke a web service from an ASP.net page using the WebRequest object to an HTTP post. The web service is basically a print manager that prints sets of documents based an an invoice...
2
by: MDANH2002 | last post by:
Hi From VB.NET I want to simulate the POST request of the following HTML form <html> <title>HTTP Post Testing</title> <body> <form action=http://www.example.com/postdata ...
2
by: =?Utf-8?B?UGF1bA==?= | last post by:
I am using ASP.Net 2.0 and VB.Net (although C#is ok also). I want to create an object/method/function that will take a URL as an input parameter and then return all of the HTML in that page. I...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.