Added: get head response for HttpWeb
This commit is contained in:
parent
51a3dde03e
commit
9de46a158b
@ -6,163 +6,208 @@ using System.Windows.Forms;
|
|||||||
|
|
||||||
namespace RyzStudio.Net
|
namespace RyzStudio.Net
|
||||||
{
|
{
|
||||||
public class HttpWeb
|
public class HttpWeb
|
||||||
{
|
{
|
||||||
public string defaultUserAgent = "Momozilla/5.0 (" + Environment.OSVersion.Platform.ToString() + " ; " + Environment.OSVersion.VersionString + "; " + Application.CurrentCulture.TwoLetterISOLanguageName + ")";
|
public string defaultUserAgent = "Momozilla/5.0 (" + Environment.OSVersion.Platform.ToString() + " ; " + Environment.OSVersion.VersionString + "; " + Application.CurrentCulture.TwoLetterISOLanguageName + ")";
|
||||||
public int defaultTimeout = 6000;
|
public int defaultTimeout = 6000;
|
||||||
public int defaultMaxRedirect = 8;
|
public int defaultMaxRedirect = 8;
|
||||||
public bool defaultAllowRedirect = true;
|
public bool defaultAllowRedirect = true;
|
||||||
public CookieContainer defaultCookierContainer = null;
|
public CookieContainer defaultCookierContainer = null;
|
||||||
|
|
||||||
public HttpWeb()
|
public HttpWeb()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public HttpWebRequest CreateRequest(string url)
|
public HttpWebRequest CreateRequest(string requestURL) => this.CreateRequest(requestURL, requestURL);
|
||||||
{
|
|
||||||
return this.CreateRequest(url, url);
|
|
||||||
}
|
|
||||||
|
|
||||||
public HttpWebRequest CreateRequest(string url, string referrerURL)
|
public HttpWebRequest CreateRequest(string requestURL, string referrerURL)
|
||||||
{
|
{
|
||||||
if (defaultCookierContainer == null)
|
if (defaultCookierContainer == null)
|
||||||
{
|
{
|
||||||
defaultCookierContainer = new CookieContainer();
|
defaultCookierContainer = new CookieContainer();
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
|
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(requestURL);
|
||||||
webRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
|
webRequest.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
|
||||||
webRequest.MaximumAutomaticRedirections = defaultMaxRedirect;
|
webRequest.MaximumAutomaticRedirections = defaultMaxRedirect;
|
||||||
webRequest.CookieContainer = defaultCookierContainer;
|
webRequest.CookieContainer = defaultCookierContainer;
|
||||||
webRequest.UserAgent = defaultUserAgent;
|
webRequest.UserAgent = defaultUserAgent;
|
||||||
webRequest.AllowAutoRedirect = defaultAllowRedirect;
|
webRequest.AllowAutoRedirect = defaultAllowRedirect;
|
||||||
webRequest.Timeout = defaultTimeout;
|
webRequest.Timeout = defaultTimeout;
|
||||||
|
webRequest.Referer = referrerURL;
|
||||||
|
|
||||||
return webRequest;
|
return webRequest;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int GetResponse(out string sourceCode, string url, string referrerURL = "")
|
public int GetResponse(out string sourceCode, string requestURL) => this.GetResponse(out sourceCode, this.CreateRequest(requestURL));
|
||||||
{
|
|
||||||
HttpWebRequest webRequest = this.CreateRequest(url, referrerURL);
|
|
||||||
|
|
||||||
return GetResponse(out sourceCode, webRequest);
|
public int GetResponse(out string sourceCode, string requestURL, string referrerURL) => this.GetResponse(out sourceCode, this.CreateRequest(requestURL, referrerURL));
|
||||||
}
|
|
||||||
|
|
||||||
public int GetResponse(out string sourceCode, HttpWebRequest webRequest)
|
public int GetResponse(out string sourceCode, HttpWebRequest webRequest)
|
||||||
{
|
{
|
||||||
sourceCode = string.Empty;
|
sourceCode = string.Empty;
|
||||||
|
|
||||||
int rv = 0;
|
int rv = 0;
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
|
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
|
||||||
|
|
||||||
rv = (int)webResponse.StatusCode;
|
rv = (int)webResponse.StatusCode;
|
||||||
|
|
||||||
StreamReader readContent = new StreamReader(webResponse.GetResponseStream());
|
StreamReader readContent = new StreamReader(webResponse.GetResponseStream());
|
||||||
sourceCode = readContent.ReadToEnd();
|
sourceCode = readContent.ReadToEnd();
|
||||||
|
|
||||||
webResponse.Close();
|
webResponse.Close();
|
||||||
webResponse = null;
|
webResponse = null;
|
||||||
}
|
}
|
||||||
catch (WebException xc)
|
catch (WebException xc)
|
||||||
{
|
{
|
||||||
if (xc.Response is HttpWebResponse)
|
if (xc.Response is HttpWebResponse)
|
||||||
{
|
{
|
||||||
HttpWebResponse rs = xc.Response as HttpWebResponse;
|
HttpWebResponse rs = xc.Response as HttpWebResponse;
|
||||||
StreamReader readContent = new StreamReader(rs.GetResponseStream());
|
StreamReader readContent = new StreamReader(rs.GetResponseStream());
|
||||||
if (readContent != null)
|
if (readContent != null)
|
||||||
{
|
{
|
||||||
sourceCode = readContent.ReadToEnd();
|
sourceCode = readContent.ReadToEnd();
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = (int)rs.StatusCode;
|
rv = (int)rs.StatusCode;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
rv = (int)xc.Status;
|
rv = (int)xc.Status;
|
||||||
sourceCode = xc.Message;
|
sourceCode = xc.Message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception xc)
|
catch (Exception xc)
|
||||||
{
|
{
|
||||||
sourceCode = xc.Message;
|
sourceCode = xc.Message;
|
||||||
}
|
}
|
||||||
|
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int GetResponse(out HttpWebRequest webRequest, out string sourceCode, string requestURL) => this.GetResponse(out webRequest, out sourceCode, requestURL, requestURL, true);
|
||||||
|
|
||||||
public static HttpWebRequest AddBasicAuthentication(HttpWebRequest webRequest, string username, string password)
|
public int GetResponse(out HttpWebRequest webRequest, out string sourceCode, string requestURL, bool allowAutoRedirect) => this.GetResponse(out webRequest, out sourceCode, requestURL, requestURL, allowAutoRedirect);
|
||||||
{
|
|
||||||
webRequest.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes(string.Concat(username, ":", password)));
|
|
||||||
webRequest.PreAuthenticate = true;
|
|
||||||
|
|
||||||
return webRequest;
|
public int GetResponse(out HttpWebRequest webRequest, out string sourceCode, string requestURL, string referrerURL) => this.GetResponse(out webRequest, out sourceCode, requestURL, referrerURL, true);
|
||||||
}
|
|
||||||
|
|
||||||
|
public int GetResponse(out HttpWebRequest webRequest, out string sourceCode, string requestURL, string referrerURL, bool allowAutoRedirect)
|
||||||
|
{
|
||||||
|
webRequest = this.CreateRequest(requestURL, referrerURL);
|
||||||
|
webRequest.AllowAutoRedirect = allowAutoRedirect;
|
||||||
|
|
||||||
public int GetPOSTResponse(out string sourceCode, HttpWebRequest webRequest, string postData)
|
return this.GetResponse(out sourceCode, webRequest);
|
||||||
{
|
}
|
||||||
sourceCode = "";
|
|
||||||
int rv = 0;
|
|
||||||
byte[] buffer = Encoding.UTF8.GetBytes(postData);
|
|
||||||
|
|
||||||
webRequest.ContentLength = buffer.Length;
|
public int GetHEADResponse(string requestURL) => this.GetHEADResponse(requestURL, requestURL, true);
|
||||||
|
|
||||||
try
|
public int GetHEADResponse(string requestURL, string referrerURL) => this.GetHEADResponse(requestURL, requestURL, true);
|
||||||
{
|
|
||||||
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 GetHEADResponse(string requestURL, bool allowAutoRedirect) => this.GetHEADResponse(requestURL, requestURL, allowAutoRedirect);
|
||||||
}
|
|
||||||
|
|
||||||
public int GetHeader(out WebHeaderCollection headerCollection, string url, string referrerURL = "")
|
public int GetHEADResponse(string requestURL, string referrerURL, bool allowAutoRedirect)
|
||||||
{
|
{
|
||||||
headerCollection = null;
|
HttpWebRequest webRequest = this.CreateRequest(requestURL, referrerURL);
|
||||||
|
webRequest.Method = "HEAD";
|
||||||
|
webRequest.AllowAutoRedirect = allowAutoRedirect;
|
||||||
|
|
||||||
int rv = 0;
|
string sc;
|
||||||
|
int rc = this.GetResponse(out sc, webRequest);
|
||||||
|
|
||||||
HttpWebRequest webRequest = this.CreateRequest(url, referrerURL);
|
return rc;
|
||||||
webRequest.Method = "HEAD";
|
}
|
||||||
|
|
||||||
try
|
public int GetHEADResponse(out HttpWebRequest webRequest, string requestURL) => this.GetHEADResponse(out webRequest, requestURL, requestURL, true);
|
||||||
{
|
|
||||||
HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse();
|
|
||||||
headerCollection = webResponse.Headers;
|
|
||||||
|
|
||||||
rv = (int)webResponse.StatusCode;
|
public int GetHEADResponse(out HttpWebRequest webRequest, string requestURL, string referrerURL) => this.GetHEADResponse(out webRequest, requestURL, referrerURL, true);
|
||||||
|
|
||||||
webResponse.Close();
|
public int GetHEADResponse(out HttpWebRequest webRequest, string requestURL, bool allowAutoRedirect) => this.GetHEADResponse(out webRequest, requestURL, requestURL, allowAutoRedirect);
|
||||||
webResponse = null;
|
|
||||||
}
|
|
||||||
catch (WebException xc)
|
|
||||||
{
|
|
||||||
if (xc.Response is HttpWebResponse)
|
|
||||||
{
|
|
||||||
HttpWebResponse rs = xc.Response as HttpWebResponse;
|
|
||||||
|
|
||||||
rv = (int)rs.StatusCode;
|
public int GetHEADResponse(out HttpWebRequest webRequest, string requestURL, string referrerURL, bool allowAutoRedirect)
|
||||||
}
|
{
|
||||||
else
|
webRequest = this.CreateRequest(requestURL, referrerURL);
|
||||||
{
|
webRequest.Method = "HEAD";
|
||||||
rv = (int)xc.Status;
|
webRequest.AllowAutoRedirect = allowAutoRedirect;
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (Exception xc)
|
|
||||||
{
|
|
||||||
// do nothing
|
|
||||||
}
|
|
||||||
|
|
||||||
return rv;
|
string sc;
|
||||||
}
|
int rc = this.GetResponse(out sc, webRequest);
|
||||||
}
|
|
||||||
|
return rc;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int GetPOSTResponse(out string sourceCode, HttpWebRequest webRequest, string postData)
|
||||||
|
{
|
||||||
|
sourceCode = string.Empty;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user