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

Home Posts Topics Members FAQ

WebRequest/WebResponse and Cookie

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->CookieContaine r/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
...

The WebResponse->Cookies only have one value from one Set-Cookie headers. Yes, you can still see all the values in all Set-Cookie in WebRespnose->Headers

Do I miss something

Pete

Jul 21 '05 #1
1 4206
You need a cookie container to accept/pass the cookies. Take a look at the
following snips. You may not need all of this but it should give you the
idea.

mgWebRequest =CType(WebReque st.Create(strWe bSite &
strWebPage),Htt pWebRequest)
mgWebRequest.ti meout = 90000
mgWebRequest.Co okieContainer = new cookiecontainer ()
mgWebRequest.re ferer = "whaterreff erer you want"
dim k as int32
Dim siteUri As New Uri(strWebSite)
'this add the current cookies to the container if you need to
for k = 0 to HttpContext.Cur rent.Request.Co okies.count - 1
dim ck as cookie = new cookie()
ck.name = HttpContext.Cur rent.Request.Co okies(k).name
ck.value = HttpContext.Cur rent.Request.Co okies(k).value
ck.domain = strWebSite.subs tring(strWebSit e.indexof(".") + 1)
ck.expires = datetime.now.ad dyears(50)
mgWebRequest.Co okieContainer.a dd(siteURI,ck)
next k
After you get the response you can use the following to add the receiving
cookies to your cookies

mgWebResponse = CType(mgWebRequ est.GetResponse (),HttpWebRespo nse)

mgWebResponse.c ookies =
mgWebrequest.Co okieContainer.G etCookies(mgWeb request.Request Uri)

mgStreamReader = New StreamReader(mg WebResponse.Get ResponseStream( ))
strText = mgStreamReader. ReadToEnd()
dim i as int32

for i = 0 to mgWebResponse.c ookies.count - 1

dim hc as httpcookie
hc = new httpcookie(mgWe bResponse.cooki es.item(i).name )
hc.value = mgWebResponse.c ookies.item(i). value
'hc.domain = "if you set this the whole thing will fail. Cannot
setdomain."
hc.expires = datetime.now.ad dyears(50)
hc.path = mgWebResponse.c ookies.item(i). path
HttpContext.Cur rent.Response.A ppendCookie(hc)
next i
mgStreamReader. Close()
"Peter" <an*******@disc ussions.microso ft.com> wrote in message
news:BA******** *************** ***********@mic rosoft.com...
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->CookieContaine r/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=assssa
Set-Cookie: b=asddasf
...

The WebResponse->Cookies only have one value from one Set-Cookie headers. Yes, you can still see all the values in all Set-Cookie in
WebRespnose->Headers.
Do I miss something?

Peter

Jul 21 '05 #2

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

Similar topics

2
1864
by: Ziphyre | last post by:
Hi, I'm quite new in C# so it can be a silly question, but i can't figure how to make a req/resp and a second req with first response's informations... What I mean: I have no problem in sending a request to a web server, and getting response from it, reading headers, cookies, body etc... but how can i make a seconde request using the first communication with HttpWebRequest and HttpWebResponse.
2
9709
by: gizmo | last post by:
Hi, I'm using the following code to request the html source from the quoted site. ...... string url = "http://www1.soccerstand.com/"; WebRequest webRequest = WebRequest.Create(url); WebResponse webResponse = webRequest.GetResponse(); beginStr = new StreamReader(webResponse.GetResponseStream(),
1
2367
by: John | last post by:
I am in need to find a way to get the HTML from an internal URL. For example: I have page : default.aspx?page=Test, I need to get HTML of that page after rendering. At first thought, I would use WebRequest to make a requestion, then use WebResponse to get the HTML. However, it won't work in my situation. The resaon for that is: WebRequestion needs a URI, which is in the format of http://<MyDomainName>/deafult.aspx?page=Test ... In my...
5
2948
by: Ya Ya | last post by:
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 = ""
1
387
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
2481
by: Richard K Bethell | last post by:
From what I am reading, a System.Net.WebRequest can be set to retrieve just parts of a file in the WebResponse, using the .AddRange method to specify a byte range. This would be very useful for me for a client that I want to build which will download files in a small footprint. However, what I'm missing (probably just overlooking) is how to use the WebRequest or WebResponse to query a file's size, so that you know what your maximum range...
0
1088
by: Naga | last post by:
I am using the code below to login to a password requiring website programatically. I keep getting back the logon page. I have read some posts here and haven't found a working solution still. I don't need any proxy to connect. After seeing some posts I eeven added a cookie container, still doesn't work.I'd really appreciate a lot, if someone could tell me what additional code is required to make this work Thanks in advance, Naga...
4
2084
by: CindyH | last post by:
Hi I'm trying to use webrequest - webresponse to post a stream. I have set up a simple test with one aspx form holding the post code and trying to get another aspx form to receive the post on localhost. Can you explain to me the possible reasons why a 'GetResponse' - to send the data to a server might not be working for a simple text file with one line. test.text - contains: this is a test.
4
2132
by: CindyH | last post by:
Hi - hope someone can help with this - this code was working for a while in the 'real' code and then suddenly stopped - not sure what happen. I made two simple forms on localhost to try to test what is going on. I'm not getting any errors right now, but code is not working either - not reading the post in second form or else the first form is not sending it correctly....
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
10609
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...
1
10366
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
10105
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
7646
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
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4323
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3007
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.