Moja propozycja na szybko byla by taka (lepiej jednak zastanow sie cze HttpRequest nie bedzie lepsze, tam sie tez da ustawic sesje etc. i dziala na pewno - z doswiadczenia!)
namespace Tester
{
/// <summary>
/// Description of Polaczenie.
/// </summary>
public class Polaczenie
{
string host;
Socket socket;
Dictionary<string, string> cookies = new Dictionary<string, string>();
public Polaczenie(string host)
{
this.host = host;
}
public string phpsessid
{
get
{
string s = "";
cookies.TryGetValue("PHPSESSID", out s);
return s;
}
}
private bool loggedIn = false;
private void SetCookie(string name, string value)
{
if (cookies.ContainsKey(name)) cookies.Remove(name);
if (value != "deleted")
{
cookies.Add(name, value);
}
if (name == "autousername") loggedIn = true;
}
private bool HasCookies
{
get
{
return cookies.Count > 0;
}
}
private string GetCookieHeaderString()
{
StringBuilder sb = new StringBuilder("Cookie: ");
foreach (KeyValuePair<string, string> cookie in cookies)
{
sb.Append(cookie.Key + "=" + cookie.Value + "; ");
}
return sb.ToString();
}
private void TransformHeaders(StreamReader sr)
{
string s = "";
string header = "";
do
{
s = sr.ReadLine();
header += s + "\n";
} while (s != "");
TransformHeaders(header);
}
private void TransformHeaders(string header)
{
Console.WriteLine(header);
foreach (Match m in Regex.Matches(header, "Set-Cookie: ([^=]*)=([^;]*)"))
{
SetCookie(m.Groups[1].Value, m.Groups[2].Value);
}
}
public bool IsLoggedIn()
{
return loggedIn;
}
public static byte[] StrToByteArray(string str)
{
Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
return encoding.GetBytes(str);
}
public MemoryStream SendGet(string addres)
{
if (addres.Contains(host))
{
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(host, 80);
addres = addres.Substring(addres.IndexOf(host) + host.Length);
NetworkStream ns = new NetworkStream(socket, true);
StreamWriter sw = new StreamWriter(ns);
StreamReader sr = new StreamReader(ns, Encoding.GetEncoding("ISO-8859-1"));
BinaryReader br = new BinaryReader(ns);
sw.WriteLine("GET " + addres + " HTTP/1.1");
sw.WriteLine("Host: " + host);
sw.WriteLine("User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-EN; rv:1.8.0.1) Gecko/20060313 Debian/1.5.dfsg+1.5.0.1-4 Firefox/1.5.0.1");
sw.WriteLine("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5");
sw.WriteLine("Accept-Language: en-us;q=0.7,en;q=0.3");
sw.WriteLine("Accept-Encoding: gzip,deflate");
sw.WriteLine("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7");
sw.WriteLine("Connection: keep-alive");
if (HasCookies) sw.WriteLine(GetCookieHeaderString());
sw.WriteLine();
sw.Flush();
Console.WriteLine("AAA");
Console.WriteLine(br.ReadString());
TransformHeaders(sr);
Console.WriteLine("SESJA: " + phpsessid);
//mozna tu pokombinowac aby bylo ladniej
MemoryStream ms = new MemoryStream();
Byte[] data = StrToByteArray(sr.ReadToEnd());
ms.Write(data, 0, data.Length);
return ms;
}
return null;
}
}
class Program
{
public static void Main(string[] args)
{
Polaczenie conn = new Polaczenie("cda.pl");
MemoryStream sr = conn.SendGet("http://www.cda.pl/images/logo-mniejsze.jpg");
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(sr);
sr.Close();
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}