接口里只传的是固定长度的数据,所以不需要这个属性,需要把这个自动分块机制去掉,最后是这样写的,
public class created : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpRequest Request = context.Request;
HttpResponse Response = context.Response;
byte[] byts = new byte[Request.InputStream.Length];
Request.InputStream.Read(byts, 0, byts.Length);
string req = System.Text.Encoding.Default.GetString(byts);
LogHelper.WriteLog(typeof(created), req);
try
{
// 处理后台逻辑
context.Response.ContentType = "application/json";
context.Response.Headers.Remove("Transfer-Encoding");
var length = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(r)).Length;
context.Response.Headers.Set("Content-Length", length.ToString());
context.Response.Write(JsonConvert.SerializeObject(r));
}
}
}
catch (Exception c)
{
LogHelper.WriteLog(typeof(created), c);
}
finally
{
context.Response.Flush();
context.Response.Close();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
注意:一定要注意 System.Text.Encoding.UTF8.GetBytes,不然会出现汉字字节长度在IIS7.5和IIS10不一样的问题
注意: context.Response.Flush();
context.Response.Close();
最后记得写上这两句话,不然有时会出现连接不上的问题