Results 1 to 5 of 5

Thread: Cookie parser help =]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    23

    Cookie parser help =]

    Well I wrote out this function for an HTTP Wrapper im making:
    Code:
            public string ExtractCookies(string strHeader)
            {
                string[] SplitStr;
                string[] Temp;
                string TempVal;
                string Key;
                string Value;
                int i;
                int TempHandler;
                string strCookies;
    
                SplitStr = strHeader.Split("\r\n");
    
                for (i = 0; SplitStr.Length != 0; i++)
                {
                    if (SplitStr[i].Contains("Set-Cookie"))
                    {
                        Temp = SplitStr[i].Split("Set-Cookie: ");
                        Temp = Temp[1].Split("=");
                        Key = Temp[0];
                        Temp = Temp[1].Split(";");
                        Value = Temp[0];
                        if (colCookies.Contains(Key))
                        {
                            TempHandler = Convert.ToInt16(Key);
                            TempVal = colCookies.GetByIndex(TempHandler);
                            if (TempVal != Value)
                            {
                                colCookies.Remove(Key);
                                colCookies.Add(Key, Key + "=" + Value + ";");
                            }
                        }
                        else
                        {
                            colCookies.Add(Key, Key + "=" + Value + ";");
                        }
                    }
                }
    
                strCookies = "";
                int j;
    
                for (j = 1; colCookies.Count != 0; j++)
                {
                    strCookies = strCookies + colCookies.GetByIndex(j);
                }
    
                return strCookies;
            }
    Im fairly new to C# would someone mind pointing out whats wrong with this coding? I get an error with every one of the split actions >_< which says
    Argument '1': cannot convert from 'string' to 'char[]'
    and I get this error for the split actions aswell
    "The best overloaded method match for 'string.Split(params char[])' has some invalid arguments"
    and I get an error in this part:
    Code:
    TempHandler = Convert.ToInt16(Key);
    TempVal = colCookies.GetByIndex(TempHandler);
    which says:
    Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
    Does anyone know what Im doing wrong with the syntax?

    PS: Im using Microsoft Visual C# 2005 Express Edition

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Cookie parser help =]

    String.Split takes a Char or Char array as a parameter. You are passing a String. Strings are enclosed in double quotes while Chars are enclosed in single quotes.

    As for the second issue, you've declared TempHandler as 'int', which is Int32, so why are you converting to Int16, which is 'short'? I would imagine that the actual issue is that GetByIndex returns an Object reference and you're trying to assign it to a String variable. If the underlying object is a string then you should cast it as a string:
    Code:
    TempVal = (string)colCookies.GetByIndex(TempHandler);
    If the underlying object is not a string then a cast will still work but strictly speaking you should convert the object:
    Code:
    TempVal = colCookies.GetByIndex(TempHandler).ToString();
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    23

    Re: Cookie parser help =]

    So how would I use the split command with strings and not chars? I fixed the colCookies.GetByIndex problem though thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Cookie parser help =]

    You're only splitting on a single character so you don't have to use a string. If you did want to split on a string you could use Regex.Split instead.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2006
    Posts
    23

    Re: Cookie parser help =]

    Oh I see thanks I'll try that

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width