本文整理汇总了C#中HttpMethod.Equals方法的典型用法代码示例。如果您正苦于以下问题:C# HttpMethod.Equals方法的具体用法?C# HttpMethod.Equals怎么用?C# HttpMethod.Equals使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpMethod
的用法示例。
在下文中一共展示了HttpMethod.Equals方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: SendHttpWebRequest
/// <summary>
/// Sends an HTTP request to the given URL, and receives the response.
/// Note: This does not do a check to see if the currently stored creds are valid.
/// </summary>
/// <param name="url">The destination URL.</param>
/// <param name="method">The desired HTTP Request Method.</param>
/// <param name="data">The data to include in the message body.</param>
/// <param name="contentType">The Content-Type of the request. Default value is "application/json"</param>
/// <param name="timeout">How long to wait for the request to complete. Default wait time is 3 seconds.</param>
/// <returns>Returns the response text on success. Throws a WebException on failure.</returns>
private string SendHttpWebRequest(string url, HttpMethod method, string data, string contentType = "application/json", int timeout = 3000)
{
// CREATE REQUEST
using (HttpClient httpClient = new HttpClient())
{
HttpRequestMessage msg = new HttpRequestMessage(method, new Uri(url));
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
if (data != null)
{
msg.Content = new StringContent(data, Encoding.ASCII, contentType);
}
else if (!method.Equals(HttpMethod.Get))
{
msg.Content = new StringContent("", Encoding.ASCII, contentType);
}
// GET RESPONSE
HttpResponseMessage resp = httpClient.SendAsync(msg).Result;
if (!resp.IsSuccessStatusCode)
{
string error;
// try to parse as an ErrorResponse from Youtube API
try
{
string result = resp.Content.ReadAsStringAsync().Result;
ErrorResponse err = JsonConvert.DeserializeObject<ErrorResponse>(result);
throw new WebException("Error contacting serer. " + resp.ReasonPhrase + "\n" + err.YoutubeError.Message);
}
catch (JsonException ex)
{
throw new WebException("Error contacting serer. " + resp.ReasonPhrase);
}
}
return resp.Content.ReadAsStringAsync().Result;
}
}
示例2: SendHttpWebRequest
/// <summary>
/// Sends an HTTP request to the given URL, and receives the response.
/// Note: This does not do a check to see if the currently stored creds are valid.
/// </summary>
/// <param name="url">The destination URL.</param>
/// <param name="method">The desired HTTP Request Method.</param>
/// <param name="data">The data to include in the message body.</param>
/// <param name="contentType">The Content-Type of the request. Default value is "application/json"</param>
/// <param name="timeout">How long to wait for the request to complete. Default wait time is 3 seconds.</param>
/// <returns>Returns the response text on success. Throws a WebException on failure.</returns>
private string SendHttpWebRequest(string url, HttpMethod method, string data, string contentType = "application/json", int timeout = 3000)
{
try
{
// CREATE REQUEST
using (HttpClient httpClient = new HttpClient())
{
HttpRequestMessage msg = new HttpRequestMessage(method, new Uri(url));
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(contentType));
if (data != null)
{
msg.Content = new StringContent(data, Encoding.ASCII, contentType);
}
else if (!method.Equals(HttpMethod.Get))
{
msg.Content = new StringContent("", Encoding.ASCII, contentType);
}
// TODO: Does this work when creds are null?
// answer: no
//if (this.isAuthroized)
//{
// httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.ASCII.GetBytes("oy5ct3rjz7dob56syzaa65npbpumuvjm6d26fzdlupvighy2c4kq")));//":6cc3meg6fhnkbz22qocgetkndxrfjca76emcnanjodgiooir3xoq")));
//}
// GET RESPONSE
HttpResponseMessage resp = httpClient.SendAsync(msg).Result;
if (!resp.IsSuccessStatusCode)
{
throw new WebException("Error contacting serer. " + resp.ReasonPhrase);
}
return resp.Content.ReadAsStringAsync().Result;
}
}
catch (System.Net.WebException e)
{
this.LogTextBox.Text += "\r\n" + e.Message;
return null;
}
}
示例3: IsMethodNeedsContent
public static bool IsMethodNeedsContent(HttpMethod method)
{
if (method.Equals(HttpMethod.Post) || method.Equals(HttpMethod.Put))
return true;
return false;
}
示例4: ParseHttpArgs
static byte[] ParseHttpArgs(HttpMethod method, HttpArgs args)
{
StringBuilder bulider = new StringBuilder();
if (method.Equals(HttpMethod.POST))
{
bulider.AppendLine(string.Format("POST {0} HTTP/1.1",
args.Url));
bulider.AppendLine("Content-Type: application/x-www-form-urlencoded");
}
else
{
bulider.AppendLine(string.Format("GET {0} HTTP/1.1",
args.Url));
}
bulider.AppendLine(string.Format("Host: {0}",
args.Host));
bulider.AppendLine("User-Agent: Mozilla/5.0 (Windows NT 6.1; IE 9.0)");
if (!string.IsNullOrEmpty(args.Referer))
bulider.AppendLine(string.Format("Referer: {0}",
args.Referer));
bulider.AppendLine("Connection: keep-alive");
bulider.AppendLine(string.Format("Accept: {0}",
args.Accept));
bulider.AppendLine(string.Format("Cookie: {0}",
args.Cookie));
if (method.Equals(HttpMethod.POST))
{
bulider.AppendLine(string.Format("Content-Length: {0}\r\n",
Encoding.Default.GetBytes(args.Body).Length));
bulider.Append(args.Body);
}
else
{
bulider.Append("\r\n");
}
string header = bulider.ToString();
return Encoding.Default.GetBytes(header);
}
示例5: UploadRawData
private Task UploadRawData( RawDataInformation info, Stream data, HttpMethod method, CancellationToken cancellationToken )
{
if( info.Target.Entity == RawDataEntity.Value && !StringUuidTools.IsStringUuidPair( info.Target.Uuid ) )
throw new ArgumentOutOfRangeException( "info", "The uuid string for uploading raw data for measurement value needs 2 uuids in the format: {measurementUuid}|{characteristicUuid}" );
if( String.IsNullOrEmpty( info.FileName ) )
throw new ArgumentException( "FileName needs to be set.", "info" );
var requestString = info.Key >= 0
? string.Format( "rawData/{0}/{1}/{2}", info.Target.Entity, info.Target.Uuid, info.Key )
: string.Format( "rawData/{0}/{1}", info.Target.Entity, info.Target.Uuid );
if( method.Equals( HttpMethod.Post ) )
return Post( requestString, data, cancellationToken, info.Size, info.MimeType, info.MD5, info.FileName );
if( method.Equals( HttpMethod.Put ) )
return Put( requestString, data, cancellationToken, info.Size, info.MimeType, info.MD5, info.FileName );
throw new ArgumentOutOfRangeException( "method" );
}