2021-09-07 11:32:24 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Net;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
|
|
|
|
namespace RyzStudio.Net
|
|
|
|
|
{
|
|
|
|
|
public class HttpWeb
|
|
|
|
|
{
|
2021-10-12 18:55:00 +00:00
|
|
|
|
|
2021-09-07 11:32:24 +00:00
|
|
|
|
|
|
|
|
|
public HttpWeb()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 18:55:00 +00:00
|
|
|
|
|
|
|
|
|
public string UserAgent { get; set; } = "Momozilla/5.0 (" + Environment.OSVersion.Platform.ToString() + " ; " + Environment.OSVersion.VersionString + "; " + Application.CurrentCulture.TwoLetterISOLanguageName + ")";
|
|
|
|
|
|
|
|
|
|
public int Timeout { get; set; } = 6000;
|
|
|
|
|
|
|
|
|
|
public int MaxRedirect { get; set; } = 8;
|
|
|
|
|
|
|
|
|
|
public bool AllowRedirect { get; set; } = true;
|
|
|
|
|
|
|
|
|
|
public bool IgnoreSSL { get; set; } = false;
|
|
|
|
|
|
|
|
|
|
public CookieContainer CookierJar { get; set; } = null;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public HttpWebRequest CreateRequest(string url) => this.CreateRequest(url, url);
|
2021-09-07 11:32:24 +00:00
|
|
|
|
|
|
|
|
|
public HttpWebRequest CreateRequest(string url, string referrerURL)
|
|
|
|
|
{
|
2021-10-12 18:55:00 +00:00
|
|
|
|
if (this.CookierJar == null) this.CookierJar = new CookieContainer();
|
2021-09-07 11:32:24 +00:00
|
|
|
|
|
|
|
|
|
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
|
webRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
|
2021-10-12 18:55:00 +00:00
|
|
|
|
webRequest.MaximumAutomaticRedirections = this.MaxRedirect;
|
|
|
|
|
webRequest.CookieContainer = this.CookierJar;
|
|
|
|
|
webRequest.UserAgent = this.UserAgent;
|
|
|
|
|
webRequest.AllowAutoRedirect = this.AllowRedirect;
|
|
|
|
|
webRequest.Timeout = this.Timeout;
|
|
|
|
|
|
|
|
|
|
if (this.IgnoreSSL) webRequest.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;
|
2021-09-07 11:32:24 +00:00
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
2021-10-12 18:55:00 +00:00
|
|
|
|
|
2021-09-07 11:32:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|