Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2012 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4347248 to your computer and use it in GitHub Desktop.
Save anonymous/4347248 to your computer and use it in GitHub Desktop.
public enum RequestType
{
GET = 1,
POST = 2
}
public struct ResRequest
{
public string result;
public CookieCollection cookies;
public string cstring;
}
public ResRequest request(string URL, RequestType typ, CookieCollection cookies, string postdata = "", int timeout = 50000, int repeats = 3)
{
try{
HttpWebRequest request;
HttpWebResponse response;
Stream str;
Stream str2;
byte[] data = System.Text.Encoding.UTF8.GetBytes(postdata);
request = (HttpWebRequest)WebRequest.Create(URL);
request.KeepAlive = true;
request.CookieContainer = new CookieContainer();
request.Timeout = timeout;
request.Method = (typ == RequestType.GET ? "GET" : "POST");
request.Accept = "*/*";
request.Headers.Add(HttpRequestHeader.AcceptLanguage, "de");
request.Headers.Add("UA-CPU", "x86");
request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30618) ";
request.ContentType = "application/x-www-form-urlencoded";
if (typ == RequestType.POST)
{
request.ContentLength = data.Length;
}
if (typ != RequestType.GET)
{
str2 = request.GetRequestStream();
str2.Write(data, 0, data.Length);
str2.Close();
str2.Dispose();
}
response = (HttpWebResponse)request.GetResponse();
str = response.GetResponseStream();
if ((response.ContentEncoding.ToLower().Contains("gzip")))
{
str = new GZipStream(str, CompressionMode.Decompress);
}
else if ((response.ContentEncoding.ToLower().Contains("deflate")))
{
str = new DeflateStream(str, CompressionMode.Decompress);
}
string buffer = new StreamReader(str, System.Text.Encoding.UTF8).ReadToEnd();
str.Close();
str.Dispose();
ResRequest result = new ResRequest() { result = buffer, cookies = response.Cookies};
response.Close();
response.Dispose();
return result;
}catch(Exception ex){
if (repeats != 0)
{
return request(URL, typ, cookies, postdata, timeout, repeats - 1);
}
else
{
throw new Exception();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment