How to view a page after logon...

L

Lorenzo

Hi i'm writing some code that retrieve the html source code of an asp page.

The problem is that if i go directly to this page the browser redirect me to
a login page where are two textbox in a html form.

<form action="/admin/login.asp?aktion=login" method="POST" name="login">
<table border="2" align="center">
<tr>
<td>
<b>Username:</b>
</td>
<td>
<input class="basic" type="text" size="15" name="username">
</td>
</tr>
<tr>
<td>
<b>Password:</b>
</td>
<td>
<input class="basic" type="password" size="15" name="password">
</td>
<td>
<input class="basic" type="submit" name="enterbut" value="Enter">
<input type="hidden" name="dest" value="">
<input type="hidden" name="qs" value="">
</td>

if i write in the address bar the link
https://xxx.xxx.com/admin/login.asp?aktion=login&username=xxxx&password=xxxx&dest=/admin/Default.asp
i go in the default.asp page.

now...i write this code in c# but it doesn't work...in the txtHtml textbox
is shown always the html source of the login.asp page...
private void Button1_Click(object sender, System.EventArgs e)

{

string postdata;

WebClient page = new WebClient();

CredentialCache cache = new CredentialCache();

CookieContainer ccContainer = new CookieContainer();

ASCIIEncoding encoding = new ASCIIEncoding();

HttpWebRequest request
=(HttpWebRequest)WebRequest.Create("https://xxxx.xxxx.com/admin/login.asp");

request.AllowAutoRedirect = false;

request.Method = "POST";

request.ContentType = @"application/x-www-form-urlencoded";

request.UserAgent = "Mozilla/4.0 (compatible;" +

" MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)";

request.CookieContainer = new CookieContainer();

postdata = "?aktion=login&username="+
HttpUtility.UrlDecode("massimiliano.del.vita") + "&password=" +
HttpUtility.UrlDecode("miami") +

"&dest=" + HttpUtility.UrlDecode("/admin/Default.asp");

request.Credentials = new NetworkCredential("xxxx","xxxx");

request.CookieContainer.Add(ccContainer.GetCookies(new
Uri("https://xxxx.xxxxx.com/admin/login.asp"));

byte[] buf = encoding.GetBytes(postdata);

request.ContentLength = buf.Length;

Stream str = request.GetRequestStream();

str.Write(buf,0,buf.Length);

str.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if(request.HaveResponse)

{

Stream str2 = response.GetResponseStream();

StreamReader strReader = new StreamReader(str2);

txtHtml.Text = strReader.ReadToEnd().Trim();

string result = strReader.ReadToEnd();

}

}



could u help me? tnx
 
L

Lorenzo

Lorenzo wrote:
[CUT]

I solved with this code :
private void Button1_Click(object sender, System.EventArgs e)

{

Encoding enc = Encoding.GetEncoding(1252);

ASCIIEncoding asciiEnc = new ASCIIEncoding();

Encoding ascii = Encoding.ASCII;


string UN = txtUsername.Text;

string PW = txtPass.Text;

string sql = txtSql.Text.Replace(" ","%20").Replace("\n","
").Replace("\r","");

string url = "https://xxx.xxx.com/admin/login.asp?aktion=login&username="+

UN+"&password="+PW

+"&dest=/admin/tracking/queryresults.asp&qs=selectquery"+
HttpUtility.UrlEncode(sql, System.Text.Encoding.GetEncoding("ISO-8859-1"));

txtURI.Text = url;

HttpWebRequest request =(HttpWebRequest)WebRequest.Create(url);

request.ContentType = "application/x-www-form-urlencoded";

request.UserAgent = "Mozilla/4.0 (compatible;" +

" MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)";

request.CookieContainer = new CookieContainer();

try

{

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if(request.HaveResponse)

{

if(response.ContentEncoding.Length > 0)

{

enc = Encoding.GetEncoding(response.ContentEncoding);

}

StreamReader strReader = new StreamReader(response.GetResponseStream(),enc);

txtHtml.Text = strReader.ReadToEnd().Trim();

string result = strReader.ReadToEnd();

strReader.Close();

response.Close();

}

}

catch(WebException wexc)

{

txtUrl.Text = wexc.Message;

}

}

but... often if i write an sql statement in txtSql textbox it appear correct
in txtURI textbox (if i cut & paste it in the explorer address bar worked
well) but the HttpWebResponse response =
(HttpWebResponse)request.GetResponse(); give me an error of type 400 (remote
server error)...

Why?
 

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