Establishing cookie based session with WebServices and HttpWebRequest

Published 24 August 04 04:18 PM | adarshk 

Create cookie based session with HttpWebRequest

One common requirement for Http based application to maintain the session state within the application, if your http based application is using the System.Net.HttpWebRequest class, then you could use Cookiecontainer property to send and recieved the cookies. Important thing to note that you should create a single CookieContainer instance and use it across all applications

Following example code snippet shows the sending and recieving the cookies from client application.

CookieContainer myContainer = new CookieContainer();

// following line will add a cookie in container, which would be for all urls on the domain myDomainstr

myContainer.Add(new Cookie("name","value","/",myDomainstr));

HttpWebRequest request1 = (HttpWebRequest) WebRequest.Create(httpUrlString);

request1.CookieContainer = myContainer; // use this same container for all requests in your app

HttpWebResponse response = (HttpWebResponse) request.GetResponse(); //you can check cookies on response.Cookies

..........

...........

HttpWebRequest request2 = (HttpWebRequest) WebRequest.Create(httpUrlString2);

request2.CookieContainer = myContainer; //all relevant cookies recieved on request1 would be automatically included in request because of same Cookiecontainer instnce.

HttpWebResponse response = (HttpWebResponse) request.GetResponse();

Create cookie based session with WebServices

If application want to use WebServices with cookie based session variables to execute service (ex. UserId of current ASP.Net session). This could be done via using the CookieContainer class and no need to add extra parameter inside the webservice call.

Following code demonstrate the setting of userID on the webservice from client side, In this example adarshk_srv.RunSoap is the WebService proxy object on client side

adarshk_srv.RunSoap RunService = new adarshk_srv.RunSoap();

RunService.CookieContainer = new System.Net.CookieContainer ();

RunService.CookieContainer.Add (new Uri(RunService.Url),new Cookie ("userName",”adarshk"););); 

On the server side WebService implementor could access the cookies send by the client from the HttpContext as shown below

if(Context.Request.Cookies != null && context.Request.Cookies["userName"] == "adarshk")

{

// access data specific to adarshk

}

else

{

// return the general data

}
  

This posting is provided "AS IS" with no warranties, and confers no rights

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# aaron said on August 8, 2006 3:26 PM:
good info but a complete example would help me better, there are some unanswered questions that i have.  should i send the cookie back to the server?  i see that the cookie is being stored but how does the server know that?  what is the servers interaction with the stored cookie.  server sends cookie to client and does nothing with it after that?
# tverskoy said on August 22, 2007 10:14 AM:

Good tip, Adarsh, thanks. It helps.

I am looking for a simple solution how can a client get cookies if the web service provides cookies only if "login" web method calling?

Direct hhtp-request/response to the server does not have cookies. Web method Login(..) works OK without cookies, but other methods do not work without cookies.

To call other methods on the web service I have to call login method first, then parse the header, then create the new CookieContainer  with the parsed values, and then call the other methods - that works ok.

My questions: is there another simple way to provide cookies to the web service in this situation?

# Andreas said on April 25, 2008 6:50 AM:

I have the following problem: I am creating web services using sessions with the cookie container. But now the problem; We have at least 3 servers. How can I find out or make sure that the following calls via web service find the server where I have my session??

# Ozan BAYRAM said on July 25, 2008 10:41 AM:

Andreas , you can use state server or store your session on the sql server

# vinodperoor said on August 12, 2008 3:33 AM:

thank you adarsh

this blog helped me a lot

i searched ,more than 2 days, to find it almost all sites having a a string like HttpWebRequest. but i didnt find anywhere that describes how can a client application keep session when using HttpWebRequest.

Thank you thank you very much

# 出会い said on June 12, 2009 12:34 AM:

ヒマだょ…誰かかまってぉ…会って遊んだりできる人募集!とりあえずメール下さい☆ uau-love@docomo.ne.jp

# 小向美奈子 said on June 13, 2009 12:37 AM:

話題の小向美奈子ストリップを隠し撮り!入念なボディチェックをすり抜けて超小型カメラで撮影した神動画がアップ中!期間限定配信の衝撃的映像を見逃すな

# 家出掲示板 said on June 14, 2009 12:18 AM:

カワイイ子ほど家出してみたくなるようです。家出掲示板でそのような子と出会ってみませんか?彼女たちは夕食をおごってあげるだけでお礼にHなご奉仕をしてくれちゃったりします

# 右脳左脳 said on June 15, 2009 12:33 AM:

あなたは右脳派?もしくは左脳派?隠されたあなたの性格分析が3分で出来ちゃう診断サイトの決定版!合コンや話のネタにも使える右脳左脳チェッカーを試してみよう

# セレブラブ said on June 15, 2009 11:48 PM:

セレブラブでは性欲のある男性を募集しています。セフレパートナーを探している20代・30代の女性たちが多数登録されています。セレブと遊びたい、Hがしたいという方は無料登録からどうぞ

# 救援部 said on June 17, 2009 11:40 PM:

貴方のオ○ニーライフのお手伝い、救援部でHな見せたがり女性からエロ写メ、ムービーをゲットしよう!近所の女の子なら実際に合ってHな事ができちゃうかも!?夏に向けて開放的になっている女の子と遊んじゃおう

# 出会い said on June 19, 2009 12:34 AM:

まったぁ〜りしたデートがしたいです☆結構いつでもヒマしてます♪ m-g-j@docomo.ne.jp 年齢と名前くらいは入れてくれるとメール返信しやすいかも…

# 逆援交際 said on June 19, 2009 11:08 PM:

会員制の逆援交際倶楽部では男性は無料、一日最低額10万円保障での交際をお求めできます。ご登録された女性様達はセレブであるがための悩みをそれぞれの方が持ち、皆様、男性との営みを求め、ご登録されております。彼女たちとの初々しい一時をお楽しみ、謝礼をいただいてくださいませ。会員制ですので人数に限りがあるため、打ち切りの場合はご了承ください

# 家出 said on June 20, 2009 11:10 PM:

家出中でネットカフェやマンガ喫茶にいる女の子たちは、お金が無くなり家出掲示板で今晩泊めてくれる男性を探しています。ご飯を食べさせてあげたり泊めてあげることで彼女たちはHなお礼をしてくれる事が多いようです

# 勝ち組負け組 said on June 21, 2009 11:04 PM:

当サイトは、みんなの「勝ち組負け組度」をチェックする性格診断のサイトです。ホントのあなたをズバリ分析しちゃいます!勝ち組負け組度には、期待以上の意外な結果があるかもしれません

# 素人 said on June 22, 2009 10:53 PM:

男性が主役の素人ホストでは、男性のテクニック次第で女性会員様から高額な謝礼がもらえます。欲求不満な人妻や、男性と出会いが無い女性が当サイトで男性を求めていらっしゃいます。興味のある方はTOPページからどうぞ

# エロ漫画 said on June 23, 2009 10:42 PM:

エロ漫画やエロゲーなどでかわいい女の子が淫らな肉欲に溺れる様子をみて「こんなの現実にあるわけない」そう思った事ありませんか?それが当サイトでは現実に実現できるのです!羨ましさを憶えた2次元の中での出来事。あなたと同じように望む女の子が当サイトに集まっているのです

# 高級チェリー said on June 24, 2009 10:38 PM:

高級チェリーの夏は童貞卒業の夏です。セレブ達も童貞を卒業させたくてウズウズしながら貴方との出会いを待っています。そんなセレブ達に童貞を捧げ、貴方もハッピーライフを送ってみませんか

# 助けて〜! said on June 25, 2009 11:11 PM:

何回かメールして会える人一緒に楽しいことしょ?お給料もらったばかりだからご飯くらいならごちそうしちゃうょ♪ cha-a@docomo.ne.jp とりあえずメールくださぃ★

# セレブラブ said on June 26, 2009 10:52 PM:

セレブラブではココロとカラダに癒しを求めるセレブ達と会って頂ける男性を募集しています。セレブ女性が集まる当サイトではリッチな彼女たちからの謝礼を保証、安心して男性はお金、女性は体の欲求を満たしていただけます。無料登録は当サイトトップページからどうぞ

# SOS少女 said on June 27, 2009 11:25 PM:

家出中でお金が無く、ネットカフェを泊り歩いているSOS少女たちは、家出掲示板で泊めてくれたり遊んでくれる男性を探しています。泊めてあげたりすると彼女たちはHなお礼をしてくれるかもしれません。家出少女と遊びたい方は当サイトはどうぞ

# 出張ホスト said on June 29, 2009 11:35 PM:

マダムと甘い時間を過ごしてみませんか?性欲を持て余しているセレブたちは出張ホストサービスで男性を探し、セックスを求めているのです。ホスト希望の方なら容姿や年齢は一切不問!ご近所の女性を探して、多額の報酬をゲットしよう

# スローセックス said on June 30, 2009 10:58 PM:

楽しく、気持ちよく絶頂を味わえることで若い女性から熟女の女性まで幅広い世代で爆発的な人気がある、スローセックス。当サイトはプレイに興味がある、あるいは試してみたいけれど相手がいない…といった方の支援サイトです。当サイトでSEXパートナーを探してみませんか

# 一人エッチ said on July 1, 2009 10:50 PM:

夏真っ盛り!女の子は開放的な気分で一人エッチしたくてウズウズしてるっ!!貴方は女の子のオナ○ーを見て気分を高めてあげてネ!!もちろん、お手伝いしてもオッケーだよ!!さぁ、今すぐ女の子にアクセスしよっ

# メル友募集 said on July 2, 2009 11:40 PM:

恋することって怖くないですか?最近ちょっと臆病になってて…そういうの抜きでえっちなことしたくて… lovely-i0709@docomo.ne.jp優しい人がいたらメール待ってます☆

# 逆円助 said on July 4, 2009 12:08 AM:

さあ、今夏も新たな出会いを経験してみませんか?当サイトは円助交際の逆、つまり女性が男性を円助する『逆円助交際』を提供します。逆円交際を未経験の方でも気軽に遊べる大人のマッチングシステムです。年齢上限・容姿・経験一切問いません。男性の方は無料で登録して頂けます。貴方も新たな出会いを経験してみませんか

# 家出 said on July 4, 2009 11:28 PM:

これから家出したい少女や、現在家出中の娘とそんな娘を助けたい人を繋げるSOS掲示板です。10代、20代の女の子が家庭内の問題などでやむなく家出している子が多数書き込みしています。女の子リストを見て彼女たちにアプローチしてみませんか

# 精神年齢 said on July 5, 2009 11:58 PM:

みんなの精神年齢を測定できる、メンタル年齢チェッカーで秘められた年齢がズバリわかっちゃう!かわいいあの子も実は精神年齢オバサンということも…合コンや話のネタに一度チャレンジしてみよう

# 童貞卒業 said on July 6, 2009 11:34 PM:

童貞卒業を考えているなら、迷わずココ!今まで童貞とヤッた事がない女性というのは意外と多いものです。そんな彼女たちは一度童貞とやってみたいと考えるのは自然な事と言えるでしょう。当サイトにはそんな好奇心旺盛な女性たちが登録されています

# 素人 said on July 7, 2009 10:30 PM:

素人ホストでは日頃のストレスを発散したい、もう一度恋がしたい、そういた女性が癒しを求めて登録されています。当サイトは癒やされたい女性・寂しい女性を癒やす男性が集うカップリングサイトです

# 熟女 said on July 8, 2009 11:23 PM:

熟女だって性欲がある、貴方がもし人妻とSEXしてお金を稼ぎたいのなら、一度人妻ワイフをご利用ください。当サイトには全国各地からお金持ちのセレブたちが集まっています。女性から男性への報酬は、 最低15万円からと決めております。興味のある方は一度当サイト案内をご覧ください

Leave a Comment

(required) 
(optional)
(required) 

Search

Go

This Blog

Syndication

Page view tracker