From a0e2808e77bc9e20957e201d78c5fe2369ad8c9e Mon Sep 17 00:00:00 2001 From: Sean Date: Thu, 6 Nov 2025 20:56:06 -0500 Subject: [PATCH] Add GetCookies to HttpNetRequest --- .../Integration/HttpNetRequest.cs | 99 +++++++++++++++---- 1 file changed, 78 insertions(+), 21 deletions(-) diff --git a/MarketData/MarketDataLib/Integration/HttpNetRequest.cs b/MarketData/MarketDataLib/Integration/HttpNetRequest.cs index 74d9ad5..5ef8b40 100755 --- a/MarketData/MarketDataLib/Integration/HttpNetRequest.cs +++ b/MarketData/MarketDataLib/Integration/HttpNetRequest.cs @@ -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) {