|
-
Feb 18th, 2006, 08:44 PM
#1
Thread Starter
Junior Member
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
-
Feb 18th, 2006, 09:27 PM
#2
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();
-
Feb 19th, 2006, 04:19 PM
#3
Thread Starter
Junior Member
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
-
Feb 19th, 2006, 06:31 PM
#4
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.
-
Feb 19th, 2006, 10:06 PM
#5
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|