Added: get head response for HttpWeb

This commit is contained in:
Ray 2020-01-30 12:35:05 +00:00
parent 51a3dde03e
commit 9de46a158b

View File

@ -18,35 +18,30 @@ namespace RyzStudio.Net
{ {
} }
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)
{ {
@ -93,18 +88,59 @@ namespace RyzStudio.Net
return rv; return rv;
} }
public static HttpWebRequest AddBasicAuthentication(HttpWebRequest webRequest, string username, string password) public int GetResponse(out HttpWebRequest webRequest, out string sourceCode, string requestURL) => this.GetResponse(out webRequest, out sourceCode, requestURL, requestURL, true);
{
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, bool allowAutoRedirect) => this.GetResponse(out webRequest, out sourceCode, requestURL, requestURL, allowAutoRedirect);
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;
return this.GetResponse(out sourceCode, webRequest);
} }
public int GetHEADResponse(string requestURL) => this.GetHEADResponse(requestURL, requestURL, true);
public int GetHEADResponse(string requestURL, string referrerURL) => this.GetHEADResponse(requestURL, requestURL, true);
public int GetHEADResponse(string requestURL, bool allowAutoRedirect) => this.GetHEADResponse(requestURL, requestURL, allowAutoRedirect);
public int GetHEADResponse(string requestURL, string referrerURL, bool allowAutoRedirect)
{
HttpWebRequest webRequest = this.CreateRequest(requestURL, referrerURL);
webRequest.Method = "HEAD";
webRequest.AllowAutoRedirect = allowAutoRedirect;
string sc;
int rc = this.GetResponse(out sc, webRequest);
return rc;
}
public int GetHEADResponse(out HttpWebRequest webRequest, string requestURL) => this.GetHEADResponse(out webRequest, requestURL, requestURL, true);
public int GetHEADResponse(out HttpWebRequest webRequest, string requestURL, string referrerURL) => this.GetHEADResponse(out webRequest, requestURL, referrerURL, true);
public int GetHEADResponse(out HttpWebRequest webRequest, string requestURL, bool allowAutoRedirect) => this.GetHEADResponse(out webRequest, requestURL, requestURL, allowAutoRedirect);
public int GetHEADResponse(out HttpWebRequest webRequest, string requestURL, string referrerURL, bool allowAutoRedirect)
{
webRequest = this.CreateRequest(requestURL, referrerURL);
webRequest.Method = "HEAD";
webRequest.AllowAutoRedirect = allowAutoRedirect;
string sc;
int rc = this.GetResponse(out sc, webRequest);
return rc;
}
public int GetPOSTResponse(out string sourceCode, HttpWebRequest webRequest, string postData) public int GetPOSTResponse(out string sourceCode, HttpWebRequest webRequest, string postData)
{ {
sourceCode = ""; sourceCode = string.Empty;
int rv = 0; int rv = 0;
byte[] buffer = Encoding.UTF8.GetBytes(postData); byte[] buffer = Encoding.UTF8.GetBytes(postData);
@ -164,5 +200,14 @@ namespace RyzStudio.Net
return rv; 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;
}
} }
} }