本文整理汇总了C#中System.Net.WebHeaderCollection类的典型用法代码示例。如果您正苦于以下问题:C# WebHeaderCollection类的具体用法?C# WebHeaderCollection怎么用?C# WebHeaderCollection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
WebHeaderCollection类属于System.Net命名空间,在下文中一共展示了WebHeaderCollection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetHttp
private Http GetHttp()
{
Http http = new Http();
//配置proxy
WebRequest.DefaultWebProxy = GlobalSetting.Proxy;
//配置cookie
if (GlobalSetting.HttpCookie != null)
{
http.Cookies = GlobalSetting.HttpCookie;
}
//配置HttpHeader
if (GlobalSetting.HttpHeader != null)
{
//由于传递过去的Headers的值可能发生变化,所以这里需要完全拷贝一份。
WebHeaderCollection tmpHeader = new WebHeaderCollection { GlobalSetting.HttpHeader };
http.Headers = tmpHeader;
}
//配置UserAgent
if (GlobalSetting.UserAgent != null)
{
int index = new Random().Next(0,GlobalSetting.UserAgent.Count);
http.Headers.Add(HttpRequestHeader.UserAgent, GlobalSetting.UserAgent[index]);
}
return http;
}
示例2: UpdateHttpHeader
public void UpdateHttpHeader(WebHeaderCollection headers, Uri requestUri, string requestMethod)
{
if (headers != null && !string.IsNullOrEmpty(accessToken))
{
headers.Add("Authorization", string.Format("Bearer {0}", accessToken));
}
}
示例3: GetAccessToken
public bool GetAccessToken(string code)
{
Dictionary<string, string> args = new Dictionary<string, string>();
args.Add("client_id", AuthInfo.Client_ID);
args.Add("client_secret", AuthInfo.Client_Secret);
args.Add("code", code);
WebHeaderCollection headers = new WebHeaderCollection();
headers.Add("Accept", "application/json");
string response = SendPostRequest("https://github.com/login/oauth/access_token", args, headers: headers);
if (!string.IsNullOrEmpty(response))
{
OAuth2Token token = JsonConvert.DeserializeObject<OAuth2Token>(response);
if (token != null && !string.IsNullOrEmpty(token.access_token))
{
AuthInfo.Token = token;
return true;
}
}
return false;
}
示例4: GetRemoteNzbFilename
private string GetRemoteNzbFilename(WebHeaderCollection whc)
{
try
{
foreach (string key in whc.Keys)
{
string value = whc.GetValues(key)[0];
if (key == "X-DNZB-Name")
{
return value;
}
if (key == "Content-Disposition")
{
int start = value.IndexOf("filename=") + 9;
int end = value.IndexOf(".nzb");
int length = end - start;
return value.Substring(start, length).TrimStart('\"', ' ').TrimEnd('\"', ' ');
}
}
}
catch (Exception)
{
return "unknown";
}
return "unknown";
}
示例5: NoHeadersAdded
public void NoHeadersAdded()
{
WebHeaderCollection headers = new WebHeaderCollection();
HttpAuthenticationHeader authenticationHeader = new HttpAuthenticationHeader(headers);
Assert.AreEqual(String.Empty, authenticationHeader.AuthenticationType);
}
示例6: DefaultPropertyValues_ReturnEmptyAfterConstruction_Success
public void DefaultPropertyValues_ReturnEmptyAfterConstruction_Success()
{
WebHeaderCollection w = new WebHeaderCollection();
Assert.Equal(0, w.AllKeys.Length);
Assert.Equal(0, w.Count);
Assert.Equal("\r\n", w.ToString());
}
示例7: GetHttpResponse
public HttpResponseMessage GetHttpResponse(Uri uri, string method, WebHeaderCollection headers)
{
WebRequest request = WebRequest.Create(uri);
request.Method = method;
if (headers != null)
{
foreach (string name in headers)
{
request.Headers.Add(name, headers[name]);
}
}
using (WebResponse respFromServer = request.GetResponse())
{
using (Stream dataStream = respFromServer.GetResponseStream())
{
MemoryStream ms = new MemoryStream();
dataStream.CopyTo(ms);
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
ms.Position = 0;
response.Content = new StreamContent(ms);
response.Content.Headers.Add("Content-Type", respFromServer.ContentType);
return response;
}
}
}
示例8: formatOutput
public string formatOutput(WebHeaderCollection input)
{
var query = input.Keys.Cast<string>().Select(x => x + " >>>> " + input[x]).ToList();
return string.Join(Environment.NewLine, query);
}
示例9: WebResponse
protected WebResponse(HttpStatusCode statusCode, string content, string contentType)
{
StatusCode = statusCode;
Content = content;
ContentType = contentType;
additionalHeaders = new WebHeaderCollection();
}
示例10: SimpleRequest
private static XmlDocument SimpleRequest(string method, ref Uri uri, HttpRequestFilter filter, out WebHeaderCollection responseHeaders, params string[] parameters)
{
string absUri = UrlHelper.SafeToAbsoluteUri(uri);
if (parameters.Length > 0)
{
FormData formData = new FormData(true, parameters);
if (absUri.IndexOf('?') == -1)
absUri += "?" + formData.ToString();
else
absUri += "&" + formData.ToString();
}
RedirectHelper.SimpleRequest simpleRequest = new RedirectHelper.SimpleRequest(method, filter);
HttpWebResponse response = RedirectHelper.GetResponse(absUri, new RedirectHelper.RequestFactory(simpleRequest.Create));
try
{
uri = response.ResponseUri;
responseHeaders = response.Headers;
return ParseXmlResponse(response);
}
finally
{
if (response != null)
response.Close();
}
}
示例11: CanLogMessage
public void CanLogMessage()
{
// Arrange
var id = _fixture.Create<int>().ToString(CultureInfo.InvariantCulture);
var logId = _fixture.Create<Guid>();
Uri actualUri = null;
string actualData = null;
var requestHeaders = new WebHeaderCollection();
var webClientMock = new Mock<IWebClient>();
webClientMock
.Setup(x => x.Post(It.IsAny<WebHeaderCollection>(), It.IsAny<Uri>(), It.IsAny<string>(), It.IsAny<Func<WebHeaderCollection, string, string>>()))
.Callback<WebHeaderCollection, Uri, string, Func<WebHeaderCollection, string, string>>((headers, uri, data, resultor) => { requestHeaders = headers; actualUri = uri; actualData = data; })
.Returns(Task.FromResult("https://elmah.io/api/v2/messages?id=" + id + "&logid=" + logId));
var logger = new Logger(logId, null, webClientMock.Object);
var message = _fixture.Create<Message>();
// Act
var result = logger.Log(message);
// Assert
Assert.That(result, Is.EqualTo(id));
Assert.That(requestHeaders[HttpRequestHeader.ContentType], Is.EqualTo("application/json"));
Assert.That(actualUri.AbsoluteUri, Is.Not.Null.And.StringEnding(string.Format("api/v2/messages?logId={0}", logId)));
Assert.That(actualData, Is.Not.Null);
Assert.That(actualData, Is.StringContaining(message.Title));
Assert.That(actualData, Is.StringContaining(message.Severity.ToString()));
}
示例12: HttpResponse
/// <summary>
/// Constructor
/// </summary>
/// <param name="url">Url</param>
/// <param name="headers">Http headers</param>
/// <param name="status">Response status code</param>
/// <param name="body">Response raw body</param>
public HttpResponse(string url, WebHeaderCollection headers, int status, byte[] body)
{
Url = url;
Headers = headers;
Status = status;
Body = body;
}
示例13: InitializeHttpWebResponse
public static void InitializeHttpWebResponse(HttpStatusCode status, string statusDescription, int contentLength)
{
SerializationInfo = new SerializationInfo(typeof(HttpWebResponse), new FormatterConverter());
//StreamingContext sc = new StreamingContext();
WebHeaderCollection headers = new WebHeaderCollection();
SerializationInfo.AddValue("m_HttpResponseHeaders", headers);
SerializationInfo.AddValue("uri", new Uri("http://example.com"));
SerializationInfo.AddValue("m_Uri", new Uri("http://example.com"));
SerializationInfo.AddValue("m_Certificate", null);
SerializationInfo.AddValue("version", HttpVersion.Version11);
SerializationInfo.AddValue("m_Version", HttpVersion.Version11);
SerializationInfo.AddValue("statusCode", status);
SerializationInfo.AddValue("m_StatusCode", status);
SerializationInfo.AddValue("contentLength", contentLength);
SerializationInfo.AddValue("m_ContentLength", contentLength);
SerializationInfo.AddValue("contentType", "");
SerializationInfo.AddValue("m_ContentType", "");
SerializationInfo.AddValue("method", "GET");
SerializationInfo.AddValue("m_Verb", "GET");
SerializationInfo.AddValue("statusDescription", statusDescription);
SerializationInfo.AddValue("m_StatusDescription", statusDescription);
SerializationInfo.AddValue("m_MediaType", null);
SerializationInfo.AddValue("m_ConnectStream", null, typeof(Stream));
SerializationInfo.AddValue("cookieCollection", null, typeof(CookieCollection));
}
示例14: HeaderInfo
public static WebHeaderCollection HeaderInfo(String str)
{
TextWriter tw = new StreamWriter(@"C:\Users\Joe\Desktop\Header.xml");
tw.WriteLine("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
WebHeaderCollection CC = new WebHeaderCollection();
HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(str);
Req.Proxy = null;
Req.UseDefaultCredentials = true;
//YOU MUST ASSIGN A COOKIE CONTAINER FOR THE REQUEST TO PULL THE COOKIES
//Req.Headers = CC;
HttpWebResponse Res = (HttpWebResponse)Req.GetResponse();
//DUMP THE COOKIES
tw.WriteLine("<HEADERS>");
if (Res.Headers != null && Res.Headers.Count != 0)
{
foreach (string key in Res.Headers)
{
tw.WriteLine("\t<" + key.ToString() + ">" + "\t" + Res.Headers[key] + " </" + key.ToString() + ">");
}
}
else
{
tw.WriteLine("No Headers");
}
tw.WriteLine("</HEADERS>");
// close the stream
tw.Close();
return Res.Headers;
}
示例15: FakeHttpWebResponse
#pragma warning restore 618
public FakeHttpWebResponse(Stream response)
: this()
{
_headers = new WebHeaderCollection();
_responseStream = (MemoryStream)response;
}