PDA

Click to See Complete Forum and Search --> : Cookie parser help =]


Root Core
Feb 18th, 2006, 07:44 PM
Well I wrote out this function for an HTTP Wrapper im making:
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:
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

jmcilhinney
Feb 18th, 2006, 08:27 PM
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: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:TempVal = colCookies.GetByIndex(TempHandler).ToString();

Root Core
Feb 19th, 2006, 03:19 PM
So how would I use the split command with strings and not chars? I fixed the colCookies.GetByIndex problem though thanks :)

jmcilhinney
Feb 19th, 2006, 05:31 PM
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.

Root Core
Feb 19th, 2006, 09:06 PM
Oh I see thanks I'll try that :)