2

I need to send multiple set of cookies like below to Java WebService from WCF Client.

Set-Cookie: JSESSIONID=ABCDLhSgAceJ9bpEFSgLvi53; Path=/XXX

Set-Cookie: zz=mmm;kk=qqq;XXXX;

These two cookies I am receving from login JAVA WebService and I need to forward these same cookies to other Java Web Service for maintaining sessions. I have tried with following using IClientMessageInspector but could not sent exact two cookie as shown above. I can send only one.

 if (!string.IsNullOrEmpty(cookieValueFromLogin))
        {
            string[] cookieValues = cookieValueFromLogin.Split(new char[] { ',' });

            for (int i = 0; i < cookieValues.Length; i++)
            {
                if (!string.IsNullOrEmpty(cookieValues[i]))
                {

                    httpRequestMessage.Headers.Add("Cookie", cookieValues[i] );                      

                }
            }               

        }

Please help here if anybody knows.

Thanks MP

mit
  • 1,763
  • 4
  • 16
  • 27
  • arent they passed as name value pairs, and thus need a unique name per header? – Chris Sep 12 '13 at 11:12
  • Cookies are separated by comma and values in a cookie are keyvalue pairs and I need to forward two separated cookies as I got in Login. – mit Sep 12 '13 at 12:05
  • yes, but the headers under the Key of 'Cookie' will only contain one value – Chris Sep 12 '13 at 15:04
  • But our java web services are expecting two cookies. Do you know how to send multiple set of cookies? – mit Sep 13 '13 at 11:06

1 Answers1

1

You only actually need to set one header key: Cookie. The request cookie header will look like:

Cookie: JSESSIONID=ABCDLhSgAceJ9bpEFSgLvi53;zz=mmm;kk=qqq;

The modification below should generate the correct value, though it can almost certainly be implemented much more cleanly.

 if (!string.IsNullOrEmpty(cookieValueFromLogin))
    {
        string[] cookieValues = cookieValueFromLogin.Split(new char[] { ',' });
        string cookieHeader = new string();        

        for (int i = 0; i < cookieValues.Length; i++)
        {
            if (!string.IsNullOrEmpty(cookieValues[i]))
            {

                cookieHeader = cookieHeader + cookieValues[i];                      

            }
        }  

        httpRequestMessage.Headers.Add("Cookie", cookieHeader );             

    }
James Ralston
  • 1,170
  • 8
  • 11
  • But we need to set two "Cookie" headers in a request, so that java services can understand two separate cookies are being received by them. – mit Sep 16 '13 at 06:47
  • 1
    Please look this link. http://stackoverflow.com/questions/16305814/are-multiple-cookie-headers-allowed-in-an-http-request – mit Sep 16 '13 at 07:03