DocumentRequest
Represents the arguments to load a document.
using AngleSharp.Dom;
using AngleSharp.Html;
using System;
using System.Collections.Generic;
using System.IO;
namespace AngleSharp.Network
{
public class DocumentRequest
{
public INode Source { get; set; }
public Url Target { get; set; }
public string Referer {
get {
return GetHeader(HeaderNames.Referer);
}
set {
SetHeader(HeaderNames.Referer, value);
}
}
public HttpMethod Method { get; set; }
public Stream Body { get; set; }
public string MimeType {
get {
return GetHeader(HeaderNames.ContentType);
}
set {
SetHeader(HeaderNames.ContentType, value);
}
}
public Dictionary<string, string> Headers { get; set; }
public DocumentRequest(Url target)
{
if (target == null)
throw new ArgumentNullException("target");
Headers = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
Target = target;
Method = HttpMethod.Get;
Body = Stream.Null;
}
public static DocumentRequest Get(Url target, INode source = null, string referer = null)
{
DocumentRequest documentRequest = new DocumentRequest(target);
documentRequest.Method = HttpMethod.Get;
documentRequest.Referer = referer;
documentRequest.Source = source;
return documentRequest;
}
public static DocumentRequest Post(Url target, Stream body, string type, INode source = null, string referer = null)
{
if (body == null)
throw new ArgumentNullException("body");
if (type == null)
throw new ArgumentNullException("type");
DocumentRequest documentRequest = new DocumentRequest(target);
documentRequest.Method = HttpMethod.Post;
documentRequest.Body = body;
documentRequest.MimeType = type;
documentRequest.Referer = referer;
documentRequest.Source = source;
return documentRequest;
}
public static DocumentRequest PostAsPlaintext(Url target, IDictionary<string, string> fields)
{
if (fields == null)
throw new ArgumentNullException("fields");
FormDataSet formDataSet = new FormDataSet();
foreach (KeyValuePair<string, string> field in fields) {
formDataSet.Append(field.Key, field.Value, InputTypeNames.Text);
}
return Post(target, formDataSet.AsPlaintext(null), MimeTypeNames.Plain, null, null);
}
public static DocumentRequest PostAsUrlencoded(Url target, IDictionary<string, string> fields)
{
if (fields == null)
throw new ArgumentNullException("fields");
FormDataSet formDataSet = new FormDataSet();
foreach (KeyValuePair<string, string> field in fields) {
formDataSet.Append(field.Key, field.Value, InputTypeNames.Text);
}
return Post(target, formDataSet.AsUrlEncoded(null), MimeTypeNames.UrlencodedForm, null, null);
}
private void SetHeader(string name, string value)
{
Headers[name] = value;
}
private string GetHeader(string name)
{
string value = null;
if (Headers.TryGetValue(name, out value))
return value;
return null;
}
}
}