using System; using System.IO; using System.Net; using System.Text; using System.Diagnostics; using System.Collections; using System.Threading; using System.IO.Compression; using System.Reflection; using System.Configuration; // Filename: HttpRequest.cs // Author:Sean Kessler namespace MarketData.Integration { public class HttpNetResponse : IDisposable { public enum TypeResponse {Stream,String}; public String ResponseString{get;set;} public MemoryStream ResponseStream{get;set;} public CookieCollection CookieCollection{get;set;} public HttpNetResponse() { Success=false; } public HttpNetResponse(String responseString,String request,HttpWebResponse webResponse,CookieCollection cookieCollection,bool success) { ResponseString=responseString; ResponseType=TypeResponse.String; Success=success; Request=request; CookieCollection=cookieCollection; if(null!=webResponse)StatusCode=webResponse.StatusCode; } public HttpNetResponse(String responseString,String request,HttpWebResponse webResponse,bool success) { ResponseString=responseString; ResponseType=TypeResponse.String; Success=success; Request=request; if(null!=webResponse)StatusCode=webResponse.StatusCode; } public HttpNetResponse(MemoryStream responseStream,String request,HttpWebResponse webResponse,bool success) { ResponseStream=responseStream; ResponseType=TypeResponse.String; Success=success; Request=request; if(null!=webResponse)StatusCode=webResponse.StatusCode; } public HttpNetResponse(HttpWebResponse webResponse,String request,bool success,String errorMessage) { ResponseType=TypeResponse.String; Success=success; Request=request; if(null!=webResponse)StatusCode=webResponse.StatusCode; ErrorMessage=errorMessage; } public Stream ResponseStringToStream() { MemoryStream stream = new MemoryStream(); StreamWriter writer = new StreamWriter(stream); writer.Write(ResponseString); writer.Flush(); stream.Position = 0; return stream; } public String ResponseStreamToString { get { if(!Success)return null; return System.Text.Encoding.UTF8.GetString(ResponseStream.ToArray()); } } public TypeResponse ResponseType{get;set;} public bool Success{get;set;} public String ErrorMessage{get;set;} public HttpStatusCode StatusCode{get;set;} public String Request{get;set;} public bool IsNetErrorResponse { get { return (int)StatusCode>=400?true:false; } } public void Dispose() { if(null!=ResponseStream) { ResponseStream.Close(); ResponseStream.Dispose(); ResponseStream=null; } } } // ********************************************************************************************************************************************************************* public class HttpNetRequest { public static int REQUEST_TIMEOUT = 300000; private HttpNetRequest() { } public static HttpNetResponse GetRequestStreamZIP(String strRequest) { HttpWebResponse webResponse=null; try { MDTrace.WriteLine(LogLevel.VERBOSE,String.Format("GetRequestStreamZIP[ENTER]{0}",strRequest)); int charCount = 0; byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); webRequest.Timeout = REQUEST_TIMEOUT; webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; webRequest.Headers.Add("Accept-Language: en-US,en;q=0.5"); webRequest.Headers.Add("Accept-Encoding: gzip, deflate"); webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0"; webRequest.KeepAlive = true; webResponse = (HttpWebResponse)webRequest.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); MemoryStream memoryStream=new MemoryStream(); while (true) { charCount = responseStream.Read(buffer, 0, buffer.Length); if(charCount<=0)break; memoryStream.Write(buffer,0,charCount); } webResponse.Close(); memoryStream.Flush(); return new HttpNetResponse(memoryStream,strRequest,webResponse,true); } catch (WebException webException) { return new HttpNetResponse((HttpWebResponse)webException.Response,strRequest,false,webException.Message); } catch (Exception exception) { return new HttpNetResponse(webResponse,strRequest,false,exception.Message); } finally { MDTrace.WriteLine(LogLevel.VERBOSE,"GetRequestStreamZIP[LEAVE]"); } } public static HttpNetResponse GetRequestStreamZIPV2(String strRequest) { HttpWebResponse webResponse=null; try { MDTrace.WriteLine(LogLevel.VERBOSE,String.Format("GetRequestStreamZIPV2[ENTER]{0}",strRequest)); int charCount = 0; byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); webRequest.Timeout = REQUEST_TIMEOUT; webRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; webRequest.Headers.Add("Accept-Language: en-US,en;q=0.5"); webRequest.Headers.Add("Accept-Encoding: gzip, deflate"); webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:59.0) Gecko/20100101 Firefox/59.0"; webRequest.KeepAlive = true; webRequest.CookieContainer=new CookieContainer(); webResponse = (HttpWebResponse)webRequest.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); MemoryStream memoryStream=new MemoryStream(); while (true) { charCount = responseStream.Read(buffer, 0, buffer.Length); if(charCount<=0)break; memoryStream.Write(buffer,0,charCount); } webResponse.Close(); memoryStream.Flush(); return new HttpNetResponse(memoryStream,strRequest,webResponse,true); } catch (WebException webException) { return new HttpNetResponse((HttpWebResponse)webException.Response,strRequest,false,webException.Message); } catch (Exception exception) { return new HttpNetResponse(webResponse,strRequest,false,exception.Message); } finally { MDTrace.WriteLine(LogLevel.VERBOSE,"GetRequestStreamZIPV2[LEAVE]"); } } public static HttpNetResponse GetRequestStreamCSV(String strRequest,CookieCollection cookieCollection=null, WebProxy webProxy=null) { HttpWebResponse webResponse=null; try { ServicePointManager.Expect100Continue=true; ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3|SecurityProtocolType.Tls12|SecurityProtocolType.Tls11|SecurityProtocolType.Tls; MDTrace.WriteLine(LogLevel.VERBOSE,String.Format("GetRequestStreamCSV[ENTER]{0}",strRequest)); int charCount = 0; byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); if(null!=webProxy) { webRequest.Proxy=webProxy; } if(null!=cookieCollection) { webRequest.CookieContainer=new CookieContainer(); webRequest.CookieContainer.Add(cookieCollection); } webRequest.Timeout = REQUEST_TIMEOUT; webRequest.Accept = "text/csv,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; webRequest.Headers.Add("Accept-Language: en-US,en;q=0.5"); webRequest.Headers.Add("Accept-Encoding: gzip, deflate"); // webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0"; webRequest.KeepAlive = true; webResponse = (HttpWebResponse)webRequest.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); if(webResponse.ContentEncoding.ToLower().Contains("gzip")) { responseStream = new GZipStream(responseStream, CompressionMode.Decompress); StreamReader reader = new StreamReader(responseStream, Encoding.Default); sb.Append(reader.ReadToEnd()); reader.Close(); } else if(webResponse.ContentEncoding.ToLower().Contains("deflate")) { responseStream = new DeflateStream(responseStream, CompressionMode.Decompress); StreamReader reader = new StreamReader(responseStream, Encoding.Default); sb.Append(reader.ReadToEnd()); reader.Close(); } else { while (true) { charCount = responseStream.Read(buffer, 0, buffer.Length); if (0 == charCount) break; sb.Append(Encoding.ASCII.GetString(buffer, 0, charCount)); } } webResponse.Close(); return new HttpNetResponse(sb.ToString(),strRequest,webResponse,true); } catch (WebException webException) { return new HttpNetResponse((HttpWebResponse)webException.Response,strRequest,false,webException.Message); } catch (Exception exception) { return new HttpNetResponse(webResponse,strRequest,false,exception.Message); } finally { MDTrace.WriteLine(LogLevel.VERBOSE,"GetRequestStreamCSV[LEAVE]"); } } public static HttpNetResponse GetRequestNoEncoding(String strRequest,WebProxy webProxy=null) { HttpWebResponse webResponse=null; try { ServicePointManager.Expect100Continue = true; ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3|SecurityProtocolType.Tls12|SecurityProtocolType.Tls11|SecurityProtocolType.Tls; MDTrace.WriteLine(LogLevel.VERBOSE,String.Format("GetRequestNoEncoding[ENTER]{0}",strRequest)); int charCount = 0; byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); if(null!=webProxy)webRequest.Proxy=webProxy; webRequest.Timeout = REQUEST_TIMEOUT; webRequest.Accept = "text/html, text/csv"; webRequest.ContentType="text/csv; charset=utf-8"; webRequest.Headers.Add("Accept-Encoding: None"); webRequest.Headers.Add("Accept-Language: en-US"); webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0"; webRequest.KeepAlive = true; webResponse = (HttpWebResponse)webRequest.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); while (true) { charCount = responseStream.Read(buffer, 0, buffer.Length); if (0 >= charCount) break; sb.Append(Encoding.ASCII.GetString(buffer, 0, charCount)); } webResponse.Close(); return new HttpNetResponse(sb.ToString(),strRequest,webResponse,true); } catch (WebException webException) { return new HttpNetResponse((HttpWebResponse)webException.Response,strRequest,false,webException.Message); } catch (Exception exception) { return new HttpNetResponse(webResponse,strRequest,false,exception.Message); } finally { MDTrace.WriteLine(LogLevel.VERBOSE,"GetRequestNoEncoding[LEAVE]"); } } public static HttpNetResponse GetRequestNoEncodingV1(String strRequest,WebProxy webProxy=null) { ServicePointManager.Expect100Continue=true; ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3|SecurityProtocolType.Tls12|SecurityProtocolType.Tls11|SecurityProtocolType.Tls; HttpWebResponse webResponse=null; try { MDTrace.WriteLine(LogLevel.VERBOSE,String.Format("GetRequestNoEncoding[ENTER]{0}",strRequest)); int charCount=0; byte[] buffer=new byte[8192]; StringBuilder sb=new StringBuilder(); HttpWebRequest webRequest=(HttpWebRequest)WebRequest.Create(new Uri(strRequest)); if(null!=webProxy)webRequest.Proxy=webProxy; webRequest.Timeout=REQUEST_TIMEOUT; webRequest.Accept="text/html, text/csv"; webRequest.ContentType="text/csv; charset=utf-8"; webRequest.Headers.Add("Accept-Encoding: None"); webRequest.Headers.Add("Accept-Language: en-US"); webRequest.UserAgent="Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0"; webRequest.KeepAlive=true; webResponse=(HttpWebResponse)webRequest.GetResponse(); Stream responseStream=webResponse.GetResponseStream(); while(true) { charCount=responseStream.Read(buffer,0,buffer.Length); if(0>=charCount) break; sb.Append(Encoding.ASCII.GetString(buffer,0,charCount)); } webResponse.Close(); return new HttpNetResponse(sb.ToString(),strRequest,webResponse,true); } catch(WebException webException) { return new HttpNetResponse((HttpWebResponse)webException.Response,strRequest,false,webException.Message); } catch(Exception exception) { return new HttpNetResponse(webResponse,strRequest,false,exception.Message); } finally { MDTrace.WriteLine(LogLevel.VERBOSE,"GetRequestNoEncoding[LEAVE]"); } } public static HttpNetResponse GetRequestCSV(String strRequest,String referer) { HttpWebResponse webResponse=null; try { MDTrace.WriteLine(LogLevel.VERBOSE,String.Format("GetRequestCSV[ENTER]{0}",strRequest)); int charCount = 0; byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); webRequest.Timeout = REQUEST_TIMEOUT; webRequest.Accept="text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; webRequest.ContentType="text/csv; charset=utf-8"; webRequest.Headers.Add("Accept-Encoding: identity"); // "gzip, deflate" webRequest.Headers.Add("Accept-Language: en-US,en;q=0.5"); webRequest.Headers.Add("Upgrade-Insecure-Requests: 1"); webRequest.UserAgent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0"; webRequest.Referer=referer; webRequest.KeepAlive = true; webResponse = (HttpWebResponse)webRequest.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); while (true) { charCount = responseStream.Read(buffer, 0, buffer.Length); if (0 == charCount) break; sb.Append(Encoding.ASCII.GetString(buffer, 0, charCount)); } webResponse.Close(); return new HttpNetResponse(sb.ToString(),strRequest,webResponse,true); } catch (WebException webException) { return new HttpNetResponse((HttpWebResponse)webException.Response,strRequest,false,webException.Message); } catch (Exception exception) { return new HttpNetResponse(webResponse,strRequest,false,exception.Message); } finally { MDTrace.WriteLine(LogLevel.VERBOSE,"GetRequestCSV[LEAVE]"); } } public static HttpNetResponse GetRequestNoEncodingV2(String strRequest) { HttpWebResponse webResponse=null; try { MDTrace.WriteLine(LogLevel.VERBOSE,String.Format("GetRequestNoEncodingV2[ENTER]{0}",strRequest)); int charCount = 0; byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(strRequest)); webRequest.Timeout = REQUEST_TIMEOUT; webRequest.Headers.Add("Accept-Language: en-US,en;q=0.5"); webRequest.Headers.Add("Accept-Encoding: None"); webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0"; webRequest.Accept = "image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; webRequest.KeepAlive = true; webResponse = (HttpWebResponse)webRequest.GetResponse(); Stream responseStream = webResponse.GetResponseStream(); while (true) { charCount = responseStream.Read(buffer, 0, buffer.Length); if (0 == charCount) break; sb.Append(Encoding.ASCII.GetString(buffer, 0, charCount)); } webResponse.Close(); return new HttpNetResponse(sb.ToString(),strRequest,webResponse,true); } catch (WebException webException) { return new HttpNetResponse((HttpWebResponse)webException.Response,strRequest,false,webException.Message); } catch (Exception exception) { return new HttpNetResponse(webResponse,strRequest,false,exception.Message); } finally { MDTrace.WriteLine(LogLevel.VERBOSE,"GetRequestNoEncodingV2[LEAVE]"); } } // I am using this code specifically on the seeking alpha web site. it seems as though seeking alpha has implemeted some user agent based bot prevention mechanism on their website // to prevent scrapers. What I do here is to choose from a set of commonly used user agents and then randomly choose a user agent to code into the request. retry logic up to 3 times // and handling of 404 (not found) public static HttpNetResponse GetRequestNoEncodingV3(String strRequest,String referer=null) { HttpWebResponse webResponse=null; WebException lastWebException=null; try { MDTrace.WriteLine(LogLevel.VERBOSE,String.Format("GetRequestNoEncodingV3[ENTER]{0}",strRequest)); Random random=new Random(); String[] userAgents= { "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537." }; int MAX_RETRIES=5; int TIMEOUT_BETWEEN_RETRIES=1000; int charCount = 0; byte[] buffer = new byte[8192]; StringBuilder sb = new StringBuilder(); for(int count=0;count=userAgents.Length) index=userAgents.Length-1; String userAgent=userAgents[index]; webRequest.UserAgent=userAgent; try { webResponse=(HttpWebResponse)webRequest.GetResponse(); Stream responseStream=webResponse.GetResponseStream(); if(webResponse.ContentEncoding.ToLower().Contains("gzip")) { responseStream=new GZipStream(responseStream,CompressionMode.Decompress); StreamReader reader=new StreamReader(responseStream,Encoding.Default); sb.Append(reader.ReadToEnd()); reader.Close(); } else if(webResponse.ContentEncoding.ToLower().Contains("deflate")) { responseStream=new DeflateStream(responseStream,CompressionMode.Decompress); StreamReader reader=new StreamReader(responseStream,Encoding.Default); sb.Append(reader.ReadToEnd()); reader.Close(); } else { while(true) { charCount=responseStream.Read(buffer,0,buffer.Length); if(0==charCount) break; sb.Append(Encoding.ASCII.GetString(buffer,0,charCount)); } } webResponse.Close(); return new HttpNetResponse(sb.ToString(),strRequest,webResponse,true); } catch(WebException webException) { if(webException.Message.Contains("(404) Not Found")) return new HttpNetResponse(webResponse,strRequest,false,null==lastWebException?"":lastWebException.Message); lastWebException=webException; Thread.Sleep(TIMEOUT_BETWEEN_RETRIES); } } MDTrace.WriteLine(LogLevel.DEBUG,String.Format("General failure with {0}",lastWebException.Message)); return new HttpNetResponse(webResponse,strRequest,false,lastWebException.Message); } catch(Exception exception) { return new HttpNetResponse(webResponse,strRequest,false,exception.Message); } finally { MDTrace.WriteLine(LogLevel.VERBOSE,"GetRequestNoEncodingV3A[LEAVE]"); } } // This one uses the random user agent generator public static HttpNetResponse GetRequestNoEncodingV3B(String strRequest,CookieCollection cookieCollection=null,WebProxy webProxy=null) { HttpWebResponse webResponse=null; WebException lastWebException=null; ServicePointManager.Expect100Continue=true; ServicePointManager.SecurityProtocol=SecurityProtocolType.Ssl3|SecurityProtocolType.Tls12|SecurityProtocolType.Tls11|SecurityProtocolType.Tls; try { MDTrace.WriteLine(LogLevel.VERBOSE,String.Format("GetRequestNoEncodingV3B[ENTER]{0}",strRequest)); int MAX_RETRIES=5; int TIMEOUT_BETWEEN_RETRIES=1000; int charCount=0; byte[] buffer=new byte[8192]; StringBuilder sb=new StringBuilder(); for(int count=0;count