本文整理汇总了C#中System.Net.WebHeaderCollection.Get方法的典型用法代码示例。如果您正苦于以下问题:C# WebHeaderCollection.Get方法的具体用法?C# WebHeaderCollection.Get怎么用?C# WebHeaderCollection.Get使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.WebHeaderCollection
的用法示例。
在下文中一共展示了WebHeaderCollection.Get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetContentLength
protected static int GetContentLength(WebHeaderCollection responseHeaders)
{
int contentLength;
//get the content length (if present)
//Will return 0 if can't get value
string clenString = responseHeaders.Get("Content-Length");
//string clenString = response.GetResponseHeader("Content-Lengthzzz");
int.TryParse(clenString, out contentLength);
return contentLength;
}
示例2: GetHeadersDictionaryFrom
private static Dictionary<string, string> GetHeadersDictionaryFrom(WebHeaderCollection headers)
{
string pattern = "\\s+";
Dictionary<string, string> dictionary = new Dictionary<string, string>();
for (int i = 0; i < headers.Keys.Count; i++)
{
string key = headers.GetKey(i).Replace("-", " ").ToUpper();
key = Regex.Replace(key, pattern, "");
string value = headers.Get(headers.GetKey(i));
//System.Console.WriteLine("Key => " + key + " Value => " + value);
dictionary.Add(key, value);
}
return dictionary;
}
示例3: SessionCommand
public SessionCommand(JsonObject response, WebHeaderCollection headers)
{
TransmissionDaemonDescriptor descriptor = new TransmissionDaemonDescriptor();
JsonObject arguments = (JsonObject)response[ProtocolConstants.KEY_ARGUMENTS];
if (arguments.Contains("version"))
ParseVersionAndRevisionResponse((string)arguments["version"], descriptor);
else if (headers.Get("Server") != null)
descriptor.Version = 1.40;
else
descriptor.Version = 1.39;
if (arguments.Contains("rpc-version"))
descriptor.RpcVersion = Toolbox.ToInt(arguments["rpc-version"]);
if (arguments.Contains("rpc-version-minimum"))
descriptor.RpcVersionMin = Toolbox.ToInt(arguments["rpc-version-minimum"]);
descriptor.SessionData = (JsonObject)response[ProtocolConstants.KEY_ARGUMENTS];
Program.DaemonDescriptor = descriptor;
}
示例4: SessionEvent
public SessionEvent(byte[] responseBytes, WebHeaderCollection responseHeaders,
String uri, String capsKey, String proto)
: base()
{
this.Protocol = proto;
this.Direction = Direction.Incoming; // EventQueue Messages are always inbound from the simulator
this.ResponseBytes = responseBytes;
this.ResponseHeaders = responseHeaders;
this.Host = uri;
this.Name = capsKey;
this.ContentType = responseHeaders.Get("Content-Type");
this.Length = Int32.Parse(responseHeaders.Get("Content-Length"));
}
示例5: FormatHeaders
static string FormatHeaders (WebHeaderCollection headers)
{
var sb = new StringBuilder();
for (int i = 0; i < headers.Count ; i++) {
string key = headers.GetKey (i);
if (WebHeaderCollection.AllowMultiValues (key)) {
foreach (string v in headers.GetValues (i)) {
sb.Append (key).Append (": ").Append (v).Append ("\r\n");
}
} else {
sb.Append (key).Append (": ").Append (headers.Get (i)).Append ("\r\n");
}
}
return sb.Append("\r\n").ToString();
}
示例6: HandleHeader
/// <summary>
/// Handle response headers
/// </summary>
private void HandleHeader(string url, WebHeaderCollection headers)
{
for (int i = 0; i < headers.Count; i++)
{
string Key = headers.GetKey(i);
string Value = headers.Get(i);
switch (Key)
{
case "Set-Cookie":
HandleResponseSetCookie(url, Value);
break;
case "Location":
this.RedirectLocation = Value;
break;
}
}
}
示例7: GetPage
public static string GetPage(string url, string method, string contentType, SortedList<string, string> values, ref WebHeaderCollection responseHeaders)
{
string tmp = string.Empty;
try
{
HttpWebRequest wReq = (HttpWebRequest)WebRequest.Create(url);
wReq.UserAgent = "FlowLib";
#if !COMPACT_FRAMEWORK
wReq.CookieContainer = new CookieContainer();
// Enable cookie support
if (responseHeaders != null && responseHeaders.HasKeys())
{
string str = responseHeaders.Get("Set-Cookie");
if (!string.IsNullOrEmpty(str))
{
string[] tmp2= str.Split('=', ' ', ';', ',', '\t', '\n', '\r');
if (tmp2.Length >= 2){
Cookie cookie = new Cookie(tmp2[0], tmp2[1]);
wReq.CookieContainer.Add(wReq.RequestUri, cookie);
}
//wReq.CookieContainer.Add(new Cookie(
//wReq.Headers.Add("Cookie", str);
//wReq.Headers.Set("Cookie", str);
}
}
#endif
if (!string.IsNullOrEmpty(method) && !string.IsNullOrEmpty(contentType) && values != null)
{
StringBuilder sb = new StringBuilder();
foreach (KeyValuePair<string, string> var in values)
{
sb.Append("&");
sb.Append(var.Key);
sb.Append("=");
sb.Append(var.Value);
}
byte[] data = Encoding.UTF8.GetBytes(sb.ToString());
// Set values for the request back
wReq.Method = method;
wReq.ContentType = contentType;
wReq.ContentLength = data.Length;
// Write to request stream.
Stream streamOut = wReq.GetRequestStream();
streamOut.Write(data, 0, data.Length);
streamOut.Flush();
streamOut.Close();
}
// Get the response stream.
WebResponse wResp = wReq.GetResponse();
Stream respStream = wResp.GetResponseStream();
StreamReader reader = new StreamReader(respStream, Encoding.UTF8);
tmp = reader.ReadToEnd();
responseHeaders = wResp.Headers;
// Close the response and response stream.
wResp.Close();
}
catch (System.Exception e)
{
System.Console.WriteLine(e.Message);
}
return tmp;
}
示例8: GetValues
public void GetValues ()
{
WebHeaderCollection w = new WebHeaderCollection ();
w.Add ("Hello", "H1");
w.Add ("Hello", "H2");
w.Add ("Hello", "H3,H4");
string [] sa = w.GetValues ("Hello");
Assert.AreEqual (3, sa.Length, "#1");
Assert.AreEqual ("H1,H2,H3,H4", w.Get ("Hello"), "#2");
w = new WebHeaderCollection ();
w.Add ("Accept", "H1");
w.Add ("Accept", "H2");
w.Add ("Accept", "H3, H4 ");
Assert.AreEqual (3, w.GetValues (0).Length, "#3a");
Assert.AreEqual (4, w.GetValues ("Accept").Length, "#3b");
Assert.AreEqual ("H4", w.GetValues ("Accept")[3], "#3c");
Assert.AreEqual ("H1,H2,H3, H4", w.Get ("Accept"), "#4");
w = new WebHeaderCollection ();
w.Add ("Allow", "H1");
w.Add ("Allow", "H2");
w.Add ("Allow", "H3,H4");
sa = w.GetValues ("Allow");
Assert.AreEqual (4, sa.Length, "#5");
Assert.AreEqual ("H1,H2,H3,H4", w.Get ("Allow"), "#6");
w = new WebHeaderCollection ();
w.Add ("AUTHorization", "H1, H2, H3");
sa = w.GetValues ("authorization");
Assert.AreEqual (3, sa.Length, "#9");
w = new WebHeaderCollection ();
w.Add ("proxy-authenticate", "H1, H2, H3");
sa = w.GetValues ("Proxy-Authenticate");
Assert.AreEqual (3, sa.Length, "#9");
w = new WebHeaderCollection ();
w.Add ("expect", "H1,\tH2, H3 ");
sa = w.GetValues ("EXPECT");
Assert.AreEqual (3, sa.Length, "#10");
Assert.AreEqual ("H2", sa [1], "#11");
Assert.AreEqual ("H3", sa [2], "#12");
try {
w.GetValues (null);
Assert.Fail ("#13");
} catch (ArgumentNullException) { }
Assert.AreEqual (null, w.GetValues (""), "#14");
Assert.AreEqual (null, w.GetValues ("NotExistent"), "#15");
w = new WebHeaderCollection ();
w.Add ("Accept", null);
Assert.AreEqual (1, w.GetValues ("Accept").Length, "#16");
w = new WebHeaderCollection ();
w.Add ("Accept", ",,,");
Assert.AreEqual (3, w.GetValues ("Accept").Length, "#17");
}
示例9: CrawlIt
public static string CrawlIt(this string url, Encoding encoding, WebHeaderCollection headers = null, CookieContainer cookieContainer = null, int timeout = 3000)
{
var tryCount = 0;
while (true)
{
try
{
var webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)";
webRequest.CookieContainer = cookieContainer == null ? new CookieContainer() : cookieContainer;
if ( headers != null )
{
foreach( String key in headers.Keys )
{
switch ( key )
{
// referer는 Header 컬렉션에 포함되지 않고 별도로 취급되므로 따로 빼놓기.
case "Referer":
webRequest.Referer = headers.Get(key);
break;
default:
webRequest.Headers.Set(key, headers.Get(key));
break;
}
}
}
webRequest.AllowAutoRedirect = true;
webRequest.Timeout = timeout;
using (var webResponse = (HttpWebResponse)webRequest.GetResponse())
using (var reader = new StreamReader(webResponse.GetResponseStream(), encoding))
{
var rawHtml = reader.ReadToEnd();
var statusCode = webResponse.StatusCode;
return rawHtml;
}
}
catch (WebException ex)
{
Logger.Log(new Exception(url) { Source = ex.Source });
Logger.Log(ex);
tryCount++;
// 3번 시도한 후 안될 경우 더 이상 시도하지 않음
if (tryCount == 3)
throw;
}
}
}
示例10: DisplayHeaders
private static void DisplayHeaders(WebHeaderCollection headers)
{
foreach (string key in headers.Keys)
Trace.WriteLine(key + ":" + headers.Get(key));
}
示例11: GetRedirectHeaders
/// <summary>
/// Receives the continue header from delegate and sets it to ContinueHeader property.
/// </summary>
/// <param name="statusCode"> The status code</param>
/// <param name="header"> The header collection.</param>
protected void GetRedirectHeaders(int statusCode, WebHeaderCollection header)
{
StringBuilder textStream = new StringBuilder();
for (int i = 0;i<header.Count;i++)
{
textStream.Append(header.GetKey(i));
textStream.Append(": ");
textStream.Append(header.Get(i));
textStream.Append("\r\n");
}
ContinueHeader= textStream.ToString();
}
示例12: ResetCookie
public void ResetCookie(WebHeaderCollection whc, string host)
{
for (int i = 0; i < whc.Count; i++) {
string key = whc.GetKey(i);
string value = whc.Get(i);
if (key == "Set-Cookie") {
Match match = Regex.Match(value, "(.+?)=(.+?);");
if (match.Captures.Count > 0) {
cookieContainer.Add(new Cookie(match.Groups[1].Value, match.Groups[2].Value, "/", host.Split(':')[0]));
}
}
}
}
示例13: SetHeaders
void SetHeaders(WebHeaderCollection headers)
{
if (headers != null && headers.Count > 0)
{
foreach (string item in headers.AllKeys)
{
request.Headers.Add(item, headers.Get(item));
}
}
}
示例14: TryParseRetryAfter
private bool TryParseRetryAfter(WebHeaderCollection headers, out TimeSpan retryAfterTimeSpan)
{
retryAfterTimeSpan = TimeSpan.FromSeconds(0);
var retryAfter = headers.Get("Retry-After");
if (retryAfter == null)
{
return false;
}
var now = DateTime.Now;
DateTime retryAfterDate;
if (DateTime.TryParse(retryAfter, out retryAfterDate))
{
if (retryAfterDate > now)
{
retryAfterTimeSpan = retryAfterDate - now;
return true;
}
else
{
return false;
}
}
TelemetryChannelEventSource.Log.TransmissionPolicyRetryAfterParseFailedWarning(retryAfter);
return false;
}
示例15: ProcessHeaderStream
private WebHeaderCollection ProcessHeaderStream(HttpRequest request, CookieContainer cookies, Stream headerStream)
{
headerStream.Position = 0;
var headerData = headerStream.ToBytes();
var headerString = Encoding.ASCII.GetString(headerData);
var webHeaderCollection = new WebHeaderCollection();
// following a redirect we could have two sets of headers, so only process the last one
foreach (var header in headerString.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Reverse())
{
if (!header.Contains(":")) break;
webHeaderCollection.Add(header);
}
var setCookie = webHeaderCollection.Get("Set-Cookie");
if (setCookie != null && setCookie.Length > 0 && cookies != null)
{
try
{
cookies.SetCookies((Uri)request.Url, FixSetCookieHeader(setCookie));
}
catch (CookieException ex)
{
_logger.Debug("Rejected cookie {0}: {1}", ex.InnerException.Message, setCookie);
}
}
return webHeaderCollection;
}