本文整理汇总了C#中HttpVerbs.ToString方法的典型用法代码示例。如果您正苦于以下问题:C# HttpVerbs.ToString方法的具体用法?C# HttpVerbs.ToString怎么用?C# HttpVerbs.ToString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpVerbs
的用法示例。
在下文中一共展示了HttpVerbs.ToString方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ExecuteRequest
private async Task<HttpResponseMessage> ExecuteRequest(HttpVerbs method, Uri requestUri, HttpContent content = null)
{
var request = WebRequest.CreateHttp(requestUri);
var httpResponseMessage = new HttpResponseMessage();
request.Headers = DefaultRequestHeaders;
request.Method = method.ToString();
request.Accept = "*/*";
httpResponseMessage.Request = request;
Logger.LogWebRequest(request);
if (content != null) Logger.LogVerbose(content.ToString());
try
{
if (method == HttpVerbs.POST)
{
request.ContentType = content.ContentType;
var requestStreamTask = Task<Stream>.Factory.FromAsync(request.BeginGetRequestStream, request.EndGetRequestStream, null);
using (Stream stream = await requestStreamTask.ConfigureAwait(false))
{
content.WriteToStream(stream);
}
}
var responseTask = Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null);
using (var response = await responseTask.ConfigureAwait(false) as HttpWebResponse)
{
httpResponseMessage.SetResponseData(response);
Logger.LogWebResponse(response);
}
}
catch (WebException we)
{
var response = (HttpWebResponse)we.Response;
if (response != null)
httpResponseMessage.SetResponseData(response);
Logger.Log(we);
}
catch (Exception e)
{
Logger.Log(e);
}
return httpResponseMessage;
}
示例2: IsHttpMethod
// CODEREVIEW: this implementation kind of misses the point of HttpVerbs
// by falling back to string comparison, consider something better
// also, how do we keep this switch in sync?
public static bool IsHttpMethod(this HttpRequestBase request, HttpVerbs httpMethod, bool allowOverride) {
switch (httpMethod) {
case HttpVerbs.Get:
return request.IsHttpMethod("GET", allowOverride);
case HttpVerbs.Post:
return request.IsHttpMethod("POST", allowOverride);
case HttpVerbs.Put:
return request.IsHttpMethod("PUT", allowOverride);
case HttpVerbs.Delete:
return request.IsHttpMethod("DELETE", allowOverride);
case HttpVerbs.Head:
return request.IsHttpMethod("HEAD", allowOverride);
default:
// CODEREVIEW: does this look reasonable?
return request.IsHttpMethod(httpMethod.ToString().ToUpperInvariant(), allowOverride);
}
}
示例3: IsUniquePropertyValidator
//"{PropertyName} must be unique."
public IsUniquePropertyValidator(IPropertyValidatorService service, string errorMessage,
string action,
string controller,
HttpVerbs HttpVerb = HttpVerbs.Get,
string additionalFields = "")
: base(errorMessage)
{
this._service = service;
var httpContext = HttpContext.Current;
if (httpContext != null)
{
var httpContextBase = new HttpContextWrapper(httpContext);
var routeData = new RouteData();
var requestContext = new RequestContext(httpContextBase, routeData);
var helper = new UrlHelper(requestContext);
Url = helper.Action(action, controller);
HttpMethod = HttpVerb.ToString();
AdditionalFields = additionalFields;
}
}
示例4: RemoteValidator
public RemoteValidator(string errorMessage, string action, string controller, HttpVerbs httpVerb = HttpVerbs.Get, string additionalFields = "")
: base(errorMessage)
{
var httpContext = HttpContext.Current;
if (httpContext == null)
{
var request = new HttpRequest("/", "http://ubasolutions.com", "");
var response = new HttpResponse(new StringWriter());
httpContext = new HttpContext(request, response);
}
var httpContextBase = new HttpContextWrapper(httpContext);
var routeData = new RouteData();
var requestContext = new RequestContext(httpContextBase, routeData);
var helper = new UrlHelper(requestContext);
Url = helper.Action(action, controller);
HttpMethod = httpVerb.ToString();
AdditionalFields = additionalFields;
}
示例5: DoDataRequest
public Stream DoDataRequest(string query, HttpVerbs method, Action<Stream> data, string contenttype)
{
HttpWebRequest req = WebRequest.Create(new Uri(Url,query)) as HttpWebRequest;
if (Credentials != null)
req.Credentials = Credentials;
req.Method = method.ToString();
//req.Timeout = System.Threading.Timeout.Infinite;
if (!string.IsNullOrEmpty(contenttype))
req.ContentType = contenttype;
if (data != null)
{
using (Stream ps = req.GetRequestStream())
{
data.Invoke(ps);
}
}
try
{
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
if (resp.StatusCode == HttpStatusCode.OK || resp.StatusCode == HttpStatusCode.Created)
return resp.GetResponseStream();
throw new RestException(resp.StatusCode, resp.StatusDescription);
}
catch (WebException ex)
{
HttpWebResponse resp = ex.Response as HttpWebResponse;
StreamReader sr = new StreamReader(resp.GetResponseStream());
string contents = sr.ReadToEnd();
throw new RestException(resp.StatusCode, resp.StatusDescription);
}
}
示例6: ProcessRequest
public RequestResult ProcessRequest(string url, HttpVerbs httpVerb, NameValueCollection formValues, NameValueCollection headers)
{
if (url == null) throw new ArgumentNullException("url");
// Fix up URLs that incorrectly start with / or ~/
if (url.StartsWith("~/"))
url = url.Substring(2);
else if(url.StartsWith("/"))
url = url.Substring(1);
// Parse out the querystring if provided
string query = "";
int querySeparatorIndex = url.IndexOf("?");
if (querySeparatorIndex >= 0) {
query = url.Substring(querySeparatorIndex + 1);
url = url.Substring(0, querySeparatorIndex);
}
// Perform the request
LastRequestData.Reset();
var output = new StringWriter();
string httpVerbName = httpVerb.ToString().ToLower();
var workerRequest = new SimulatedWorkerRequest(url, query, output, Cookies, httpVerbName, formValues, headers);
HttpRuntime.ProcessRequest(workerRequest);
// Capture the output
AddAnyNewCookiesToCookieCollection();
Session = LastRequestData.HttpSessionState;
return new RequestResult
{
ResponseText = output.ToString(),
ActionExecutedContext = LastRequestData.ActionExecutedContext,
ResultExecutedContext = LastRequestData.ResultExecutedContext,
Response = LastRequestData.Response,
};
}
示例7: HTTP
/// <summary>
/// Performs an HTTP request to an URL using the specified method and data, returning the response as a string
/// </summary>
protected virtual LatchResponse HTTP(Uri URL, HttpVerbs method, IDictionary<string, string> headers, IDictionary<string, string> data)
{
HttpWebRequest request = BuildHttpUrlConnection(URL, headers);
if (request == null)
{
throw new HttpException("Request could not be created correctly");
}
request.Method = method.ToString();
try
{
if (method.Equals(HttpVerbs.Post) || method.Equals(HttpVerbs.Put))
{
request.ContentType = HTTP_HEADER_CONTENT_TYPE_FORM_URLENCODED;
using (StreamWriter sw = new StreamWriter(request.GetRequestStream()))
{
sw.Write(GetSerializedParams(data));
sw.Flush();
}
}
using (StreamReader sr = new StreamReader(request.GetResponse().GetResponseStream()))
{
string json = sr.ReadToEnd();
return new LatchResponse(json);
}
}
catch (Exception)
{
return null;
}
}
示例8: StringToSign
private IDictionary<string, string> StringToSign(HttpVerbs httpMethod, string queryString, IDictionary<string, string> xHeaders, string UTC, IDictionary<string, string> param)
{
string stringToSign = String.Concat(httpMethod.ToString().ToUpper(), "\n", UTC, "\n", GetSerializedHeaders(xHeaders), "\n", queryString.Trim());
{
string serializedParams = GetSerializedParams(param);
if (!String.IsNullOrEmpty(serializedParams))
{
stringToSign = String.Concat(stringToSign, "\n", serializedParams);
}
}
string signedData = String.Empty;
try
{
signedData = SignData(stringToSign.ToString());
}
catch (Exception e)
{
return null;
}
string authorizationHeader = String.Concat(AUTHORIZATION_METHOD, AUTHORIZATION_HEADER_FIELD_SEPARATOR, this.appId, AUTHORIZATION_HEADER_FIELD_SEPARATOR, signedData);
IDictionary<string, string> headers = new Dictionary<string, string>();
headers.Add(AUTHORIZATION_HEADER_NAME, authorizationHeader);
headers.Add(DATE_HEADER_NAME, UTC);
return headers;
}
示例9: ProcessRequest
/// <summary>
/// Processes the request.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="httpVerb">The HTTP verb.</param>
/// <param name="formValues">The form values.</param>
/// <param name="headers">The headers.</param>
/// <returns>The request of executing the simulate request.</returns>
private RequestResult ProcessRequest(
string url, HttpVerbs httpVerb, NameValueCollection formValues, NameValueCollection headers)
{
if (url == null)
{
throw new ArgumentNullException("url");
}
// TODO: Doesn't the BCL contain methods to do all this URL handling already?
// Fix up URLs that incorrectly start with / or ~/
// TODO: Why is this incorrect? They can be handled fine and make the library easier to use by avoiding unecessary restrictions.
if (url.StartsWith("~/", true, CultureInfo.InvariantCulture))
{
url = url.Substring(2);
}
else if (url.StartsWith("/", true, CultureInfo.InvariantCulture))
{
url = url.Substring(1);
}
// Parse out the querystring if provided
var query = string.Empty;
var querySeparatorIndex = url.IndexOf("?");
if (querySeparatorIndex >= 0)
{
query = url.Substring(querySeparatorIndex + 1);
url = url.Substring(0, querySeparatorIndex);
}
// Perform the request
LastRequestData.Reset();
var output = new StringWriter();
var httpVerbName = httpVerb.ToString().ToLower();
var workerRequest = new SimulatedWorkerRequest(
url, query, output, this.Cookies, httpVerbName, formValues, headers);
HttpRuntime.ProcessRequest(workerRequest);
// Capture the output
this.AddAnyNewCookiesToCookieCollection();
this.Session = LastRequestData.HttpSessionState;
return new RequestResult
{
ResponseText = output.ToString(),
ActionExecutedContext = LastRequestData.ActionExecutedContext,
ResultExecutedContext = LastRequestData.ResultExecutedContext,
Response = LastRequestData.Response,
};
}
示例10: RouteAttribute
/// <summary>
/// Specify the route information for an action.
/// </summary>
/// <param name="routeUrl">The url that is associated with this action</param>
/// <param name="allowedMethods">The httpMethods against which to constrain the route</param>
public RouteAttribute(string routeUrl, HttpVerbs allowedMethods)
: this(routeUrl, allowedMethods.ToString().ToUpper().SplitAndTrim(new[] { "," }))
{
}