Here is a basic one I would recommend only for learning purposes, NOT for apps that you may want to present in a professional manner unless you heavily modify it for efficiency:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.IO;
using System.IO.Compression;
using System.Net.Sockets;
namespace goodwrap
{
class wrapper
{
private TcpClient client;
IDictionary<string, string> colCookies = new Dictionary<string, string>();
public string strCookies;
public string LastPage;
public string Request(string Method, string URL, string Referer)
{
string Host = null;
string strFile = null;
string strPost = null;
int pos = 0;
if (Referer == null)
{
Referer = LastPage;
}
if (URL.Contains("[Only registered and activated users can see links]"))
{
Host = URL.Substring(7);
}
else
{
Host = URL;
}
if (Host.Contains("/"))
{
pos = Host.IndexOf("/", 0);
strFile = Host.Substring(pos);
Host = Host.Substring(0, pos);
}
else
{
strFile = "/";
}
if (Method == "POST")
{
pos = strFile.IndexOf("?");
if (pos != -1)
{
strPost = strFile.Substring(pos + 1);
strFile = strFile.Substring(0, pos);
}
else
{
strPost = null;
}
}
LastPage = URL;
string ReqHeaders = null;
if (Method == "GET" || Method == "PIC")
{
ReqHeaders = "GET" + " " + strFile + " [Only registered and activated users can see links]" + "\r\n"
+ "Host: " + Host + "\r\n"
+ "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4 (.NET CLR 3.5.30729)" + "\r\n"
+ "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "\r\n"
+ "Accept-Language: en-us,en;q=0.5" + "\r\n"
+ "Accept-Encoding: gzip, deflate" + "\r\n"
+ "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "\r\n"
+ "Keep-Alive: 300" + "\r\n"
+ "Connection: keep-alive" + "\r\n"
+ "Referer: " + Referer + "\r\n"
+ "Cookie: " + strCookies + "\r\n" + "\r\n";
}
else
{
ReqHeaders = "POST " + strFile + " [Only registered and activated users can see links]" + "\r\n"
+ "Host: " + Host + "\r\n"
+ "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4 (.NET CLR 3.5.30729)" + "\r\n"
+ "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5" + "\r\n"
+ "Accept-Language: en-us,en;q=0.5" + "\r\n"
+ "Accept-Encoding: gzip, deflate" + "\r\n"
+ "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7" + "\r\n"
+ "Keep-Alive: 300" + "\r\n"
+ "Connection: keep-alive" + "\r\n"
+ "Referer: " + Referer + "\r\n"
+ "Cookie: " + strCookies + "\r\n"
+ "Content-Type: application/x-[Only registered and activated users can see links]" + "\r\n"
+ "Content-Length: " + strPost.Length.ToString() + "\r\n"
+ "Connection: close" + "\r\n" + "\r\n"
+ strPost;
}
if (Method == "PIC") ReqHeaders.Replace("Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5", "Accept: image/png,*/*;q=0.5");
client = new TcpClient(Host, 80);
Byte[] headers = System.Text.Encoding.ASCII.GetBytes(ReqHeaders);
NetworkStream ns = client.GetStream();
ns.Write(headers, 0, headers.Length);
StreamReader sr = new StreamReader(ns, Encoding.Default);
String strHTML = sr.ReadToEnd();
string[] strParts = Regex.Split(strHTML, Environment.NewLine + Environment.NewLine);
strCookies = ParseCookies(strParts[0]);
if (strParts[0].Contains("Content-Encoding"))
{
strParts[1] = DecompressGzip(strParts[1]);
}
return strParts[0] + Environment.NewLine + Environment.NewLine + strParts[1];
}
public string DecompressGzip(string compressed)
{
MemoryStream memStream = new MemoryStream(System.Text.Encoding.Default.GetBytes (compressed));
GZipStream decompressStream = new GZipStream(memStream, CompressionMode.Decompress);
byte[] endBytes = new byte[4];
int position = (int)memStream.Length - 4;
memStream.Position = position;
memStream.Read(endBytes, 0, 4);
memStream.Position = 0;
byte[] buffer = new byte[BitConverter.ToInt32(endBytes, 0) + 100];
int offset = 0;
int total = 0;
while (true)
{
int o = decompressStream.Read(buffer, offset, 100);
if (o == 0) break;
offset += o;
total += o;
}
return Encoding.ASCII.GetString(buffer);
}
public string NeoLogin(string user, string pass, ref bool loggedIn)
{
string strHTML = null;
Request("GET", "[Only registered and activated users can see links]", "[Only registered and activated users can see links]");
Pause(1);
Request("POST", "[Only registered and activated users can see links]" + user, "[Only registered and activated users can see links]");
Pause(1);
strHTML = Request("POST", "[Only registered and activated users can see links]" + user + "&password=" + pass + "&destination=%2Fpetcentral.phtml", "[Only registered and activated users can see links]");
if (strHTML.Contains("Set-Cookie: neologin="))
{
loggedIn = true;
return "Logged In";
}
else if (strHTML.Contains("too many times"))
{
loggedIn = false;
return "To Many Login Attempts";
}
else if (strHTML.Contains("badpassword"))
{
loggedIn = false;
return "Wrong Password";
}
else if (strHTML.Contains("frozen"))
{
loggedIn = false;
return "Account Frozen";
}
else if (strHTML.Contains("just a technical problem"))
{
loggedIn = false;
return "Neopets is down for maintenance.";
}
else
{
loggedIn = false;
return strHTML;
}
}
private static void Pause(double seconds)
{
double num = seconds * 1000;
DateTime t1 = DateTime.Now, t2 = DateTime.Now;
TimeSpan tmDiff = t2 - t1;
while (Convert.ToDouble(tmDiff.TotalMilliseconds.ToStrin g()) < num)
{
t2 = DateTime.Now;
tmDiff = t2 - t1;
Application.DoEvents();
}
}
public string StripHeaders(string strSource)
{
string[] strParts = Regex.Split(strSource, Environment.NewLine + Environment.NewLine);
return strParts[1];
}
public Bitmap GrabPic(string strURL)
{
MemoryStream memStream = new MemoryStream(System.Text.Encoding.Default.GetBytes (StripHeaders(Request("GET", strURL, LastPage))));
Bitmap bitmap = new Bitmap(memStream);
return bitmap;
}
public void ClearCookies()
{
colCookies.Clear();
strCookies = null;
}
public string ParseCookies(string Headers)
{//Credit's to Mystical for RegEx
string ParseCookies = null;
MatchCollection matches;
Regex reg = new Regex("set-cookie:\\s*([^=]+)=([^;]+);", RegexOptions.IgnoreCase);
if (reg.IsMatch(Headers))
{
matches = reg.Matches(Headers);
foreach (Match m in matches)
{
if (colCookies.ContainsKey(m.Groups[1].ToString()))
{
colCookies.Remove(m.Groups[1].ToString());
colCookies.Add(m.Groups[1].ToString(), m.Groups[1].ToString() + "=" + m.Groups[2].ToString());
}
else
{
colCookies.Add(m.Groups[1].ToString(), m.Groups[1].ToString() + "=" + m.Groups[2].ToString());
}
}
}
foreach (KeyValuePair<string, string> item in colCookies)
{
ParseCookies = ParseCookies + item.Value.ToString() + "; ";
}
return ParseCookies;
}
}
}
It can do most of everything you'd need for a Neopets program, so that's cool. Again, when I say learning purposes, I mean you can see how some functions are handled and you can use the code for a better HTTP client.
There is also Drew's from SDNeoClient:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.Globalization;
using System.Text.RegularExpressions;
namespace SD[Only registered and activated users can see links]
{
public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs e);
public delegate void ConnectedEventHandler(object sender, EventArgs e);
public delegate void RequestCompletedEventHandler(object sender, EventArgs e);
public class SD[Only registered and activated users can see links]
{
public event DataReceivedEventHandler DataReceived;
public event ConnectedEventHandler Connected;
public event RequestCompletedEventHandler RequestCompleted;
/// <summary>
/// The response headers from the last request
/// </summary>
public string ResponseHeaders
{
get { return this._ResponseHeaders; }
}
/// <summary>
/// The response body from the last request
/// </summary>
public string ResponseBody
{
get { return this._ResponseBody; }
}
/// <summary>
/// The User Agent to send in requests
/// </summary>
public string UserAgent
{
get { return this._UserAgent; }
set { this._UserAgent = value; }
}
/// <summary>
/// The referer to send
/// </summary>
public string [Only registered and activated users can see links]
{
get { return this._[Only registered and activated users can see links] }
set { this._[Only registered and activated users can see links] = value; }
}
/// <summary>
/// true to use keep-alive connections, false to send connection: close
/// </summary>
public bool KeepAlive
{
get { return this._UseKeepAlive; }
set { this._UseKeepAlive = value; }
}
/// <summary>
/// True to follow 300 redirects
/// </summary>
public bool FollowLocation
{
get { return this._FollowLocation; }
set { this._FollowLocation = value; }
}
/// <summary>
/// If the last request was cancelled
/// </summary>
public bool Cancelled
{
get { return this._Cancelled; }
}
protected const string DEFAULT_USERAGENT = "Firefox 0.3";
protected string _ResponseHeaders; // response headers from the last request
protected string _ResponseBody; // response body from the last request
protected string _LastRequest; // the last http request issued
protected string _UserAgent; // user agent to spoof
protected bool _UseKeepAlive; // send keep-alive header
protected string _[Only registered and activated users can see links] // referer to send with the request
protected string _Last[Only registered and activated users can see links] // the last http response code i.e. 200, 404, 500, 302
protected Socket _sock; // the underlying socket to use for transport
protected string _LastHost; // host that the last request was issued to
protected bool _WasKeepAlive; // if the request was a keepalive request
protected int _LastErrorCode; // last socket error code
protected string _LastErrorMessage; // last error message
protected ulong _KeepAliveLastRequest; // time of the last keep-alive request
protected int _KeepAliveTimeout; // keep-alive timeout
protected string _302Location; // location found in the 302 redirect
protected bool _FollowLocation; // automatically follow redirect responses
protected Hashtable _Cookies; // hash table of cookies from the request
protected double _LastRequestStartTime; // start time of the last request with microseconds
protected double _LastRequestTotalTime; // total elapsed time of the last request with microseconds
protected bool _RequestInProgress; // if a request is currently in progress
protected bool _CancelFlag; // a flag raised to cancel the current operation
protected bool _Cancelled; // the last request was cancelled before it could complete
protected enum [Only registered and activated users can see links] : int
{
RESPONSE_HEADER_BEGIN_H,
RESPONSE_HEADER_BEGIN_T,
RESPONSE_HEADER_BEGIN_T2,
RESPONSE_HEADER_BEGIN_P,
RESPONSE_HEADER_BEGIN_SLASH,
RESPONSE_HEADER_BEGIN_VERMAJOR,
RESPONSE_HEADER_BEGIN_VERDOT,
RESPONSE_HEADER_BEGIN_VERMINOR,
RESPONSE_HEADER_BEGIN_RESPONSECODE,
RESPONSE_HEADER_BEGIN_MESSAGE,
RESPONSE_HEADER_BEGIN_CR,
RESPONSE_HEADER_BEGIN_LF,
RESPONSE_HEADER_KEY,
RESPONSE_HEADER_KEY_COLON,
RESPONSE_HEADER_VALUE,
RESPONSE_HEADER_DATA,
RESPONSE_HEADER_CR,
RESPONSE_HEADER_LF,
RESPONSE_HEADER_CR2,
RESPONSE_HEADER_LF2,
RESPONSE_HEADER_FINISHED,
RESPONSE_BODY_BEGIN,
RESPONSE_READ_CHUNKED_SIZE,
RESPONSE_READ_CHUNKED_EXTENSION,
RESPONSE_READ_CHUNKED_SIZE_LF,
RESPONSE_READ_CHUNKED_DATA,
RESPONSE_READ_CHUNKED_DATA_CR,
RESPONSE_READ_CHUNKED_DATA_LF,
RESPONSE_READ_BODY_BYLEN,
RESPONSE_READ_BODY_TOEND,
RESPONSE_READ_COMPLETE
};
/// <summary>
/// Create a new SD[Only registered and activated users can see links] object
/// </summary>
public SD[Only registered and activated users can see links]()
{
this._UserAgent = DEFAULT_USERAGENT;
this._sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
this._LastHost = "";
this._WasKeepAlive = false;
this._Cookies = new Hashtable();
this._[Only registered and activated users can see links] = "";
this._FollowLocation = false;
}
/// <summary>
/// Perform an HTTP Get request to the URL provided with the optional referer
/// </summary>
/// <param name="url">The URL to fetch</param>
/// <param name="referer">The referer to send</param>
/// <exception cref="ArgumentException">Thrown if the url is malformed</exception>
/// <exception cref="SD[Only registered and activated users can see links]">Thrown on failure of the request</exception>
public void Get(string url, string referer = "")
{
Uri uri;
try {
uri = new Uri(url);
} catch (UriFormatException ex) {
throw new ArgumentException("Invalid URL: " + ex.Message);
}
this.[Only registered and activated users can see links] = referer;
Byte[] request = this._Get[Only registered and activated users can see links]("GET", uri);
do {
if (this._CancelFlag) {
this._CleanupRequest();
return;
}
try {
this._DoRequest(request, uri);
} catch (SD[Only registered and activated users can see links] e) {
throw e;
}
if (this._302Location != "") {
uri = new Uri(uri, this._302Location);
request = this._Get[Only registered and activated users can see links]("GET", uri);
}
} while (this.FollowLocation == true && this._Last[Only registered and activated users can see links] == "302");
}
/// <summary>
/// Perform an HTTP Post request to the URL provided with the post parameters and optional referer
/// </summary>
/// <param name="url">The URL to post to</param>
/// <param name="postfields">The HTTP post data to send</param>
/// <param name="referer">The referer to send</param>
/// <exception cref="ArgumentException">Thrown if the url is malformed</exception>
/// <exception cref="SD[Only registered and activated users can see links]">Thrown on failure of the request</exception>
public void Post(string url, string postfields, string referer = "")
{
Uri uri;
try {
uri = new Uri(url);
} catch (UriFormatException ex) {
throw new ArgumentException("Invalid URL: " + ex.Message);
}
this.[Only registered and activated users can see links] = referer;
Byte[] request = this._Get[Only registered and activated users can see links]("POST", uri, postfields);
do {
if (this._CancelFlag) {
this._CleanupRequest();
return;
}
try {
this._DoRequest(request, uri);
} catch (SD[Only registered and activated users can see links] e) {
throw e;
}
if (this._302Location != "") {
uri = new Uri(uri, this._302Location);
request = this._Get[Only registered and activated users can see links]("GET", uri);
}
} while (this.FollowLocation == true && this._Last[Only registered and activated users can see links] == "302");
}
/// <summary>
/// Check if the client is busy with an operation
/// </summary>
/// <returns>true if a request is in process, false if not</returns>
public bool IsBusy()
{
return this._RequestInProgress;
}
/// <summary>
/// Cancel the current request immediately
/// </summary>
public void Cancel()
{
this._CancelFlag = true;
}
/// <summary>
/// Get the CookieContainer for the specified host
/// </summary>
/// <param name="host">The host to get the cookies from</param>
/// <returns>CookieContainer containing the active cookies</returns>
public CookieContainer GetCookies(string host)
{
if (this._Cookies.ContainsKey(host)) {
return (CookieContainer)this._Cookies[host];
} else {
return new CookieContainer();
}
}
/// <summary>
/// Set the cookies for the specified host
/// </summary>
/// <param name="host">The host to set cookies for</param>
/// <param name="cookies">The cookies to set</param>
public void SetCookies(string host, CookieContainer cookies)
{
this._Cookies[host] = cookies;
}
/// <summary>
/// Get the HTTP request body for the last request issued
/// </summary>
/// <returns>A string containing the last request issued</returns>
public string GetLastRequest()
{
return this._LastRequest;
}
/// <summary>
/// Get the total amount of time the last request took to process from connection to completion
/// </summary>
/// <returns>The elapsed time in seconds</returns>
public double GetLastRequestElapsedTime()
{
return this._LastRequestTotalTime;
}
/// <summary>
/// Strip all HTML tags from the text or response body
/// </summary>
/// <param name="html">Optional html to strip tags from</param>
/// <returns>The string without html tags</returns>
public string StripTags(string html = null)
{
Regex re = new Regex("(?:<script[^>]*>.*?</script>)|(?:<[^>]+>)");
if (html == null) {
if (this.ResponseBody == null) return "";
return re.Replace(this.ResponseBody, "");
} else {
return re.Replace(html, "");
}
}
protected void _DoRequest(Byte[] [Only registered and activated users can see links] Uri uri)
{
this._CancelFlag = this._Cancelled = false;
this._LastRequestStartTime = this._UnixTimestampWithMicroseconds();
this._RequestInProgress = true;
this._ResponseBody = "";
this._ResponseHeaders = "";
if (this._CancelFlag) {
this._CleanupRequest();
return;
}
try {
this._Connect(uri);
} catch(SD[Only registered and activated users can see links] e) {
this._CleanupRequest();
throw e;
}
this._WasKeepAlive = false;
Byte[] recv = new Byte[32768];
string headers = "", body = "";
byte c;
int bytePointer = 0;
Hashtable hHeaders = CollectionsUtil.CreateCaseInsensitiveHashtable();
this._LastRequest = Encoding.ASCII.GetString([Only registered and activated users can see links]);
if (this._CancelFlag) {
this._CleanupRequest();
return;
}
Console.WriteLine("Sending.");
try {
this._sock.Send([Only registered and activated users can see links] [Only registered and activated users can see links] 0);
Console.WriteLine("Sent.");
} catch (SocketException ex) {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links](ex.Message, ex);
}
Byte[] responseBody = new Byte[0];
int bytes = 0;
long bytesReceived = 0;
int resp_state = (int)[Only registered and activated users can see links]
long contentLength = 0, contentRead = 0;
bool connectionClose = true;
string header = "", headerValue = "", responseCode = "", responseMessage = "";
string contentEncoding = "", transferEncoding = "";
string sChunkSize = "";
int iChunkSize = 0;
Console.WriteLine("Receiving Data.");
this._sock.ReceiveTimeout = 1000;
do {
try {
if (this._CancelFlag) {
this._CleanupRequest();
return;
}
bytes = this._sock.Receive(recv, recv.Length, 0);
} catch (SocketException ex) {
// see if this is a result of our receivetimeout
if (ex.SocketErrorCode == SocketError.WouldBlock ||
ex.SocketErrorCode == SocketError.IOPending ||
ex.SocketErrorCode == SocketError.NoBufferSpaceAvailable) {
Thread.Sleep(10);
continue; // go back to recv
}
this._LastErrorMessage = ex.Message;
this._LastErrorCode = ex.NativeErrorCode;
this._CleanupRequest();
if (ex.NativeErrorCode == 10054) {
throw new SD[Only registered and activated users can see links]("The connection was reset");
} else {
throw new SD[Only registered and activated users can see links](ex.Message, ex);
}
}
Console.Write("Received " + bytes.ToString() + " ");
if (resp_state > (int)[Only registered and activated users can see links]) {
bytesReceived += bytes;
}
if (this._CancelFlag) {
this._CleanupRequest();
return;
}
OnDataReceivedEvent(new DataReceivedEventArgs(bytesReceived, contentLength));
for (int i = 0; resp_state != (int)[Only registered and activated users can see links] ++i) {
if (i >= bytes) {
break; // read more data from socket
}
c = recv[i];
switch (resp_state) {
case (int)[Only registered and activated users can see links]
if (c == 'H') {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid HTTP response line, expected H");
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == 'T') {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid HTTP response line, expected HT");
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == 'T') {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid HTTP response line, expected HTT");
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == 'P') {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid HTTP response line, expected HTTP");
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == '/') {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid HTTP response line, expected [Only registered and activated users can see links]");
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == '.') {
resp_state = (int)[Only registered and activated users can see links]
} else {
if (!this._IsNumeric((char)c)) {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid HTTP version");
}
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == ' ') {
resp_state = (int)[Only registered and activated users can see links] ;
} else {
if (!this._IsNumeric((char)c)) {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid HTTP version");
}
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links] :
if (this._IsNumeric((char)(c))) {
responseCode += (char)c;
} else if (c == ' ') {
if (responseCode.Length == 0) {
break; // go again
} else {
resp_state = (int)[Only registered and activated users can see links]
}
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid response code");
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (this._IsCtl((char)c)) {
if (responseMessage.Length == 0) {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Expecting response message, invalid control character");
} else if (c == 0x0D) {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Expecting response message");
}
} else {
responseMessage += (char)c;
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == 0x0A) {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Expecting \\n");
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == ' ') {
if (header.Length > 0) {
resp_state = (int)[Only registered and activated users can see links]
} else {
break; // go again
}
} else if (this._IsText((char)c) && c != ':') {
header += (char)c;
} else if (c == ':') {
if (header.Length == 0) {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Missing header value");
} else {
resp_state = (int)[Only registered and activated users can see links]
}
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == ' ') {
break; // go again
} else if (c == ':') {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid header line, expecting :");
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (this._IsCtl((char)c)) {
if (c == 0x0D) {
if (hHeaders.ContainsKey(header)) {
// TODO: Fix this
} else {
hHeaders.Add(header, headerValue);
}
header = "";
headerValue = "";
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid header line, expecting 0x0D");
}
} else {
if (c == ' ' && headerValue.Length == 0) {
// consume whitespace
} else {
headerValue += (char)c;
}
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == 0x0D) {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid header line, expecting 0x0D");
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == 0x0A) {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid response, expecting LF");
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == 0x0D) {
resp_state = (int)[Only registered and activated users can see links]
} else if (this._IsText((char)c)) {
header += (char)c;
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid header line, expecting 0x0D or new header");
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
if (c == 0x0A) {
resp_state = (int)[Only registered and activated users can see links]
bytePointer = i;
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid header line, expecting 0x0A");
}
headers += (char)c;
break;
case (int)[Only registered and activated users can see links]
// inspect the headers for a content-length
if (hHeaders.ContainsKey("Content-Length")) {
// get cl
if (Int64.TryParse(hHeaders["Content-Length"].ToString(), out contentLength) == false) {
// invalid content length
this._CleanupRequest();
throw new Exception("Invalid content-length value in response");
}
}
if (hHeaders.ContainsKey("Content-Encoding")) {
contentEncoding = (string)hHeaders["Content-Encoding"];
}
if (hHeaders.ContainsKey("Transfer-Encoding")) {
transferEncoding = (string)hHeaders["Transfer-Encoding"];
}
if (hHeaders.ContainsKey("Connection")) {
string conn = (string)hHeaders["Connection"];
if (conn.ToLower().Trim() == "close") {
connectionClose = true;
} else {
connectionClose = false;
}
}
if (transferEncoding.ToLower() == "chunked") {
resp_state = (int)[Only registered and activated users can see links]
} else if (contentLength > 0) {
resp_state = (int)[Only registered and activated users can see links]
} else if (connectionClose == true) {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new Exception("Invalid body read state - could be a bug or invalid response");
}
i--; // step back because i is the first part of the body
break;
case (int)[Only registered and activated users can see links]
{
if (bytes == 0) {
resp_state = (int)[Only registered and activated users can see links]
} else {
Byte[] temp = new Byte[responseBody.Length + bytes - i];
Buffer.BlockCopy(responseBody, 0, temp, 0, responseBody.Length);
Buffer.BlockCopy(recv, i, temp, responseBody.Length, bytes - i);
responseBody = temp;
i = bytes + 1;
}
break;
}
case (int)[Only registered and activated users can see links]
{
int readLen = 0;
if (contentLength - contentRead <= (bytes - i)) {
readLen = (int)contentLength;
resp_state = (int)[Only registered and activated users can see links]
} else {
readLen = bytes - i;
}
Byte[] temp = new Byte[responseBody.Length + readLen];
Buffer.BlockCopy(responseBody, 0, temp, 0, responseBody.Length);
Buffer.BlockCopy(recv, i, temp, responseBody.Length, readLen);
responseBody = temp;
contentRead += readLen;
i += readLen - 1;
break;
}
case (int)[Only registered and activated users can see links]
if (this._IsHex((char)c)) {
sChunkSize += (char)c;
} else if (c == ' ') {
if (sChunkSize.Length == 0) {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid response, expecting chunk size");
} else {
resp_state = (int)[Only registered and activated users can see links]
}
} else if (c == 0x0D) {
resp_state = (int)[Only registered and activated users can see links]
}
break;
case (int)[Only registered and activated users can see links]
if (this._IsCtl((char)c)) {
if (c == 0x0D) {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid chunked response line, expecting 0x0D");
}
} else if (this._IsText((char)c)) {
// add to chunk ext
sChunkSize += (char)c;
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid response, bad chunk size");
}
break;
case (int)[Only registered and activated users can see links]
if (c == 0x0A) {
resp_state = (int)[Only registered and activated users can see links]
if (Int32.TryParse(sChunkSize, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out iChunkSize) == false) {
// invalid state
this._CleanupRequest();
throw new Exception("Bad response, invalid chunk size");
} else {
sChunkSize = "";
if (iChunkSize == 0) {
resp_state = (int)[Only registered and activated users can see links]
}
}
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid chunked response line, expecting 0x0A");
}
break;
case (int)[Only registered and activated users can see links]
{
if (iChunkSize == 0) {
resp_state = (int)[Only registered and activated users can see links]
i--; // put back
} else {
int readLen = 0;
if (iChunkSize <= bytes - i) {
readLen = iChunkSize;
resp_state = (int)[Only registered and activated users can see links]
} else {
readLen = bytes - i;
}
Byte[] temp = new Byte[responseBody.Length + readLen];
Buffer.BlockCopy(responseBody, 0, temp, 0, responseBody.Length);
Buffer.BlockCopy(recv, i, temp, responseBody.Length, readLen);
responseBody = temp;
contentLength += readLen;
iChunkSize -= readLen;
i += readLen - 1;
}
break;
}
case (int)[Only registered and activated users can see links]
if (c == 0x0D) {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid chunked response line, expecting 0x0D");
}
break;
case (int)[Only registered and activated users can see links]
if (c == 0x0A) {
resp_state = (int)[Only registered and activated users can see links]
} else {
this._CleanupRequest();
throw new SD[Only registered and activated users can see links]("Invalid chunked response line, expecting 0x0A");
}
break;
} // switch(resp_state)
}
if (resp_state == (int)[Only registered and activated users can see links]) {
// the response has finished being read, socket is either being closed by remote host
// or is being kept-alive
this._LastRequestTotalTime = this._UnixTimestampWithMicroseconds() - this._LastRequestStartTime;
break;
}
if (this._CancelFlag) {
this._CleanupRequest();
return;
}
} while (bytes > 0);
if (contentEncoding == "gzip") {
// deflate gzip content
this._GZInflate(ref responseBody, ref body);
} else {
body = Encoding.ASCII.GetString(responseBody);
}
responseBody = null;
this._WasKeepAlive = connectionClose == false;
if (this._WasKeepAlive) {
if (this._CancelFlag) {
this._CleanupRequest();
return;
}
if (hHeaders.ContainsKey("Keep-Alive"))
{
string kaStr = hHeaders["Keep-Alive"].ToString();
[Only registered and activated users can see links]
Match m;
m = re.Match(kaStr);
if (m.Success)
{
if (Int32.TryParse(m.Groups[1].Value.ToString(), out this._KeepAliveTimeout) == false)
{
// invalid state
this._CleanupRequest();
throw new Exception("Bad response, keep-alive value is invalid");
}
}
}
else
{
this._KeepAliveTimeout = 60; // default timeout of 60 seconds if the server does not indicate a limit
}
this._KeepAliveLastRequest = this._UnixTimestamp();
} else {
this._DisconnectSocket();
}
if (responseCode == "302") {
this._302Location = hHeaders["Location"].ToString();
} else {
this._302Location = "";
}
this._ParseCookies(uri.Host, headers);
this._Last[Only registered and activated users can see links] = responseCode;
this._ResponseBody = body;
this._ResponseHeaders = headers;
this._RequestInProgress = false;
this._CancelFlag = false;
this._Cancelled = false;
OnRequestCompletedEvent(new EventArgs());
}
protected void _CleanupRequest()
{
this._RequestInProgress = false;
this._DisconnectSocket();
if (this._CancelFlag) {
this._Cancelled = true;
this._CancelFlag = false;
OnRequestCompletedEvent(new EventArgs());
}
}
protected void _DisconnectSocket()
{
try {
this._sock.Shutdown(SocketShutdown.Both);
this._sock.Disconnect(false);
} catch (SocketException e) {
Console.WriteLine("Caught socket exception trying to shut down socket: {0}: {1}", e.NativeErrorCode.ToString(), e.Message);
}
}
protected int _ParseCookies(string host, string headers)
{
CookieContainer cc;
if (this._Cookies.ContainsKey(host)) {
cc = (CookieContainer)this._Cookies[host];
} else {
cc = new CookieContainer();
this._Cookies[host] = cc;
}
return cc.ParseHeaders(headers);
}
protected void _Connect(Uri uri)
{
bool connect = false;
bool blockingState = this._sock.Blocking;
if (this._LastHost != uri.Host) {
connect = true;
} else if (this._WasKeepAlive == false) {
connect = true;
} else {
// check to see if the current socket is still open
try {
bool part1 = this._sock.Poll(1000, SelectMode.SelectRead);
bool part2 = (this._sock.Available == 0);
if (part1 && part2) {
connect = true;
} else {
connect = false;
}
} catch (SocketException e) {
if (e.NativeErrorCode.Equals(10035)) {
connect = true;
} else {
connect = true;
}
} finally {
this._sock.Blocking = blockingState;
if (this._sock.Connected == false) {
connect = true;
}
if ( connect == false && (int)(this._UnixTimestamp() - this._KeepAliveLastRequest) >= this._KeepAliveTimeout) {
connect = true;
}
}
}
if (connect == true) {
if (this._sock.Connected) {
this._DisconnectSocket();
}
this._sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
Console.WriteLine("Connecting.");
try {
this._sock.Connect(uri.Host, uri.Port);
OnConnectedEvent(new EventArgs());
this._LastHost = uri.Host;
} catch (SocketException e) {
this._LastErrorCode = e.NativeErrorCode;
this._LastErrorMessage = e.Message;
throw new SD[Only registered and activated users can see links]("Failed to connect to remote host", e);
}
} else {
OnConnectedEvent(new EventArgs());
Console.WriteLine("Used keep-alive connection");
}
}
protected Byte[] _Get[Only registered and activated users can see links](string method, Uri uri, string data = "")
{
string contentLen = "";
string cookieHeader = "";
string refererHeader = "";
string connection = "";
string req = "{0} {1} [Only registered and activated users can see links]" +
"Host: {2}\r\n" +
"User-Agent: {3}\r\n" +
"Accept-Encoding: gzip,deflate\r\n" +
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
"Keep-Alive: 30\r\n" +
"Connection: {4}\r\n" +
"{5}" + // referer, if set
"{6}" + // cookies, if any
//"Connection: close\r\n" +
"{7}" + // content length
"{8}\r\n";
if (method == "POST") {
contentLen = "Content-type: application/x-[Only registered and activated users can see links]";
contentLen += "Content-Length: " + data.Length.ToString() + "\r\n";
data = "\r\n" + data;
}
if (this._Cookies.ContainsKey(uri.Host)) {
CookieContainer cc = (CookieContainer)this._Cookies[uri.Host];
cookieHeader = cc.ToString();
if (cookieHeader != "") {
cookieHeader = "Cookie: " + cookieHeader + "\r\n";
}
}
if (this._[Only registered and activated users can see links] != "") {
refererHeader = "Referer: " + this._[Only registered and activated users can see links] + "\r\n";
}
if (this._UseKeepAlive == true) {
connection = "keep-alive";
} else {
connection = "close";
}
req = string.Format(req,
method,
uri.PathAndQuery,
uri.Host,
this._UserAgent,
connection,
refererHeader,
cookieHeader,
contentLen,
data);
Byte[] b = Encoding.ASCII.GetBytes(req);
return b;
}
protected string UrlEncode(string s)
{
string encoded = "";
char c;
for (int i = 0; i < s.Length; ++i) {
c = s[i];
if (c == ' ') {
encoded += '+';
} else if ((c < '0' && c != '-' && c != '.') ||
(c < 'A' && c > '9') ||
(c > 'Z' && c < 'a' && c != '_') ||
(c > 'z')) {
encoded += '%';
encoded += ((int)c).ToString("X");
} else {
encoded += c;
}
}
return encoded;
}
protected bool _GZInflate(ref byte[] content, ref string decompressed)
{
Byte[] outBuf = new Byte[0x4000];
int read = 0;
decompressed = "";
System.IO.MemoryStream stream = new System.IO.MemoryStream(content);
System.IO.Compression.GZipStream gzstream = new System.IO.Compression.GZipStream(stream, System.IO.Compression.CompressionMode.Decompress);
while ((read = gzstream.Read(outBuf, 0, outBuf.Length)) > 0) {
decompressed += Encoding.ASCII.GetString(outBuf, 0, read);
}
outBuf = null;
stream.Close();
gzstream.Close();
stream.Dispose();
gzstream.Dispose();
return true;
}
protected bool _IsNumeric(char c)
{
if (c >= 48 && c <= 57) {
return true;
} else {
return false;
}
}
protected bool _IsAlpha(char c)
{
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
return true;
} else {
return false;
}
}
protected bool _IsText(char c)
{
if (c >= 32 && c < 127) {
return true;
} else {
return false;
}
}
protected bool _IsCtl(char c)
{
if (c < 32 || c == 127) {
return true;
} else {
return false;
}
}
protected bool _IsHex(char c)
{
if ((c >= 65 && c <= 70) || (c >= 97 && c <= 102) || (c >= 48 && c <= 57)) {
return true;
} else {
return false;
}
}
protected ulong _UnixTimestamp()
{
return (ulong)this._UnixTimestampWithMicroseconds();
}
protected double _UnixTimestampWithMicroseconds()
{
return (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
}
/// <summary>
/// Get the current time as a Unix timestamp
/// </summary>
/// <returns></returns>
public static double UnixTime()
{
return (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds;
}
protected virtual void OnDataReceivedEvent(DataReceivedEventArgs e)
{
if (DataReceived != null) {
DataReceived(this, e);
}
}
protected virtual void OnConnectedEvent(EventArgs e)
{
if (Connected != null) {
Connected(this, e);
}
}
protected virtual void OnRequestCompletedEvent(EventArgs e)
{
if (RequestCompleted != null) {
RequestCompleted(this, e);
}
}
}
/// <summary>
/// A container of http cookies
/// </summary>
public class CookieContainer
{
protected Hashtable _cookies;
public CookieContainer()
{
this._cookies = new Hashtable(10);
}
/// <summary>
/// Parse cookies out of an http response header block
/// </summary>
/// <param name="headers">The headers to get cookies out of</param>
/// <returns>The number of cookies sent</returns>
public int ParseHeaders(string headers)
{
char[] split = new char[] { '\r', '\n' };
string[] lines = headers.Split(split, StringSplitOptions.RemoveEmptyEntries);
[Only registered and activated users can see links]
Match m;
int count = 0;
//Set-Cookie: xt6Yr4e33D=94394812224217313165124; expires=Tue, 22-May-2012 03:44:02 GMT; path=/; domain=.site.com
foreach (string header in lines) {
m = regCookie.Match(header);
if (m.Success) {
this.SetCookie(m.Groups[1].Value.ToString(), m.Groups[2].Value.ToString());
count++;
}
}
return count;
}
/// <summary>
/// Set an individual cookie
/// </summary>
/// <param name="key">The cookie name</param>
/// <param name="value">The value of the cookie</param>
public void SetCookie(string key, string value)
{
if (this._cookies.ContainsKey(key)) {
this._cookies[key] = value;
} else {
this._cookies.Add(key, value);
}
}
/// <summary>
/// Get the content of a cookie by name
/// </summary>
/// <param name="key">The cookie to get</param>
/// <returns>The cookie value</returns>
public string GetCookie(string key)
{
if (this._cookies.ContainsKey(key)) {
return this._cookies[key].ToString();
} else {
return "";
}
}
/// <summary>
/// Unset a cookie
/// </summary>
/// <param name="key">The cookie to remove</param>
/// <returns>true if found and removed, false if not found</returns>
public bool RemoveCookie(string key)
{
if (this._cookies.ContainsKey(key)) {
this._cookies.Remove(key);
return true;
} else {
return false;
}
}
/// <summary>
/// Return the underlying hash table of the cookie container
/// </summary>
/// <returns>Hashtable of key value cookie pairs</returns>
public Hashtable GetCookies()
{
return this._cookies;
}
/// <summary>
/// Return the cookies as a string
/// </summary>
/// <returns></returns>
public override string ToString()
{
string s = "";
foreach (DictionaryEntry d in this._cookies) {
s += d.Key + "=" + d.Value + "; ";
}
if (s.Length > 2) {
s = s.Substring(0, s.Length - 1);
}
return s;
}
}
/// <summary>
/// Thrown when a request fails
/// </summary>
public class SD[Only registered and activated users can see links] : Exception
{
public SD[Only registered and activated users can see links]()
{
}
public SD[Only registered and activated users can see links](string message)
: base(message)
{
}
public SD[Only registered and activated users can see links](string message, Exception innerException)
: base(message, innerException)
{
}
}
/// <summary>
/// Parameters for the DataReceived event
/// </summary>
public class DataReceivedEventArgs : EventArgs
{
private long bytesReceived;
private long bytesTotal;
public DataReceivedEventArgs(long received, long total)
{
this.bytesReceived = received;
this.bytesTotal = total;
}
public long BytesTotal
{
get { return this.bytesTotal; }
}
public long BytesReceived
{
get { return this.bytesReceived; }
}
}
}
This I would leave here for the same purpose, just for learning. I transitioned to this HTTP client when making Stealth Core which was the worst decision ever for someone new to C# as I had to fix and add way too many things. This is untouched and would leave it here for learning purposes. Some functions presented better than the previous wrapper.
Powered by vBulletin® Version 4.2.0 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.