473,238 Members | 1,919 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,238 software developers and data experts.

HTTP POST + Webrequest


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;"
<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
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
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>
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
HTTP GET => Hypertext Transfer Protocol
GET /adminpages/adminindex.aspx HTTP/1.1\r\n
Host: www.cleverpc.be\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
Line-based text data: text/html
<title>Invalid character in a Base-64 string.</title>
\tbody {font-family:"Verdana";font-weight:normal;font-size:
{font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
{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:
\t.version {color: gray;}
\t.error {margin-bottom: 10px;}
\t.expandable { text-decoration:underline; font-weight:bold;
color:navy; cursor:hand; }

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

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

<b> Description: </b>An

Program code

Public Function ForceLogin() As Boolean
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
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()
' 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())
'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()
' our webpage data is in the 'page' string.
Catch WebExcp As WebException
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 3816

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

Similar topics

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"...
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...
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 -...
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...
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...
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...
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...
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 ...
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...
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
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...
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...
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
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 =...

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.