/// <summary> /// 数据转换 /// </summary> /// <param name="context"></param> public void ProcessRequest(HttpContext context) { try { var api = context.Request["_api"] ?? ""; var url = ApiUrlDict.ContainsKey(api) ? ApiUrlDict[api] : context.Request["_url"]; var method = context.Request["_type"] ?? "GET"; var data = context.Request["_data"] ?? ""; var contentType = context.Request["_contentType"] ?? ""; var callback = context.Request["callback"]; var result = string.Empty; if (!string.IsNullOrEmpty(url)) { FillGetParams(ref url, context); var webRequest = (HttpWebRequest)WebRequest.Create(new Uri(url)); webRequest.Timeout = 1000 * 60; webRequest.Method = method.ToUpper(); if (method.Equals("POST", StringComparison.InvariantCultureIgnoreCase)) { FillPostParams(ref data, context, webRequest); var _params = Encoding.ASCII.GetBytes(data); webRequest.ContentLength = _params.Length; webRequest.ContentType = !string.IsNullOrEmpty(contentType) ? contentType : "application/x-www-form-urlencoded"; using (var reqStream = webRequest.GetRequestStream()) { reqStream.Write(_params, 0, _params.Length); } } else if (method.Equals("GET", StringComparison.InvariantCultureIgnoreCase)) { } using (var webResponse = (HttpWebResponse)webRequest.GetResponse()) { if (webResponse.StatusCode == HttpStatusCode.OK) { var streamResponse = webResponse.GetResponseStream(); if (streamResponse != null) { var streamRead = new StreamReader(streamResponse, System.Text.Encoding.UTF8); result = streamRead.ReadToEnd(); } } else { result = "调用错误!"; } } } context.Response.Write(string.IsNullOrEmpty(callback) ? string.Format("{0}", result) : string.Format("{0}({1});", callback, result)); } catch (Exception ex) { context.Response.Write("程序错误!" + ex); } }
public void ProcessRequest(HttpContext context) { string result; var session = context.Session; var userId = int.Parse((session["s_user_id"] != null ? session["s_user_id"].ToString() : "0")); var uid = session["s_uid"] != null ? session["s_uid"].ToString() : ""; var token = session["s_token"] != null ? session["s_token"].ToString() : ""; if (string.IsNullOrEmpty(session["s_guid"].ToString())) { WebUtility.RefreshGuid(); } var guid = session["s_guid"] != null ? session["s_guid"].ToString() : ""; var systemType = MConfigUtility.Get("SystemType"); var systemkey = MConfigUtility.Get("SystemKey"); var requestTime = DateTime.Now; _memberInfo = new MemberInfo { UserId = userId, Uid = uid, ToKen = token, Guid = guid, SystemType = systemType }; var callback = context.Request["callback"]; try { var apiName = context.Request["_api"] ?? ""; var apiHost = ApiUrlDict.ContainsKey(apiName) ? ApiUrlDict[apiName] : ""; var url = context.Request["_url"] ?? ""; if (string.IsNullOrEmpty(apiHost)) { result = WebUtility.ApiResult("api 不能为空!"); } else { var data = context.Request["_data"] ?? ""; var requestUrl = string.Format("{0}/{1}/{2}/{3}/{4}/{5}{6}", apiHost, MemberInfo.SystemType, string.IsNullOrEmpty(MemberInfo.ToKen) ? "null" : MemberInfo.ToKen, string.IsNullOrEmpty(MemberInfo.Guid) ? "null" : MemberInfo.Guid, MemberInfo.UserId, string.IsNullOrEmpty(MemberInfo.Uid) ? "null" : MemberInfo.Uid, string.IsNullOrEmpty(url) ? "" : "/" + url.Trim('/')); var uri = new Uri(requestUrl + "?time=" + requestTime + "&md5=" + System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(string.Format("{0}{1}{2}{3}", requestUrl, requestTime, data, systemkey).ToUpper(), "MD5")); var method = (MMethodType)Enum.Parse(typeof(MMethodType), context.Request["_type"] ?? "GET"); var contentType = context.Request["_contentType"] ?? ""; var accept = context.Request["_accept"] ?? ""; result = WebUtility.InvokeRestApi(new InvokeParmeter() { Uri = uri, Method = method, Data = data, Callback = callback, ContentType = contentType, Accept = accept }); ApiFactory.FormatResult(ref result, apiName, _memberInfo, context); } } catch (Exception ex) { result = WebUtility.ApiResult(ex.ToString()); } if (!string.IsNullOrEmpty(callback)) { context.Response.Write(string.Format("{0}({1});", callback, result)); } else { context.Response.Write(result); } }