Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / January 2007

Tip: Looking for answers? Try searching our database.

Https form post using Httpwebrequest brings back the same page.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
nganapat@yahoo.com - 23 Jan 2007 17:16 GMT
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;
Joerg Jooss - 23 Jan 2007 19:15 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.