From d0471a03a88091b1401e12b2d981928793979325 Mon Sep 17 00:00:00 2001 From: Ray Date: Wed, 24 Apr 2019 22:06:52 +0100 Subject: [PATCH] Fixed: replaced web client --- Models/BookmarkItemModel.cs | 33 ++++-- Properties/AssemblyInfo.cs | 2 +- Properties/Resources.Designer.cs | 2 +- Properties/Resources.resx | 2 +- RyzStudio/Net/HttpWeb.cs | 168 +++++++++++++++++++++++++++++++ bomg.csproj | 1 + 6 files changed, 197 insertions(+), 11 deletions(-) create mode 100644 RyzStudio/Net/HttpWeb.cs diff --git a/Models/BookmarkItemModel.cs b/Models/BookmarkItemModel.cs index 8aa2de0..fe98ab6 100644 --- a/Models/BookmarkItemModel.cs +++ b/Models/BookmarkItemModel.cs @@ -1,5 +1,6 @@ using bzit.bomg.Models; using HtmlAgilityPack; +using RyzStudio.Net; using System; using System.Drawing; using System.IO; @@ -11,6 +12,8 @@ namespace bzit.bomg { public class BookmarkItemModel : BookmarkItemViewModel { + protected HttpWeb webClient = null; + public BookmarkItemModel() { this.Clear(); @@ -147,17 +150,31 @@ namespace bzit.bomg protected string retrieveSourceCode() { - WebClient webClient = new WebClient(); - webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore); + if (webClient == null) + { + webClient = new HttpWeb(); + } - try + string sourceCode; + int statusCode = webClient.GetResponse(out sourceCode, this.SiteAddress); + if ((statusCode == 200) || (statusCode == 301) || (statusCode == 302)) { - return webClient.DownloadString(this.SiteAddress); - } - catch - { - return null; + return sourceCode; } + + return null; + + //WebClient webClient = new WebClient(); + //webClient.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore); + + //try + //{ + // return webClient.DownloadString(this.SiteAddress); + //} + //catch (Exception exc) + //{ + // return null; + //} } protected string parseTagValue(HtmlDocument doc, string xpath, string defaultValue = "") diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 154548b..56a6f4f 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -32,5 +32,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.3.0.012")] +[assembly: AssemblyVersion("0.3.0.013")] [assembly: AssemblyFileVersion("0.1.0.0")] diff --git a/Properties/Resources.Designer.cs b/Properties/Resources.Designer.cs index 30474e0..4a5ce0b 100644 --- a/Properties/Resources.Designer.cs +++ b/Properties/Resources.Designer.cs @@ -89,7 +89,7 @@ namespace bzit.bomg.Properties { } /// - /// Looks up a localized string similar to 0.3.0.012 beta. + /// Looks up a localized string similar to 0.3.0.013 beta. /// internal static string app_version { get { diff --git a/Properties/Resources.resx b/Properties/Resources.resx index 7053999..0e30a35 100644 --- a/Properties/Resources.resx +++ b/Properties/Resources.resx @@ -128,7 +128,7 @@ Bookmark Manager - 0.3.0.012 beta + 0.3.0.013 beta ..\Resources\2\arrow-down-circle.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a diff --git a/RyzStudio/Net/HttpWeb.cs b/RyzStudio/Net/HttpWeb.cs new file mode 100644 index 0000000..44e3622 --- /dev/null +++ b/RyzStudio/Net/HttpWeb.cs @@ -0,0 +1,168 @@ +using System; +using System.IO; +using System.Net; +using System.Text; +using System.Windows.Forms; + +namespace RyzStudio.Net +{ + public class HttpWeb + { + public string defaultUserAgent = "Momozilla/5.0 (" + Environment.OSVersion.Platform.ToString() + " ; " + Environment.OSVersion.VersionString + "; " + Application.CurrentCulture.TwoLetterISOLanguageName + ")"; + public int defaultTimeout = 6000; + public int defaultMaxRedirect = 8; + public bool defaultAllowRedirect = true; + public CookieContainer defaultCookierContainer = null; + + public HttpWeb() + { + } + + public HttpWebRequest CreateRequest(string url) + { + return this.CreateRequest(url, url); + } + + public HttpWebRequest CreateRequest(string url, string referrerURL) + { + if (defaultCookierContainer == null) + { + defaultCookierContainer = new CookieContainer(); + } + + HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url); + webRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore); + webRequest.MaximumAutomaticRedirections = defaultMaxRedirect; + webRequest.CookieContainer = defaultCookierContainer; + webRequest.UserAgent = defaultUserAgent; + webRequest.AllowAutoRedirect = defaultAllowRedirect; + webRequest.Timeout = defaultTimeout; + + return webRequest; + } + + public int GetResponse(out string sourceCode, string url, string referrerURL = "") + { + HttpWebRequest webRequest = this.CreateRequest(url, referrerURL); + + return GetResponse(out sourceCode, webRequest); + } + + public int GetResponse(out string sourceCode, HttpWebRequest webRequest) + { + sourceCode = string.Empty; + + int rv = 0; + + try + { + HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); + + rv = (int)webResponse.StatusCode; + + StreamReader readContent = new StreamReader(webResponse.GetResponseStream()); + sourceCode = readContent.ReadToEnd(); + + webResponse.Close(); + webResponse = null; + } + catch (WebException xc) + { + if (xc.Response is HttpWebResponse) + { + HttpWebResponse rs = xc.Response as HttpWebResponse; + StreamReader readContent = new StreamReader(rs.GetResponseStream()); + if (readContent != null) + { + sourceCode = readContent.ReadToEnd(); + } + + rv = (int)rs.StatusCode; + } + else + { + rv = (int)xc.Status; + sourceCode = xc.Message; + } + } + catch (Exception xc) + { + sourceCode = xc.Message; + } + + return rv; + } + + public static HttpWebRequest AddBasicAuthentication(HttpWebRequest webRequest, string username, string password) + { + webRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(string.Concat(username, ":", password))); + webRequest.PreAuthenticate = true; + + return webRequest; + } + + + public int GetPOSTResponse(out string sourceCode, HttpWebRequest webRequest, string postData) + { + sourceCode = ""; + int rv = 0; + byte[] buffer = Encoding.UTF8.GetBytes(postData); + + webRequest.ContentLength = buffer.Length; + + try + { + Stream dataStream = webRequest.GetRequestStream(); + dataStream.Write(buffer, 0, buffer.Length); + dataStream.Close(); + } + catch (Exception xc) + { + sourceCode = xc.Message; + return rv; + } + + return this.GetResponse(out sourceCode, webRequest); + } + + public int GetHeader(out WebHeaderCollection headerCollection, string url, string referrerURL = "") + { + headerCollection = null; + + int rv = 0; + + HttpWebRequest webRequest = this.CreateRequest(url, referrerURL); + webRequest.Method = "HEAD"; + + try + { + HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse(); + headerCollection = webResponse.Headers; + + rv = (int)webResponse.StatusCode; + + webResponse.Close(); + webResponse = null; + } + catch (WebException xc) + { + if (xc.Response is HttpWebResponse) + { + HttpWebResponse rs = xc.Response as HttpWebResponse; + + rv = (int)rs.StatusCode; + } + else + { + rv = (int)xc.Status; + } + } + catch (Exception xc) + { + // do nothing + } + + return rv; + } + } +} \ No newline at end of file diff --git a/bomg.csproj b/bomg.csproj index 53f17ab..968d32a 100644 --- a/bomg.csproj +++ b/bomg.csproj @@ -106,6 +106,7 @@ OptionForm.cs + UserControl