Thus wrote nganapat@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,

Signature
Joerg Jooss
news-reply@joergjooss.de
nganapat@yahoo.com - 23 Jan 2007 20:36 GMT
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
> Thus wrote nganapat@yahoo.com,
>
[quoted text clipped - 23 lines]
>
> Cheers,
Joerg Jooss - 23 Jan 2007 21:05 GMT
Thus wrote nganapat@yahoo.com,
> Joerg,
>
[quoted text clipped - 4 lines]
> 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,

Signature
Joerg Jooss
news-reply@joergjooss.de
nganapat@yahoo.com - 23 Jan 2007 21:52 GMT
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
> Thus wrote nganapat@yahoo.com,
>
[quoted text clipped - 20 lines]
>
> Cheers,
Joerg Jooss - 25 Jan 2007 19:27 GMT
Thus wrote nganapat@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,

Signature
Joerg Jooss
news-reply@joergjooss.de
nganapat@yahoo.com - 31 Jan 2007 18:08 GMT
Thanks for your help. I figured it out, I had to urlencode the field
names and now it works.
Thanks,
NeelG