2

I'm trying to POST a cookie on my .net client api request to a remote server.

Using Fiddler, I can see the cookie I need looks like this:

Request sent 265 bytes of Cookie data:

cookiesDirective=1; __utmt=1; currentLanguage=EN; 
__utma=198151921.81872271.1398032350.1423043623.1423075781.3; 
__utmb=198151921.3.10.1423075781; 
__utmc=198151921; 
__utmz=198151921.1423043623.2.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)

However when I POST my api call, my cookie container is empty (according to Fiddler)

My code is as follows:

static async Task RunAsync()
{

    string url = "http://www.myserver.com/Services/GetData";

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:9000/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var model = new Payload() { pageNumber = 2 };

        var cookie = new CookieHeaderValue("session-id", "12345");
        cookie.Expires = DateTimeOffset.Now.AddDays(1);
        cookie.Path = "/";

        HttpResponseMessage response = new HttpResponseMessage();

        response.Headers.AddCookies(new CookieHeaderValue[] { cookie });

        response = await client.PostAsJsonAsync(url, model);
        if (response.IsSuccessStatusCode)
        {
             // Testing
        }

    }
}
DROP TABLE users
  • 1,955
  • 14
  • 26
Evonet
  • 3,600
  • 4
  • 37
  • 83
  • 2
    http://stackoverflow.com/questions/12373738/how-do-i-set-a-cookie-on-httpclients-httprequestmessage – mikey Feb 04 '15 at 19:37
  • also consider using RestSharp (http://restsharp.org/) when communicating with a REST service.. – slvnperron Feb 04 '15 at 19:45
  • 3
    Settings cookies on the *response* object isn't going to do you much good, that's the object that manages what the server sends to you. You need to set the cookies on the thing that actually sends the *request*, which in your code would be the `client`. See the link that @mikey provided for an explanation on how to do that. – Craig W. Feb 04 '15 at 19:59

1 Answers1

4

Thanks to @mikey I got this working with the following:

    string url = "http://www.myserver.com/Services/GetData";

    var baseAddress = new Uri(url);
    using (var handler = new HttpClientHandler { UseCookies = false })
    using (var client = new HttpClient(handler) { BaseAddress = baseAddress })
    {
        var message = new HttpRequestMessage(HttpMethod.Post, "/Services/ContentRetrievalService.svc/GetSpecAmountOfFilteredPepole");
        message.Headers.Add("Cookie", "cookiesDirective=1; __utmt=1; currentLanguage=EN; __utma=198151921.81872271.1398032350.1423043623.1423075781.3; __utmb=198151921.3.10.1423075781; __utmc=198151921; __utmz=198151921.1423043623.2.1.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided)");

        message.Headers.Add("Connection", "keep-alive");
        message.Headers.Add("X-Requested-With", "XMLHttpRequest");
        message.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36");
        message.Headers.Add("Accept-Encoding", "gzip, deflate");

        var result = await client.SendAsync(message);
        result.EnsureSuccessStatusCode();
    }
Evonet
  • 3,600
  • 4
  • 37
  • 83