惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
月光博客
月光博客
Y
Y Combinator Blog
量子位
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
MyScale Blog
MyScale Blog
aimingoo的专栏
aimingoo的专栏
Microsoft Azure Blog
Microsoft Azure Blog
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
罗磊的独立博客
Google DeepMind News
Google DeepMind News
大猫的无限游戏
大猫的无限游戏
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Visual Studio Blog
S
Schneier on Security
S
Security Affairs
Project Zero
Project Zero
L
LINUX DO - 热门话题
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
L
Lohrmann on Cybersecurity
Latest news
Latest news
P
Palo Alto Networks Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
The Last Watchdog
The Last Watchdog
Help Net Security
Help Net Security
I
Intezer
The Register - Security
The Register - Security
小众软件
小众软件
C
Check Point Blog
NISL@THU
NISL@THU
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tor Project blog
D
Docker
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
Forbes - Security
Forbes - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Security Latest
Security Latest
博客园 - 司徒正美
IT之家
IT之家

博客园 - Steven Xiao

解决Asp.net 程序在 IIS 5.1 上运行不支持转换Decimal类型小数点的问题 - Steven Xiao 把XML 文件转换为 String 字符串 - Steven Xiao C#语言规范 3.0 版 分享DotNetBar控件制作office 2007风格界面的视频教程(winform office 2007 风格) XMLHttpRequest对象 SQL 2005实现单表分页的查询语句 分享实现web用户控件调用.aspx页面里的方法(从而达到访问母页面中控件的目的) 软件开发者面试百问 分享一个DotNetBar做的三层架构的winForm程序 分享一个不错的js提示信息代码(tooltips) - Steven Xiao - 博客园 dropdownlist实现树型结构的栏目信息 asp.net 2.0实现多语言(二) asp.net 2.0实现多语言(一) 一个封装的在后台弹出JS Alert消息和JS confirm信息以及跳转到指定的页面 一个小小的WEB程序源代码 網絡收藏: 弹出窗口总结 - Steven Xiao gridview 实现全选和反选--补充 收藏的 sql经典语句 ---来自网上 ASP.NET button控件样式
asp.net接收API Post Json数据为空要注意的事项
Steven Xiao · 2022-07-28 · via 博客园 - Steven Xiao

今天在处理一个接收API通过Post方式传送Json数据的方法时,碰到接收的Json数据一直是空的问题。最好找了好久才解决,现在把需要的问题列出来。

1. 在一般处理程序中,需要设置  context.Request.InputStream.Position = 0;  刚开始设置了这个,但后面还是为空,原因是每二点。

2.在web.config文件,sessionState  要去掉 cookieless="AutoDetect" ,否则就算设置了第一点也还是会为空, 按下面的格式去写

<sessionState mode="InProc" cookieless="false" timeout="20" />

=========================================================

参考的IHttpHandler源代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Web;
using System.Text;
using System.Web.Script.Serialization;

// 接收 API 通过 Post 方式传送 Json格式的数据

public class GetIPN : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);

string jsonText = string.Empty;

context.Request.InputStream.Position = 0; //这一句很重要,不然一直是空

StreamReader sr = new StreamReader(context.Request.InputStream);
jsonText = sr.ReadToEnd();

 // IPNinfo model = (new JavaScriptSerializer()).Deserialize<IPNinfo >(jsonText);  //转化成实体对象

 //然后去处理业务

context.Response.ContentType = "text/plain";
context.Response.Write("OK"); //API回调响应,如需要返回OK
}

public bool IsReusable
{
get
{
return false;
}
}
}
}

工具Json转化为实体类:

https://www.bejson.com/convert/json2csharp/