Get session cookies from webrequest and pass then to new request

T

ThatsIT.net.au

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?
 
H

Hans Kesting

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
 
A

Arne Vajhøj

ThatsIT.net.au said:
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
 
T

ThatsIT.net.au

Sorry I did not get back earlier,

thanks very much ill try it



Arne Vajhøj said:
ThatsIT.net.au said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top