99 lines
2.8 KiB
C#
99 lines
2.8 KiB
C#
using KJD_Web.BLL.WXXD;
|
|
using KJD_Web.Requests;
|
|
using KJD_Web.Utils;
|
|
using Store.Framework.Base;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Specialized;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Text;
|
|
using System.Web;
|
|
using System.Web.Http;
|
|
using System.Web.Http.Controllers;
|
|
|
|
namespace KJD_Web.Controllers
|
|
{
|
|
public class AsmxApiController : ApiControllerBase
|
|
{
|
|
//public override System.Threading.Tasks.Task<HttpResponseMessage> ExecuteAsync(
|
|
//HttpControllerContext controllerContext,
|
|
//System.Threading.CancellationToken cancellationToken)
|
|
//{
|
|
// // 把 POST 请求的 HTTP 方法临时改成 GET,绕过动词匹配
|
|
// if (controllerContext.Request.Method == HttpMethod.Post)
|
|
// {
|
|
// controllerContext.Request.Method = HttpMethod.Get;
|
|
// }
|
|
|
|
// return base.ExecuteAsync(controllerContext, cancellationToken);
|
|
//}
|
|
|
|
|
|
protected string GetRequestJsonString()
|
|
{
|
|
//Context.Response.ContentType = "text/html;charset=utf-8";
|
|
//Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
|
|
object raw;
|
|
if (Request.Properties.TryGetValue("RawBody", out raw))
|
|
{
|
|
return raw as string;
|
|
}
|
|
//using (Stream s = HttpContext.Current.Request.InputStream) // 获得json 字符流
|
|
//{
|
|
// // 还原数据流
|
|
// byte[] b = new byte[s.Length];
|
|
// s.Read(b, 0, (int)s.Length);
|
|
// string jsonText = Encoding.UTF8.GetString(b); // JSON字符串
|
|
// if (string.IsNullOrEmpty(jsonText))
|
|
// {
|
|
// var qs = HttpContext.Current.Request.QueryString;
|
|
// if (qs != null && qs.Keys.Count > 0)
|
|
// {
|
|
// jsonText = "{";
|
|
// foreach (var key in qs.Keys)
|
|
// {
|
|
// jsonText += "\"" + key + "\":" + "\"" + qs[key.ToString()] + "\";";
|
|
// }
|
|
// jsonText = jsonText.TrimEnd(';') + "}";
|
|
// }
|
|
// }
|
|
// return jsonText;
|
|
//}
|
|
|
|
var request = HttpContext.Current.Request;
|
|
|
|
// 优先用 Web API 的 Content
|
|
var content = System.Web.HttpContext.Current?.Items["MS_HttpRequestMessage"]
|
|
as System.Net.Http.HttpRequestMessage;
|
|
|
|
string jsonText = null;
|
|
if (content?.Content != null)
|
|
{
|
|
jsonText = content.Content.ReadAsStringAsync().Result;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(jsonText))
|
|
{
|
|
var qs = request.QueryString;
|
|
if (qs != null && qs.Keys.Count > 0)
|
|
{
|
|
var sb = new StringBuilder("{");
|
|
foreach (string key in qs.Keys)
|
|
sb.Append($"\"{key}\":\"{qs[key]}\",");
|
|
jsonText = sb.ToString().TrimEnd(',') + "}";
|
|
}
|
|
}
|
|
return jsonText;
|
|
|
|
}
|
|
protected IHttpActionResult JsonString(string result)
|
|
{
|
|
var response = Request.CreateResponse(HttpStatusCode.OK);
|
|
response.Content = new StringContent(result, Encoding.UTF8, "text/html");
|
|
return base.ResponseMessage(response);
|
|
}
|
|
}
|
|
} |