0

I have to send the cookies to server for every subsequent HTTPWebRequest. My code goes below.

class APIManager
{

CookieContainer cookieJar = new CookieContainer();
CookieCollection responseCookies = new CookieCollection();

private async Task<string> httpRequest(HttpWebRequest request)
{
    string received;

    using (var response = (HttpWebResponse)(await Task<WebResponse>.Factory
        .FromAsync(request.BeginGetResponse, request.EndGetResponse, null)))
    {
        using (var responseStream = response.GetResponseStream())
        {
            using (var sr = new StreamReader(responseStream))
            {
                cookieJar = request.CookieContainer;
                responseCookies = response.Cookies;
                received = await sr.ReadToEndAsync();
            }
        }
    }

    return received;
}

public async Task<string> Get(string path)
{
    var request = WebRequest.Create(new Uri(path)) as HttpWebRequest;
    request.CookieContainer = cookieJar;

    return await httpRequest(request);
}

public async Task<string> Post(string path, string postdata)
{
    var request = WebRequest.Create(new Uri(path)) as HttpWebRequest;
    request.Method = "POST";
    request.CookieContainer = cookieJar;

    byte[] data = Encoding.UTF8.GetBytes(postdata);
    using (var requestStream = await Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, null))
    {
        await requestStream.WriteAsync(data, 0, data.Length);
    }

    return await httpRequest(request);
}

}

Every time i ask for the question people say that i have to set the cookie container with request by following code line.

  request.CookieContainer = cookieJar;

and i used it but still server returns the 'token does not match' error. Do i need to talk to the vendor for it?

Following image shows my problem and requirement.

enter image description here

LojiSmith
  • 282
  • 3
  • 20

1 Answers1

0

I haven't seen you do something with the cookieJar !

//Create the cookie container and add a cookie.
request.CookieContainer = new CookieContainer();

// This example shows manually adding a cookie, but you would most
// likely read the cookies from isolated storage.
request.CookieContainer.Add(new Uri("http://api.search.live.net"),
new Cookie("id", "1234"));

cookieJar in your APIManager is a member, everytime your instance APIManager, the cookieJar is a new instance. you need to make sure cookieJar contains what the website needs.

you can have a look at this How to: Get and Set Cookies

AlexisXu
  • 126
  • 3
  • you got an eagle eye, in fact the code is older one. I changed cookiejar to static and verified that it had count 3 for the next call. Please let me know if making it static will work or i should persist the domain-ii (the cookie vendor told to persist) and then send it every time? – LojiSmith Jan 23 '14 at 07:13
  • current code is private static CookieContainer cookieJar = new CookieContainer(); – LojiSmith Jan 23 '14 at 07:14
  • static member still can be shared since static member belongs to the Class not the instance. – AlexisXu Jan 23 '14 at 07:47
  • @LojiSmithKaleem you can try request.Headers["Cookie"]="your cookie"; – AlexisXu Jan 23 '14 at 07:49
  • request.CookieContainer.Add(new Uri("http://.com", UriKind.Absolute), responseCookies["cookietitle"]); – LojiSmith Jan 23 '14 at 13:31
  • @LojiSmithKaleem I use cookie with `HttpClient` in my own project. – AlexisXu Jan 23 '14 at 13:45
  • @LojiSmithKaleem for example, the web server need to verify your id, we send the cookie with the request with following code [sample](http://stackoverflow.com/questions/12373738/how-do-i-set-a-cookie-on-httpclients-httprequestmessage) and I think httpwebrequest works the same way [httpwebrequest cookie sample](http://stackoverflow.com/questions/18667931/httpwebrequest-add-cookie-to-cookiecontainer-argumentexception-parameternam) – AlexisXu Jan 23 '14 at 13:47