本文整理汇总了C#中System.Net.WebHeaderCollection.HasKeys方法的典型用法代码示例。如果您正苦于以下问题:C# WebHeaderCollection.HasKeys方法的具体用法?C# WebHeaderCollection.HasKeys怎么用?C# WebHeaderCollection.HasKeys使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.WebHeaderCollection
的用法示例。
在下文中一共展示了WebHeaderCollection.HasKeys方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetPingBackServerURI
private static string GetPingBackServerURI(WebHeaderCollection headers, string pageText)
{
string pingBackServerURI = null;
// First look for the X-Pingback HTTP Header
if (headers != null && headers.HasKeys())
{
foreach (string key in headers.Keys)
{
if (key.ToLower().Trim() == "x-pingback")
{
pingBackServerURI = headers[key];
break;
}
}
}
// If the HTTP header was not found, look for the PingBack <Link> element in the body
if (string.IsNullOrEmpty(pingBackServerURI) && !string.IsNullOrEmpty(pageText))
{
Match m = pingbackLinkElementRegex.Match(pageText);
if (m.Success)
pingBackServerURI = m.Result("$1");
}
return pingBackServerURI;
}
示例2: ExectueMethod
private static void ExectueMethod(String username, String password, String caldavUrl, String methodName, WebHeaderCollection headers, string content, string contentType)
{
// <?xml version="1.0" encoding="utf-8"?>
//<propfind xmlns="DAV:">
// <allprop/>
//</propfind>
// Create an HTTP request for the URL.
HttpWebRequest httpGetRequest =
(HttpWebRequest)WebRequest.Create(caldavUrl);
// Set up new credentials.
httpGetRequest.Credentials =
new NetworkCredential(username, password);
// Pre-authenticate the request.
httpGetRequest.PreAuthenticate = true;
// Define the HTTP method.
httpGetRequest.Method = methodName;
// Optional, but allows for larger files.
httpGetRequest.SendChunked = true;
// Specify the request for source code.
//httpGetRequest.Headers.Add(@"Translate", "F");
if (headers != null && headers.HasKeys())
httpGetRequest.Headers = headers;
byte[] optionsArray = Encoding.UTF8.GetBytes(content);
httpGetRequest.ContentLength = optionsArray.Length;
if(!String.IsNullOrWhiteSpace(contentType))
httpGetRequest.ContentType = contentType;
// Retrieve the request stream.
Stream requestStream =
httpGetRequest.GetRequestStream();
// Write the string to the destination as a text file.
requestStream.Write(optionsArray, 0, optionsArray.Length);
// Close the request stream.
requestStream.Close();
// Retrieve the response.
HttpWebResponse httpGetResponse =
(HttpWebResponse)httpGetRequest.GetResponse();
// Retrieve the response stream.
Stream responseStream =
httpGetResponse.GetResponseStream();
// Retrieve the response length.
long responseLength =
httpGetResponse.ContentLength;
// Create a stream reader for the response.
StreamReader streamReader =
new StreamReader(responseStream, Encoding.UTF8);
StringBuilder sb = new StringBuilder();
// Write the response status to the console.
sb.AppendFormat(
@"GET Response: {0}",
httpGetResponse.StatusDescription).AppendLine();
sb.AppendFormat(
@" Response Length: {0}",
responseLength).AppendLine();
sb.AppendFormat(
@" Response Text: {0}",
streamReader.ReadToEnd()).AppendLine();
// Close the response streams.
streamReader.Close();
responseStream.Close();
}
示例3: 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;
}