Add GetCookies to HttpNetRequest
This commit is contained in:
@@ -3,6 +3,8 @@ using System.Text;
|
||||
using System.IO.Compression;
|
||||
using System.Configuration;
|
||||
using MarketData.Configuration;
|
||||
using MarketData.Utils;
|
||||
|
||||
// Filename: HttpRequest.cs
|
||||
// Author:Sean Kessler
|
||||
|
||||
@@ -99,12 +101,67 @@ namespace MarketData.Integration
|
||||
private HttpNetRequest()
|
||||
{
|
||||
}
|
||||
public static HttpNetResponse GetRequestStreamZIP(String strRequest)
|
||||
{
|
||||
HttpWebResponse webResponse=null;
|
||||
|
||||
public static CookieCollection GetCookies(String strRequest)
|
||||
{
|
||||
HttpWebResponse webResponse = null;
|
||||
|
||||
try
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.VERBOSE,String.Format("GetRequestStreamZIP[ENTER]{0}",strRequest));
|
||||
Uri uri = new Uri(strRequest);
|
||||
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
|
||||
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();
|
||||
CookieContainer cookieContainer = new CookieContainer();
|
||||
|
||||
if (webResponse.Headers.AllKeys.Contains("Set-Cookie"))
|
||||
{
|
||||
String[] cookies = webResponse.Headers.GetValues("Set-Cookie");
|
||||
foreach (String strCookie in cookies)
|
||||
{
|
||||
String cookieName = strCookie.Substring(0, strCookie.IndexOf('='));
|
||||
String cookieValue = Utility.BetweenString(strCookie, "=", ";");
|
||||
String metaData = strCookie.Substring(strCookie.IndexOf(';')+1);
|
||||
String[] metaDataElements = metaData.Split(';');
|
||||
String domain = metaDataElements.Where(x => x.Contains("Domain",StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
|
||||
if (default != domain) domain = Utility.BetweenString(domain, "=", null);
|
||||
String path = metaDataElements.Where(x => x.Contains("Path", StringComparison.InvariantCultureIgnoreCase)).FirstOrDefault();
|
||||
if (default != path) path = Utility.BetweenString(path, "=", null);
|
||||
Cookie cookie = default;
|
||||
if (null != path && null != domain) cookie = new Cookie(cookieName, cookieValue, path, domain);
|
||||
else if (null != path) cookie = new Cookie(cookieName, cookieValue, path);
|
||||
else cookie = new Cookie(cookieName, cookieValue);
|
||||
cookieContainer.Add(uri, cookie);
|
||||
}
|
||||
}
|
||||
return cookieContainer.GetCookies(uri);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
MDTrace.WriteLine(LogLevel.DEBUG, $"Exception {exception.ToString()}");
|
||||
return null;
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (null != webResponse)
|
||||
{
|
||||
webResponse.Close();
|
||||
webResponse.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
@@ -112,48 +169,48 @@ namespace MarketData.Integration
|
||||
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.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;
|
||||
try{webResponse = (HttpWebResponse)webRequest.GetResponse();}
|
||||
catch(WebException webException)
|
||||
try { webResponse = (HttpWebResponse)webRequest.GetResponse(); }
|
||||
catch (WebException webException)
|
||||
{
|
||||
if(IsMovedException(webException))
|
||||
if (IsMovedException(webException))
|
||||
{
|
||||
webRequest=Redirect(webRequest, webException);
|
||||
webResponse=(HttpWebResponse)webRequest.GetResponse();
|
||||
webRequest = Redirect(webRequest, webException);
|
||||
webResponse = (HttpWebResponse)webRequest.GetResponse();
|
||||
}
|
||||
else throw;
|
||||
}
|
||||
Stream responseStream = webResponse.GetResponseStream();
|
||||
MemoryStream memoryStream=new MemoryStream();
|
||||
MemoryStream memoryStream = new MemoryStream();
|
||||
while (true)
|
||||
{
|
||||
charCount = responseStream.Read(buffer, 0, buffer.Length);
|
||||
if(charCount<=0)break;
|
||||
memoryStream.Write(buffer,0,charCount);
|
||||
if (charCount <= 0) break;
|
||||
memoryStream.Write(buffer, 0, charCount);
|
||||
}
|
||||
memoryStream.Flush();
|
||||
return new HttpNetResponse(memoryStream,strRequest,webResponse,true);
|
||||
memoryStream.Flush();
|
||||
return new HttpNetResponse(memoryStream, strRequest, webResponse, true);
|
||||
}
|
||||
catch (WebException webException)
|
||||
{
|
||||
return new HttpNetResponse((HttpWebResponse)webException.Response,strRequest,false,webException.Message);
|
||||
return new HttpNetResponse((HttpWebResponse)webException.Response, strRequest, false, webException.Message);
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
return new HttpNetResponse(webResponse,strRequest,false,exception.Message);
|
||||
return new HttpNetResponse(webResponse, strRequest, false, exception.Message);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if(null!=webResponse)
|
||||
{
|
||||
if (null != webResponse)
|
||||
{
|
||||
webResponse.Close();
|
||||
webResponse.Dispose();
|
||||
}
|
||||
MDTrace.WriteLine(LogLevel.VERBOSE,"GetRequestStreamZIP[LEAVE]");
|
||||
MDTrace.WriteLine(LogLevel.VERBOSE, "GetRequestStreamZIP[LEAVE]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static HttpNetResponse GetRequestStreamZIPV2(String strRequest)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user