Advertisement
Guest User

APIRequest.cs

a guest
Apr 15th, 2013
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5.  
  6. using System.Collections;
  7. using System.Net;
  8. using System.Text;
  9.  
  10. using WebDashboardLibrary;
  11.  
  12. namespace Web.api
  13. {
  14.     public class APIRequest : IHttpHandler
  15.     {
  16.         public void ProcessRequest(HttpContext context)
  17.         {
  18.             HttpRequest request = context.Request;
  19.             HttpResponse response = context.Response;
  20.  
  21.             if (api.APIUtils.HandleCORSPreflight(request, response))
  22.             {
  23.                 return;
  24.             }
  25.  
  26.             if (api.APIUtils.CheckAuthentication(request, response))
  27.             {
  28.                 Hashtable responseData = CreateResponseData(request, response);
  29.  
  30.                 string jsonStr = JSON.Encode(responseData);
  31.  
  32.                 string jsonpCallback = null;
  33.  
  34.                 if (!string.IsNullOrEmpty(request.QueryString["jsonp"]))
  35.                 {
  36.                     jsonpCallback = request.QueryString["jsonp"];
  37.                 }
  38.  
  39.                 if (jsonpCallback != null) // JSONP response
  40.                 {
  41.                     response.ContentType = "text/javascript";
  42.                     string jsonPStr = jsonpCallback + "(" + jsonStr + ");";
  43.                     response.Write(jsonPStr);
  44.                 }
  45.                 else // Normal JSON response
  46.                 {
  47.                     response.ContentType = "application/json";
  48.                     response.Write(jsonStr);
  49.                 }
  50.             }
  51.             else
  52.             {
  53.                 response.StatusCode = (int)HttpStatusCode.Unauthorized;
  54.             }
  55.         }
  56.  
  57.         public virtual Hashtable CreateResponseData(HttpRequest request, HttpResponse response)
  58.         {
  59.             Hashtable responseData = new Hashtable();
  60.  
  61.             return responseData;
  62.         }
  63.  
  64.         public bool IsReusable
  65.         {
  66.             get
  67.             {
  68.                 return false;
  69.             }
  70.         }
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement