Https form post using Httpwebrequest brings back the same page.

N

nganapat

I am trying to post form values to a https web page programmatically
using Httpwebrequest but no matter what I do the same login page is
returned instead of the next page. I would very much appreciate if
someone could show me what is it that I am doing wrong. Below is the
code that I am using.

string viewstate = HttpUtility.UrlEncode(viewstatevalue);
StringBuilder data = new StringBuilder();
data.Append("VAM_Group=");
data.Append("&__VIEWSTATE=" + viewstate);
data.Append("&ctlSignon:txtUserID=userid");
data.Append("&ctlSignon:txtPassword=password");
data.Append("&ctlSignon:ddlSignonDestination=");
data.Append("&ctlSignon:chkMakeDefaultPage=on");
data.Append("&ctlSignon:btnLogin=Login");
data.Append("&TestJavaScript=OK");

byte[] bBuffer;
bBuffer = Encoding.UTF8.GetBytes(data.ToString());

HttpWebRequest req =

(HttpWebRequest)WebRequest.Create("https://online.texanscu.org/texans/login.aspx");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bBuffer.Length;
req.Credentials = CredentialCache.DefaultCredentials;
req.KeepAlive = true;
req.CookieContainer = new CookieContainer();

Stream swOut = req.GetRequestStream();
swOut.Write(bBuffer,0,bBuffer.Length);

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream sr = resp.GetResponseStream();
string result = new StreamReader(sr).ReadToEnd();

return result;
 
J

Joerg Jooss

Thus wrote (e-mail address removed),
I am trying to post form values to a https web page programmatically
using Httpwebrequest but no matter what I do the same login page is
returned instead of the next page. I would very much appreciate if
someone could show me what is it that I am doing wrong. Below is the
code that I am using. [...]
(HttpWebRequest)WebRequest.Create("https://online.texanscu.org/texans/
login.aspx");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bBuffer.Length;
req.Credentials = CredentialCache.DefaultCredentials;

This is only meaningful for Windows Authentication. Your site seems to be
using Forms Authentication -- you already send username and password as form
data.
req.KeepAlive = true;
req.CookieContainer = new CookieContainer();

Note that you should one CookieContainer instance throughout the entire HTTP
message exchange. If you create a CookieContainer for each request, you throw
away your previously received session and authentication cookies.


Cheers,
 
N

nganapat

Joerg,

Thanks a lot for your reply. I understand that when I write
req.CookieContainer = new CookieContainer(); it creates a new
CookieContainer for each request. I am new to this. I would very much
appreciate if you could show me how to create one CookieContainer
instance throughout the entire HTTP message exchange. Thank you once
again.

NeelG

Joerg said:
Thus wrote (e-mail address removed),
I am trying to post form values to a https web page programmatically
using Httpwebrequest but no matter what I do the same login page is
returned instead of the next page. I would very much appreciate if
someone could show me what is it that I am doing wrong. Below is the
code that I am using. [...]
(HttpWebRequest)WebRequest.Create("https://online.texanscu.org/texans/
login.aspx");
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bBuffer.Length;
req.Credentials = CredentialCache.DefaultCredentials;

This is only meaningful for Windows Authentication. Your site seems to be
using Forms Authentication -- you already send username and password as form
data.
req.KeepAlive = true;
req.CookieContainer = new CookieContainer();

Note that you should one CookieContainer instance throughout the entire HTTP
message exchange. If you create a CookieContainer for each request, you throw
away your previously received session and authentication cookies.


Cheers,
 
J

Joerg Jooss

Thus wrote (e-mail address removed),
Joerg,

Thanks a lot for your reply. I understand that when I write
req.CookieContainer = new CookieContainer(); it creates a new
CookieContainer for each request. I am new to this. I would very much
appreciate if you could show me how to create one CookieContainer
instance throughout the entire HTTP message exchange. Thank you once
again.

Simply make that CookieContainer a field of your HTTP client class, e.g.

class HttpClient
{
private CookieContainer cookieContainer = new CookieContainer();

// Your methods...
}

In your method, assign this.cookieContainer to HttpWebReqest.CookieContainer
for each request you send.

Cheers,
 
N

nganapat

I tried what you suggested and that did not work. I am inserting the
whole code that I have in my form (I have created a windows application
- the form has a textbox and a button). Please let me know what I have
done wrong.

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private CookieContainer cookiejar = new CookieContainer();

public string ScrapeURL(string URL)
{
string viewstate = HttpUtility.UrlEncode(viewstatevalue);
StringBuilder data = new StringBuilder();
data.Append("VAM_Group=");
data.Append("&__VIEWSTATE=" + viewstate);
data.Append("&ctlSignon:txtUserID=userid");
data.Append("&ctlSignon:txtPassword=password");
data.Append("&ctlSignon:ddlSignonDestination=");
data.Append("&ctlSignon:chkMakeDefaultPage=on");
data.Append("&ctlSignon:btnLogin=Login");
data.Append("&TestJavaScript=OK");

byte[] bBuffer;

bBuffer = Encoding.UTF8.GetBytes(data.ToString());

HttpWebRequest req =
(HttpWebRequest)WebRequest.Create(URL);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bBuffer.Length;
req.Credentials = CredentialCache.DefaultCredentials;
req.KeepAlive = true;
req.CookieContainer = this.cookiejar;

Stream swOut = req.GetRequestStream();
swOut.Write(bBuffer,0,bBuffer.Length);

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Stream sr = resp.GetResponseStream();
string result = new StreamReader(sr).ReadToEnd();

return result;
}

private void button1_Click(object sender, EventArgs e)
{
textBox1.Text =
ScrapeURL("https://online.texanscu.org/texans/login.aspx");
}
}

I appreciate your help.

Thanks,
NeelG
 
J

Joerg Jooss

Thus wrote (e-mail address removed),
I tried what you suggested and that did not work. I am inserting the
whole code that I have in my form (I have created a windows
application - the form has a textbox and a button). Please let me know
what I have done wrong.

You should capture the HTTP traffic between browser and server using a browser
plugin and program your web requests accordingly. Maybe you're missing a
cookie that the web site issues before you hit the login page, or maybe the
site expects you to to send a User-Agent header, etc.

And sending a previously captured ASP.NET view state in your first requests
seems pretty wrong. You should send the view state received at runtime

Cheers,
 
N

nganapat

Thanks for your help. I figured it out, I had to urlencode the field
names and now it works.

Thanks,
NeelG
 

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