Fixed: replaced web client

This commit is contained in:
Ray 2019-04-24 22:06:52 +01:00
parent b3303a830b
commit d0471a03a8
6 changed files with 197 additions and 11 deletions

View File

@ -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 = "")

View File

@ -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")]

View File

@ -89,7 +89,7 @@ namespace bzit.bomg.Properties {
}
/// <summary>
/// Looks up a localized string similar to 0.3.0.012 beta.
/// Looks up a localized string similar to 0.3.0.013 beta.
/// </summary>
internal static string app_version {
get {

View File

@ -128,7 +128,7 @@
<value>Bookmark Manager</value>
</data>
<data name="app_version" xml:space="preserve">
<value>0.3.0.012 beta</value>
<value>0.3.0.013 beta</value>
</data>
<data name="arrow_down_circle" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Resources\2\arrow-down-circle.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>

168
RyzStudio/Net/HttpWeb.cs Normal file
View File

@ -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;
}
}
}

View File

@ -106,6 +106,7 @@
<DependentUpon>OptionForm.cs</DependentUpon>
</Compile>
<Compile Include="Models\BookmarkItemModel.cs" />
<Compile Include="RyzStudio\Net\HttpWeb.cs" />
<Compile Include="Windows\Forms\BookmarkTreeViewSNode.cs" />
<Compile Include="RyzStudio\Windows\Forms\HorizontalSeparator.cs">
<SubType>UserControl</SubType>