By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,899 Members | 1,357 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,899 IT Pros & Developers. It's quick & easy.

Get session cookies from webrequest and pass then to new request

P: n/a
I am making a console app that requests pages from our site one after
another. Each request starts a new session, What I want to do is make all
requests in the same session.

How can I do this.

I was hoping to be able to get the session back from first request and then
set them on each further request.

Any ideas?

Jul 3 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
ThatsIT.net.au used his keyboard to write :
I am making a console app that requests pages from our site one after
another. Each request starts a new session, What I want to do is make all
requests in the same session.

How can I do this.

I was hoping to be able to get the session back from first request and then
set them on each further request.

Any ideas?
There is a CookieContainer specifically for this scenario:
create one and add it to every request you issue.

Hans Kesting
Jul 3 '08 #2

P: n/a
ThatsIT.net.au wrote:
I am making a console app that requests pages from our site one after
another. Each request starts a new session, What I want to do is make
all requests in the same session.

How can I do this.

I was hoping to be able to get the session back from first request and
then set them on each further request.
Example of using CookieContainer:

using System;
using System.IO;
using System.Net;

namespace E
{
public class MainClass
{
public static string GetContent(string url, CookieContainer
session)
{
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.CookieContainer = session;
string html = (new
StreamReader(wr.GetResponse().GetResponseStream()) ).ReadToEnd();
return html;
}
public static void Main(string[] args)
{
CookieContainer session = new CookieContainer();
string login =
GetContent("http://localhost:8080/logintest/login.jsp?username=arne&password=hemmeligt",
session);
Console.WriteLine(login);
string other =
GetContent("http://localhost:8080/logintest/other.jsp", session);
Console.WriteLine(other);
}
}
}

Arne
Jul 3 '08 #3

P: n/a
Sorry I did not get back earlier,

thanks very much ill try it

"Arne Vajhøj" <ar**@vajhoej.dkwrote in message
news:48***********************@news.sunsite.dk...
ThatsIT.net.au wrote:
>I am making a console app that requests pages from our site one after
another. Each request starts a new session, What I want to do is make all
requests in the same session.

How can I do this.

I was hoping to be able to get the session back from first request and
then set them on each further request.

Example of using CookieContainer:

using System;
using System.IO;
using System.Net;

namespace E
{
public class MainClass
{
public static string GetContent(string url, CookieContainer
session)
{
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.CookieContainer = session;
string html = (new
StreamReader(wr.GetResponse().GetResponseStream()) ).ReadToEnd();
return html;
}
public static void Main(string[] args)
{
CookieContainer session = new CookieContainer();
string login =
GetContent("http://localhost:8080/logintest/login.jsp?username=arne&password=hemmeligt",
session);
Console.WriteLine(login);
string other =
GetContent("http://localhost:8080/logintest/other.jsp", session);
Console.WriteLine(other);
}
}
}

Arne
Jul 13 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.