473,883 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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=dDw xMzU4OTE3NTA2Oz s%2BTK88jS63JXN 181X3N8zKivua8c o%3D&txt_userna me=xxx&txt_pass word=xxxxxx&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="loginpa ge.aspx"
language="javas cript" onsubmit="if (!ValidatorOnSu bmit()) return false;"
<input type="hidden" name="__VIEWSTA TE"
value="dDwxMzU4 OTE3NTA2Ozs+TK8 8jS63JXN181X3N8 zKivua8co=" />
<input name="txt_usern ame" type="text" id="txt_usernam e" />
<input name="txt_passw ord" type="password" id="txt_passwor d" />
<input type="submit" name="btn_login " value="Login" onclick="if
(typeof(Page_Cl ientValidate) == 'function') Page_ClientVali date(); "
language="javas cript" 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_Session Id=lhjccg45vfhx 3eixl1d3ye55\r\ n
Line-based text data: application/x-www-form-urlencoded
__VIEWSTATE=dDw xMzU4OTE3NTA2Oz s%2BTK88jS63JXN 181X3N8zKivua8c o%3D&txt_userna me=wom&txt_pass word=wauters&bt n_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><ti tle>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>Invali d character in a Base-64 string.</title>
\tbody {font-family:"Verdana ";font-weight:normal;f ont-size:
..7em;color:bla ck;}
{font-family:"Verdana ";font-weight:normal;c olor:black;marg in-top: -5px}
{font-family:"Verdana ";font-weight:bold;col or:black;margin-top: -5px}
\tH1 {
font-family:"Verdana ";font-weight:normal;f ont-size:18pt;color :red }
\tH2 {
font-family:"Verdana ";font-weight:normal;f ont-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:unde rline; font-weight:bold;
color:navy; cursor:hand; }

<body bgcolor="white" >

<span><H1>Serve r 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.Crea te(loginDlgUri)
'get the response object, so that we may get the session cookie.
Dim response As HttpWebResponse
response = request.GetResp onse()
' populate the cookie container.
request.CookieC ontainer = cookies
response.Cookie s = request.CookieC ontainer.GetCoo kies(request.Re questUri)

'read the incoming stream containing the login dialog page.
Dim reader As StreamReader = New StreamReader(re sponse.GetRespo nseStream())
Dim loginDlgPage = reader.ReadToEn d()
' 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(lo ginDlgPage)
' 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=Log in", viewState, username, password)
' our second request is the POST of the username / password data.
Dim request2 As HttpWebRequest = WebRequest.Crea te(loginDlgUri)
request2.Method = "POST"
request2.Conten tType = "applicatio n/x-www-form-urlencoded"
request2.Cookie Container = cookies
' write our postback data into the request stream
Dim writer As StreamWriter = New StreamWriter(re quest2.GetReque stStream())
writer.Write(po stback)
'request2.GetRe sponse().Close( )
'our third request is for the actual webpage after the login.
Dim request3 As HttpWebRequest = WebRequest.Crea te(mainConsoleU ri)
request3.Cookie Container = cookies
reader = New StreamReader(re quest3.GetRespo nse().GetRespon seStream())
' and read the response
Dim page As String = reader.ReadToEn d()
MessageBox.Show (page)
' our webpage data is in the 'page' string.
Catch WebExcp As WebException
MessageBox.Show (WebExcp.ToStri ng)
End Try
End Function
' extract the viewstate data from a page.
Private Function GetViewState(By Val s As String) As String
Dim viewStateNameDe limiter As String = "__VIEWSTAT E"
Dim valueDelimiter As String
Dim viewStateNamePo sition, viewStateValueP osition, viewStateStartP osition,
viewStateEndPos ition As Integer
valueDelimiter = "value="
viewStateNamePo sition = s.IndexOf(viewS tateNameDelimit er)
viewStateValueP osition = s.IndexOf(value Delimiter, viewStateNamePo sition)
viewStateStartP osition = viewStateValueP osition + Len(valueDelimi ter)
viewStateEndPos ition = s.IndexOf("""", viewStateStartP osition + 1)
Return httpUtility.url encodenicode(s. Substring(viewS tateStartPositi on,
viewStateEndPos ition - viewStateStartP osition))
End Function

Nov 21 '05 #1
0 3893

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" anywhere. I looked around and found a class called HttpWorkerRequest which has a method called GetHttpVersion() which returns the exact thing I am missing. However, I can't see how to get at the HttpWorkerRequest from a Page or from the HttpApplication.
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 multipart/form-data post. I have tried it using a regular html form and it works but that is no good because it leaves you at the other server where
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 - uses GET method. However I have to open remote server page using POST method. Normally this can be achieved using <form ACTION="remote-page-url"> tag. However this is impossible with ASP.NET. Also, using WebRequest class is not good solution, as I...
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 programs can be successfully compiled. There is no error message shown at running also. But the httpLook confirms that the program doesn't post. -- Here is the HTTP request acquired by the software: ---
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 as per RFC 2617 (http://www.faqs.org/rfcs/rfc2617.html). My attempts to authenticate are failing. The server requires the header to be present with the request. For security reasons, it will not reply in any way if the header is not present. ...
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 one in particular is causing the exception "The underlying connection was closed: The server committed an HTTP protocol violation" to be thrown. I found a post that said to add a config file with the following.... <configuration>...
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 number passed as an argument. The first time I invoke it, it works fine. However when I do a second invokation, the http request hangs until it times out. When I tell to time out at 30 minutes, it hangs for those 30 minutes, then times out. I...
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 enctype="multipart/form-data" method="post" name="testform">
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 also want to return the HTTP header information (response object). Does anyone have an insight as to any code samples or .Net objects I would use to accomplish this?
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.