469,328 Members | 1,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

Https form post using Httpwebrequest brings back the same page.

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;

Jan 23 '07 #1
6 7543
Thus wrote ng******@yahoo.com,
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,
--
Joerg Jooss
ne********@joergjooss.de
Jan 23 '07 #2
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 Jooss wrote:
Thus wrote ng******@yahoo.com,
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,
--
Joerg Jooss
ne********@joergjooss.de
Jan 23 '07 #3
Thus wrote ng******@yahoo.com,
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,
--
Joerg Jooss
ne********@joergjooss.de
Jan 23 '07 #4
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

Joerg Jooss wrote:
Thus wrote ng******@yahoo.com,
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,
--
Joerg Jooss
ne********@joergjooss.de
Jan 23 '07 #5
Thus wrote ng******@yahoo.com,
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,
--
Joerg Jooss
ne********@joergjooss.de
Jan 25 '07 #6
Thanks for your help. I figured it out, I had to urlencode the field
names and now it works.

Thanks,
NeelG

Jan 31 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.