473,805 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using WebRequest with cookie

I am trying to download a web page using WebRequest.
The problem is that this page uses cookies. In IE, I can view this page
correctly (since I have the cookie) but when downloading using WebRequest I
don't get the page I want.
How can I add cookie information to my WebRequest ?
How do I know which cookie information the page need ?
A code example will be great.

Here is my code:
Dim szDate As String = ""
Dim NetDataRequest As System.Net.WebR equest =
System.Net.WebR equest.Create(s zUrl)
Dim NetDataResponse As System.Net.WebR esponse = NetDataRequest. GetResponse()
Dim StreamRead As System.IO.Strea mReader
StreamRead = New System.IO.Strea mReader(NetData Response.GetRes ponseStream(),
System.Text.Enc oding.ASCII)
szDate = StreamRead.Read ToEnd()
NetDataResponse .Close()
StreamRead.Clos e()
Return szDate

Thanks for your time

ra***@hotmail.c om
Nov 21 '05 #1
5 2947
"Ya Ya" <ra***@hotmail. com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
I am trying to download a web page using WebRequest.
The problem is that this page uses cookies. In IE, I can view this page
correctly (since I have the cookie) but when downloading using WebRequest I don't get the page I want.
How can I add cookie information to my WebRequest ?
How do I know which cookie information the page need ?
A code example will be great.

Here is my code:
Dim szDate As String = ""
Dim NetDataRequest As System.Net.WebR equest =
System.Net.WebR equest.Create(s zUrl)
Dim NetDataResponse As System.Net.WebR esponse = NetDataRequest. GetResponse() Dim StreamRead As System.IO.Strea mReader
StreamRead = New System.IO.Strea mReader(NetData Response.GetRes ponseStream(), System.Text.Enc oding.ASCII)
szDate = StreamRead.Read ToEnd()
NetDataResponse .Close()
StreamRead.Clos e()
Return szDate

Thanks for your time

ra***@hotmail.c om


Your cookie collection will be populated initially after you log in (or
select your settings). You must then be sure to hand that cookie (or cookie
collection) back with each request.
Nov 21 '05 #2
You have to use a CookieContainer

--
Sriram Krishnan

http://www.dotnetjunkies.com/weblog/sriram
"Chris Hornberger" <chris@no_spam. chornbe.com> wrote in message
news:M5******** ************@co mcast.com...
"Ya Ya" <ra***@hotmail. com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
I am trying to download a web page using WebRequest.
The problem is that this page uses cookies. In IE, I can view this page
correctly (since I have the cookie) but when downloading using WebRequest

I
don't get the page I want.
How can I add cookie information to my WebRequest ?
How do I know which cookie information the page need ?
A code example will be great.

Here is my code:
Dim szDate As String = ""
Dim NetDataRequest As System.Net.WebR equest =
System.Net.WebR equest.Create(s zUrl)
Dim NetDataResponse As System.Net.WebR esponse =

NetDataRequest. GetResponse()
Dim StreamRead As System.IO.Strea mReader
StreamRead = New

System.IO.Strea mReader(NetData Response.GetRes ponseStream(),
System.Text.Enc oding.ASCII)
szDate = StreamRead.Read ToEnd()
NetDataResponse .Close()
StreamRead.Clos e()
Return szDate

Thanks for your time

ra***@hotmail.c om


Your cookie collection will be populated initially after you log in (or
select your settings). You must then be sure to hand that cookie (or
cookie
collection) back with each request.

Nov 21 '05 #3
Do you mean I need to do webRequest to the login page first (posting the
user/pass as parameters) , and then pass the CookieContainer to another
webRequest for the page I need ?

Thanks
ra***@hotmail.c om

"Chris Hornberger" <chris@no_spam. chornbe.com> wrote in message
news:M5******** ************@co mcast.com...
"Ya Ya" <ra***@hotmail. com> wrote in message
news:%2******** ********@TK2MSF TNGP15.phx.gbl. ..
I am trying to download a web page using WebRequest.
The problem is that this page uses cookies. In IE, I can view this page
correctly (since I have the cookie) but when downloading using WebRequest

I
don't get the page I want.
How can I add cookie information to my WebRequest ?
How do I know which cookie information the page need ?
A code example will be great.

Here is my code:
Dim szDate As String = ""
Dim NetDataRequest As System.Net.WebR equest =
System.Net.WebR equest.Create(s zUrl)
Dim NetDataResponse As System.Net.WebR esponse =

NetDataRequest. GetResponse()
Dim StreamRead As System.IO.Strea mReader
StreamRead = New

System.IO.Strea mReader(NetData Response.GetRes ponseStream(),
System.Text.Enc oding.ASCII)
szDate = StreamRead.Read ToEnd()
NetDataResponse .Close()
StreamRead.Clos e()
Return szDate

Thanks for your time

ra***@hotmail.c om


Your cookie collection will be populated initially after you log in (or
select your settings). You must then be sure to hand that cookie (or
cookie
collection) back with each request.

Nov 21 '05 #4
"Ya Ya" <ra***@hotmail. com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Do you mean I need to do webRequest to the login page first (posting the
user/pass as parameters) , and then pass the CookieContainer to another
webRequest for the page I need ?

yessir.

Let me see if I can find a snippet of a production app we're using at the
office for you to see....
Nov 21 '05 #5
This appears to be exactly what I'm looking for. Was a code example ever
published for this?

"Chris Hornberger" wrote:
"Ya Ya" <ra***@hotmail. com> wrote in message
news:%2******** ********@TK2MSF TNGP10.phx.gbl. ..
Do you mean I need to do webRequest to the login page first (posting the
user/pass as parameters) , and then pass the CookieContainer to another
webRequest for the page I need ?

yessir.

Let me see if I can find a snippet of a production app we're using at the
office for you to see....

Nov 21 '05 #6

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

Similar topics

0
1197
by: | last post by:
Hello, I can use WebRequest when accessing web sites that don't require a cookie. However, when I want to use it to get an html page from a web site that I have a current cookie for, I an unable to get things to work. I don't know how to point to the existing cookie. Any help would be greatly appreciated.
1
4206
by: Peter | last post by:
Hi I am trying to do very simple http client that requires a login, copy cookie snet by a server and does a request with the cookies. WebRequest->CookieContainer/WebResponse->Cookies does not seems to handle cookie correctly. User case: If a server send back cookies in headers that looks like following Set-Cookie: a=assss Set-Cookie: b=asddas ...
1
5904
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 Iam having is to navigate thru multiple pages. I have to login
17
7243
by: James Johnson | last post by:
Dear C#dex, I define a variable: HttpWebRequest webRequest and run the following request webRequest = WebRequest.Create(TARGET_URL) as HttpWebRequest; The webRequest object returns values and in the debugger I can see the value I want in the property
0
1328
by: Tony Archer | last post by:
Here's my problem... When I use this code to hit the site in question I'm redirected to another SSL login page. If processed successfully the page creates a cookie and then redirects you to the original request. However, my code cannot seem to hold onto the cookie it creates. Does anyone know how to do this?
6
1717
by: matt | last post by:
hello, i am having trouble doing something. when a user triggers a certain event in my app, i need to initiate another web request to one of my other webpages, programmatically. currently, i do this via a WebRequest -- i attach the default credentials and get the response stream. that part works. like so: WebRequest request = HttpWebRequest.Create(reportAspxURL); request.Credentials = CredentialCache.DefaultCredentials;
1
4418
by: WeCi2i | last post by:
Okay, I have a problem that has been stumping me for weeks. I have tried many different solutions and this is pretty much my last resort. I have seen a lot of good answers give here so I figured I would give it a try. First of all, I am using Visual Studio 2005 to write my program. I am using C# .NET as the language. I am running Windows XP Professional with all service packs and updates applied. Now, I have been trying to write a...
3
8189
by: Dave | last post by:
string m_request = some_web_page; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(m_request ); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Which works fine, but I need to set and send a cookie with the WebRequest. How do I do that?
1
18662
by: dschu012 | last post by:
I am logging into a site using a WebBrowser. Once I log in I want to save an Image from within it without having to make the WebBrowser navigate to the URL where the Image is stored. Currently I am doing this HttpWebRequest request = (HttpWebRequest)WebRequest.Create(myUri); request.Headers.Add(HttpRequestHeader.Cookie, webBrowser1.Document.Cookie); WebResponse response = request.GetResponse(); Image captchaImage =...
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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
10607
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10359
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...
0
10104
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...
1
7645
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
5541
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.