zoukankan      html  css  js  c++  java
  • Resh Handler 获取数据

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Net.Http;
    using System.Configuration;
    using System.Net.Http.Headers;
    using System.Net;
    using System.Threading.Tasks;

    namespace QPP.WebApp.Rest
    {
        /// <summary>
        /// 异步处理请求
        /// </summary>
        public class RestHandler : IHttpHandler, IHttpAsyncHandler
        {
            private HttpContext m_context;

            public void ProcessRequest(HttpContext context)
            {
                //Use Async Process and DO Nothing Here.
            }

            public bool IsReusable
            {
                get { return false; }
            }

            public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, object extraData)
            {
                this.m_context = context;

                try
                {
                    HttpClient client = new HttpClient();
                    client.BaseAddress = new Uri(ConfigurationManager.AppSettings["RestServiceUri"]);
                    // Request.RawUrl Format:/Rest/{*pathInfo}...
                    var url = context.Request.RawUrl.Substring(context.Request.RawUrl.IndexOf("Rest", StringComparison.OrdinalIgnoreCase) + 5);
                    var request = new HttpRequestMessage(new HttpMethod(context.Request.RequestType), url);
                    CopyHeaders(context.Request, request.Headers);
                    if (context.Request.InputStream.Length > 0)
                    {
                        request.Content = new StreamContent(context.Request.InputStream);
                        CopyHeaders(context.Request, request.Content.Headers);
                    }
                    request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json", 0.99));
                    return client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead)
                        .ContinueWith(p => cb.Invoke(p));
                }
                catch (Exception exc)
                {
                    //TODO:log
                    throw exc;
                }
            }

            void CopyHeaders(HttpRequest request, HttpHeaders header)
            {
                foreach (string key in request.Headers.Keys)
                {
                    try
                    {
                        //TODO:添加权限信息
                        header.Add(key, request.Headers[key]);
                    }
                    catch { /*ignor the unacceptable values*/ }
                }
            }

            public void EndProcessRequest(IAsyncResult result)
            {
                var task = result as Task<HttpResponseMessage>;
                var response = task.Result;
                this.m_context.Response.ContentType = "application/json";
                if (response.IsSuccessStatusCode)
                {
                    var data = response.Content.ReadAsStringAsync().Result;
                    m_context.Response.Write(data);
                }
                else
                {
                    m_context.Response.StatusCode = (int)response.StatusCode;
                    m_context.Response.StatusDescription = response.ReasonPhrase;
                    var msg = string.Format("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase);
                    var data = "{"Success":false,"Message":"" + msg + ""}";
                    m_context.Response.Write(data);
                }
            }
        }
    }

  • 相关阅读:
    leetcode-788-Rotated Digits(使用vector替代if else的逐个判断)
    leetcode-771-Jewels and Stones(建立哈希表,降低时间复杂度)
    leetcode-766-Toeplitz Matrix(每一条对角线元素的比较)
    leetcode-762-Prime Number of Set Bits in Binary Representation
    leetcode-747-Largest Number At Least Twice of Others(求vector的最大值和次大值)
    Flink中逻辑计划和物理计划的概念划分和对应关系
    Java线程池中线程的生命周期
    在ServletFilter层返回多字段提示给前端
    JavaBIO利用装饰器模式来组织和扩展接口
    SparkShuffle机制
  • 原文地址:https://www.cnblogs.com/shen119/p/3453359.html
Copyright © 2011-2022 走看看