473,715 Members | 2,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Https form post using Httpwebrequest brings back the same page.

I am trying to post form values to a https web page programmaticall y
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.Url Encode(viewstat evalue);
StringBuilder data = new StringBuilder() ;
data.Append("VA M_Group=");
data.Append("&_ _VIEWSTATE=" + viewstate);
data.Append("&c tlSignon:txtUse rID=userid");
data.Append("&c tlSignon:txtPas sword=password" );
data.Append("&c tlSignon:ddlSig nonDestination= ");
data.Append("&c tlSignon:chkMak eDefaultPage=on ");
data.Append("&c tlSignon:btnLog in=Login");
data.Append("&T estJavaScript=O K");

byte[] bBuffer;
bBuffer = Encoding.UTF8.G etBytes(data.To String());

HttpWebRequest req =

(HttpWebRequest )WebRequest.Cre ate("https://online.texanscu .org/texans/login.aspx");
req.Method = "POST";
req.ContentType = "applicatio n/x-www-form-urlencoded";
req.ContentLeng th = bBuffer.Length;
req.Credentials = CredentialCache .DefaultCredent ials;
req.KeepAlive = true;
req.CookieConta iner = new CookieContainer ();

Stream swOut = req.GetRequestS tream();
swOut.Write(bBu ffer,0,bBuffer. Length);

HttpWebResponse resp = (HttpWebRespons e)req.GetRespon se();
Stream sr = resp.GetRespons eStream();
string result = new StreamReader(sr ).ReadToEnd();

return result;

Jan 23 '07 #1
6 7813
Thus wrote ng******@yahoo. com,
I am trying to post form values to a https web page programmaticall y
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.Cre ate("https://online.texanscu .org/texans/
login.aspx");
req.Method = "POST";
req.ContentType = "applicatio n/x-www-form-urlencoded";
req.ContentLeng th = bBuffer.Length;
req.Credentials = CredentialCache .DefaultCredent ials;
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.CookieConta iner = 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********@joer gjooss.de
Jan 23 '07 #2
Joerg,

Thanks a lot for your reply. I understand that when I write
req.CookieConta iner = 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 programmaticall y
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.Cre ate("https://online.texanscu .org/texans/
login.aspx");
req.Method = "POST";
req.ContentType = "applicatio n/x-www-form-urlencoded";
req.ContentLeng th = bBuffer.Length;
req.Credentials = CredentialCache .DefaultCredent ials;

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.CookieConta iner = 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********@joer gjooss.de
Jan 23 '07 #3
Thus wrote ng******@yahoo. com,
Joerg,

Thanks a lot for your reply. I understand that when I write
req.CookieConta iner = 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.cookieCont ainer to HttpWebReqest.C ookieContainer
for each request you send.

Cheers,
--
Joerg Jooss
ne********@joer gjooss.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()
{
InitializeCompo nent();
}

private CookieContainer cookiejar = new CookieContainer ();

public string ScrapeURL(strin g URL)
{
string viewstate = HttpUtility.Url Encode(viewstat evalue);
StringBuilder data = new StringBuilder() ;
data.Append("VA M_Group=");
data.Append("&_ _VIEWSTATE=" + viewstate);
data.Append("&c tlSignon:txtUse rID=userid");
data.Append("&c tlSignon:txtPas sword=password" );
data.Append("&c tlSignon:ddlSig nonDestination= ");
data.Append("&c tlSignon:chkMak eDefaultPage=on ");
data.Append("&c tlSignon:btnLog in=Login");
data.Append("&T estJavaScript=O K");

byte[] bBuffer;

bBuffer = Encoding.UTF8.G etBytes(data.To String());

HttpWebRequest req =
(HttpWebRequest )WebRequest.Cre ate(URL);
req.Method = "POST";
req.ContentType = "applicatio n/x-www-form-urlencoded";
req.ContentLeng th = bBuffer.Length;
req.Credentials = CredentialCache .DefaultCredent ials;
req.KeepAlive = true;
req.CookieConta iner = this.cookiejar;

Stream swOut = req.GetRequestS tream();
swOut.Write(bBu ffer,0,bBuffer. Length);

HttpWebResponse resp = (HttpWebRespons e)req.GetRespon se();
Stream sr = resp.GetRespons eStream();
string result = new StreamReader(sr ).ReadToEnd();

return result;
}

private void button1_Click(o bject sender, EventArgs e)
{
textBox1.Text =
ScrapeURL("http s://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.CookieConta iner = 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.cookieCont ainer to HttpWebReqest.C ookieContainer
for each request you send.

Cheers,
--
Joerg Jooss
ne********@joer gjooss.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********@joer gjooss.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
8851
by: Aung | last post by:
Has anybody develop RFC1950 and RFC1951 compliant Zip utility? Any pointer will be appreciated.
2
2317
by: Gill Bates | last post by:
I'm trying to login to a banking site (https://www.providentconnection.com) using vb.net. I've tried many variations of WebClient and HttpWebRequest; none of which I've got to work. My latest version is: Dim myWebClient As New WebClient Dim nvc As New NameValueCollection nvc.Add("Login", username) nvc.Add("Password", password)
2
20096
by: dgiard | last post by:
I am using the System.Net.HttpWebRequest object to POST data to an HTTPS web page. Other than prefixing the URL with "HTTPS://", do I need to do anything in my code to indicate that this is SSL? Here is my code: // Create Web Request string url = "https://something.net/something.dll";
2
2268
by: Jonathan Wax | last post by:
Hi, I spent the last week looking for an answer to the following question: How can you upload an xml file to an HTTPS server with a specific certificate. Basically doing the same as this html form in c# code: <form method="post" enctype="multipart/form-data" action="https://www.site.com/upload/"/> <input type="file" name="xml" id="xml="><br>
0
4096
by: shlim | last post by:
Currently I'm using VB.Net to perform a http/https multipart form post to a servlet. I'm able to perform the post using HttpWebrequest via GetRequestStream(). However, the servlet returned me with "The remote server returned an error: (500) Internal Server Error". Obviously, this means that I have not posted all the parameters as the servlet requested. But I just can't seem to find out what went wrong with my code. Hope someone can enlighten me,...
3
1919
by: wzb6 | last post by:
Hi, 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. Imports System.IO
0
8715
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9193
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9092
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9037
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7963
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6641
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4734
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2531
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2116
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.