当前位置: 首页>>代码示例>>C#>>正文


C# HttpMethod.Value方法代码示例

本文整理汇总了C#中HttpMethod.Value方法的典型用法代码示例。如果您正苦于以下问题:C# HttpMethod.Value方法的具体用法?C# HttpMethod.Value怎么用?C# HttpMethod.Value使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在HttpMethod的用法示例。


在下文中一共展示了HttpMethod.Value方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Request

        internal static string Request(HttpMethod httpMethod, Uri endpoint, List<Parameter> parameters, string header)
        {
            Uri uri = CreateUri(endpoint, parameters);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = httpMethod.Value();

            if (httpMethod == HttpMethod.Post)
                request.ContentType = "application/x-www-form-urlencoded";

            // oauth 1.0a authorization header
            if (header != null)
                request.Headers.Add("Authorization", header);

            // post parameters
            if (parameters != null && parameters.Any(p => p.Type == ParameterType.Post)) {
                string parameterString = string.Join("&", parameters.Where(p => p.Type == ParameterType.Post).Select(p => p.Name + "=" + (p.Encode ? UrlEncode(p.Value) : p.Value)));
                byte[] bytes = System.Text.Encoding.ASCII.GetBytes(parameterString);
                using (var requestStream = request.GetRequestStream()) {
                    requestStream.Write(bytes, 0, bytes.Length);
                    requestStream.Flush();
                }
            }

            try {
                using (var response = request.GetResponse())
                using (var responseStream = response.GetResponseStream())
                using (var reader = new StreamReader(responseStream))
                    return reader.ReadToEnd();
            }
            catch (WebException ex) {
                using (var response = ex.Response)
                using (var reader = new StreamReader(response.GetResponseStream())) {
                    throw new OpenAuthException(ex) {
                        Uri = uri,
                        Endpoint = endpoint,
                        Parameters = parameters,
                        HttpMethod = httpMethod,
                        Response = reader.ReadToEnd(),
                        HttpStatusCode = (response as HttpWebResponse).StatusCode
                    };
                }
            }
            catch (Exception ex) {
                throw new OpenAuthException(ex) {
                    Uri = uri,
                    Endpoint = endpoint,
                    Parameters = parameters,
                    HttpMethod = httpMethod
                };
            }
        }
开发者ID:fbdegroot,项目名称:OpenAuth,代码行数:51,代码来源:Utils.cs

示例2: GenerateSignatureBaseString

        private static string GenerateSignatureBaseString(HttpMethod httpMethod, Uri uri, List<Parameter> parameters)
        {
            parameters.Sort(new LexicographicComparer());

            var normalizedUrl = string.Format("{0}://{1}", uri.Scheme, uri.Host);

            if (!((uri.Scheme == "http" && uri.Port == 80) || (uri.Scheme == "https" && uri.Port == 443)))
                normalizedUrl += ":" + uri.Port;
            normalizedUrl += uri.AbsolutePath;

            var normalizedRequestParameters = string.Join("&", parameters.Select(p => p.Name + "=" + Utils.UrlEncode(p.Value)));

            StringBuilder signatureBaseSb = new StringBuilder();
            signatureBaseSb.AppendFormat("{0}&", httpMethod.Value());
            signatureBaseSb.AppendFormat("{0}&", Utils.UrlEncode(normalizedUrl));
            signatureBaseSb.AppendFormat("{0}", Utils.UrlEncode(normalizedRequestParameters));
            return signatureBaseSb.ToString();
        }
开发者ID:fbdegroot,项目名称:OpenAuth,代码行数:18,代码来源:OAuth.cs


注:本文中的HttpMethod.Value方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。