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

  • 相关阅读:
    python测试开发django-rest-framework-87.分页查询之偏移分页(LimitOffsetPagination)和游标分页(CursorPagination)
    python测试开发django-rest-framework-86.分页查询功能(PageNumberPagination)
    python测试开发django-rest-framework-85.序列化(ModelSerializer)之设置必填(required)和非必填字段
    python测试开发django-rest-framework-84.序列化(ModelSerializer)之日期时间格式带T问题
    去掉DELPHI开启后弹出安全警告框
    使用path 格式获取java hashmap key 值
    Kubeapps-2.0 发布了
    monio系统性能分析相关命令
    imgproxy 强大高效的图片处理服务
    nodejs java 互调用
  • 原文地址:https://www.cnblogs.com/shen119/p/3453359.html
Copyright © 2011-2022 走看看