tags:

views:

39

answers:

2

I'm trying to call a web service from a c# application, with sessionID.
In order to do this I need to set the "Domain" header in a cookie.

In Fiddler it looks like - "ASP.NET_SessionId=izdtd4tbzczsa3nlt5ujrbf5" (no domain is specified in the cookie).

The web service is at - "http://[some ip goes here]:8989/MyAPI.asmx".

I've tried:
http://[ip] ,
http://[ip]:8989 ,
http://[ip]:8989/MyAPI.asmx

All of these cause runtime error.

I've also tried the ip alone (i.e. 100.10.10.10) , which doesn't cause a runtime error, and sets the cookie, but the cookie is never sent when I invoke a web method.

Here's my code for setting the domain:

if (!string.IsNullOrEmpty(currentSessionID))
{
   req.CookieContainer=new CookieContainer();
   Cookie cookie = new Cookie("ASP.NET_SessionId", currentSessionID);
   cookie.Domain = GetCookieUrl();  //<- What should this be?
   req.CookieContainer.Add(cookie);
}

So what should the domain be?

Thanks.

A: 

I believe it should simply be [ip]. Drop the http:// part of what you've tried.

According to this page on MSDN, your code should be

cookie.Domain = "100.10.10.10";

Next, exactly what error are you getting? Also, are you confusing a Compile error with a Runtime error? I find it hard to believe you are getting a compilation error as Domain is a String property which means you can put pretty much anything into it.

Finally, why are you sending a cookie to a web service? The normal way is to pass everything in the form post or on the query string.


Update

BTW, if you absolutely must add a cookie to the header in order to pass it to a web service, the way you do this is (taken from here):

byte[] buffer = Encoding.ASCII.GetBytes("fareId=123456"); //the data you want to send to the web service
HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(url);
WebReq.Method = "POST";
WebReq.ContentType = "application/x-www-form-urlencoded";
WebReq.ContentLength = buffer.Length;
WebReq.Headers["Cookie"] = "ASP.NET_SessionId=izdtd4tbzczsa3nlt5ujrbf5"

Stream PostData = WebReq.GetRequestStream();

Note that this sets the header inline with the request without instantiating a "cookie" object. The Domain property of a cookie is to help ensure the cookie is only sent to the domain listed. However, if you are initiating the request and trying to append a cookie to it, then the best way is to just add it as a string to the request headers.

Chris Lively
I've tried ip alone (as I wrote in the question), the cookie is then not sent when I invoke a web-service.
Oren A
@Oren A: see update.
Chris Lively
@Chris: you're right about the "time", Cookie is .NET's cookie (should I try httpCookie instead?), when I send a request with only the ip, it's not sent when I invoke the request (I know that from Fiddler). As to why sending a cookie, I'm quite sure that's what the browser does (again, Fiddler)
Oren A
A: 

The reason the cookie was not sent is that the request's content length should be set after adding the cookie, and not before.
The domain is the ip alone.

Oren A