473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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 7792
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Aung | last post by:
Has anybody develop RFC1950 and RFC1951 compliant Zip utility? Any pointer will be appreciated.
2
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...
2
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?...
2
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...
0
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...
3
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
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...
0
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...
0
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,...

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.