473,395 Members | 2,446 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,395 software developers and data experts.

Using HttpWebRequest to post form data with cookies

I'm working on a web scraping application that needs to log into a website
before it can get the data to scrape. I've always been confused about how
the HttpWebRequest and HttpWebResponse objects work together with cookies,
and was hoping that someone here could clear it up for me!

Here's the steps I need to accomplish:

1) Set two cookies containing information I already have
2) Request the login page URL, and save a third cookie that is given to me
from that page
3) Submit a form via POST, along with the 3 cookies
4) Save a fourth cookie that is set after the form is submitted with valid
information

I don't need the entire code to do this, but rather some help with the order
to do it. Do I create a request and THEN set the cookies? Can I use the
same CookieContainer for each request? If someone could point me in the
right direction it would be greatly appreciated!
Nov 19 '05 #1
2 6940
Luis Esteban Valencia Muñoz wrote:
I'm working on a web scraping application that needs to log into a
website before it can get the data to scrape. I've always been
confused about how the HttpWebRequest and HttpWebResponse objects
work together with cookies, and was hoping that someone here could
clear it up for me!

Here's the steps I need to accomplish:

1) Set two cookies containing information I already have
2) Request the login page URL, and save a third cookie that is given
to me from that page
3) Submit a form via POST, along with the 3 cookies
4) Save a fourth cookie that is set after the form is submitted with
valid information

I don't need the entire code to do this, but rather some help with
the order to do it. Do I create a request and THEN set the cookies?
Can I use the same CookieContainer for each request? If someone
could point me in the right direction it would be greatly appreciated!


This topic has been beaten to death by now -- just search for
WebRequest and CookieContainer on Google: http://tinyurl.com/9yp6r

Just make sure to use one CookieContainer instance throughout your web
conversation. It will pick up all cookies, and you don't need to copy
cookies from responses back to it -- the CookieContainer picks them up
automatically.

You can set any request header (including cookies) as long as you
haven't written to the request stream or read from the response stream
yet.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 19 '05 #2
This example might be helpful:
http://msdn.microsoft.com/library/de...ainerTopic.asp

The key is that you need to create and assign a cookiecontainer object to
the request object's CookieContainer property first. Then and only then
will you receive the response cookies.
--Buddy
"Luis Esteban Valencia Muñoz" <le********@avansoft.com> wrote in message
news:OM**************@TK2MSFTNGP14.phx.gbl...
I'm working on a web scraping application that needs to log into a website
before it can get the data to scrape. I've always been confused about how
the HttpWebRequest and HttpWebResponse objects work together with cookies,
and was hoping that someone here could clear it up for me!

Here's the steps I need to accomplish:

1) Set two cookies containing information I already have
2) Request the login page URL, and save a third cookie that is given to me
from that page
3) Submit a form via POST, along with the 3 cookies
4) Save a fourth cookie that is set after the form is submitted with valid
information

I don't need the entire code to do this, but rather some help with the order to do it. Do I create a request and THEN set the cookies? Can I use the
same CookieContainer for each request? If someone could point me in the
right direction it would be greatly appreciated!

Nov 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: TJO | last post by:
Can someone at MS please reply to this. I am trying to post data so a web form via ssl with the following code. I keep getting this error: "The underlying connection was closed: Could not...
10
by: Gregory A Greenman | last post by:
I'm trying to write a program in vb.net to automate filling out a series of forms on a website. There are three forms I need to fill out in sequence. The first one is urlencoded. My program is...
1
by: Vasu | last post by:
Hi, I have a requirement to download a file from the web site using a client tool. Iam writing a C# program to download using WebRequest, HttpRequest, WebResponse and so on. The problem...
2
by: Keith Patrick | last post by:
I'm trying to programmatically post data to another page within my ASP.Net app. Not POSTing is not an option (I can't store this data in my session, context, hidden fields, or anything else...I've...
7
by: Mark Waser | last post by:
Hi all, I'm trying to post multipart/form-data to a web page but seem to have run into a wall. I'm familiar with RFC 1867 and have done this before (with AOLServer and Tcl) but just can't seem...
8
by: shankararaman.s | last post by:
Hi, I am trying to develop an interface which will fetch all my Yahoo mails. I am not able to sign in to yahoo by posting the form with my username & password. Please find my code below and...
1
by: branden.hughes | last post by:
http://www.dcor.state.ga.us/GDC/OffenderQuery/jsp/OffQryForm.jsp?Institution=vDisclaimer=True Every time I try it just kicks back the same HTML you'd find from the link above rather than a page...
2
by: adwooley2 | last post by:
Hello. Have been losing plenty of hair over problem whereby I can't make it off the login page. Trying to pass login info to a login page and then move on to another page within the site so that...
0
by: barrybevel | last post by:
Hi, I'm trying to login to the www.vodafone.ie website using HttpWebRequest. It works fine with IE/Firefox and the .NET Web Control too, just not with my code. I think it's a redirect 302...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
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
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.