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);
            }
        }
    }
}

原文地址:https://www.cnblogs.com/shen119/p/3453359.html