Maintains all cookies/sessions within instance. Works fine multi-instanced.PHP Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
class HTTPWrapper
{
private CookieContainer CookieJar = new CookieContainer();
public String get(String URL)
{
try
{
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
Request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10";
Request.CookieContainer = getCookies();
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
setCookies(Response.Cookies);
Stream RStream = Response.GetResponseStream();
StreamReader SReader = new StreamReader(RStream);
String Return = SReader.ReadToEnd();
SReader.Close();
RStream.Close();
Response.Close();
return Return;
}
catch (Exception ex) { }
return String.Empty;
}
public String post(String URL, String PostData)
{
try
{
byte[] Sending = Encoding.UTF8.GetBytes(PostData);
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
Request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/534.10 (KHTML, like Gecko) Chrome/8.0.552.224 Safari/534.10";
Request.ContentType = "application/x-www-form-urlencoded";
Request.ContentLength = Sending.Length;
Request.CookieContainer = getCookies();
Request.Method = "POST";
Stream DStream = Request.GetRequestStream();
DStream.Write(Sending, 0, Sending.Length);
DStream.Close();
HttpWebResponse Response = (HttpWebResponse)Request.GetResponse();
setCookies(Response.Cookies);
Stream RStream = Response.GetResponseStream();
StreamReader SReader = new StreamReader(RStream);
String Return = SReader.ReadToEnd();
SReader.Close();
RStream.Close();
Response.Close();
return Return;
}
catch (Exception ex) { }
return String.Empty;
}
public void setCookies(CookieCollection Cookies)
{
foreach (Cookie c in Cookies)
{
CookieJar.Add(c);
}
}
public CookieContainer getCookies ()
{
return CookieJar;
}
}
Results 1 to 1 of 1
- 24 Dec. 2010 08:27am #1
- Age
- 30
- Join Date
- Nov. 2009
- Location
- Anaheim, California
- Posts
- 1,065
- Reputation
- 99
- LCash
- 70.08
[Release - C#] Streamlined HTTPWrappers