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

推荐订阅源

Help Net Security
Help Net Security
Latest news
Latest news
G
GRAHAM CLULEY
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
The Exploit Database - CXSecurity.com
WordPress大学
WordPress大学
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
U
Unit 42
人人都是产品经理
人人都是产品经理
小众软件
小众软件
Microsoft Security Blog
Microsoft Security Blog
C
CERT Recently Published Vulnerability Notes
V
Vulnerabilities – Threatpost
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
NISL@THU
NISL@THU
AWS News Blog
AWS News Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cisco Blogs
Google DeepMind News
Google DeepMind News
阮一峰的网络日志
阮一峰的网络日志
Project Zero
Project Zero
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Know Your Adversary
Know Your Adversary
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
L
LINUX DO - 热门话题
The Hacker News
The Hacker News
Application and Cybersecurity Blog
Application and Cybersecurity Blog
GbyAI
GbyAI
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
T
Threat Research - Cisco Blogs
The GitHub Blog
The GitHub Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
Attack and Defense Labs
Attack and Defense Labs
Cloudbric
Cloudbric
Scott Helme
Scott Helme
J
Java Code Geeks
H
Hacker News: Front Page
N
News and Events Feed by Topic
Recorded Future
Recorded Future
Martin Fowler
Martin Fowler
W
WeLiveSecurity
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
量子位
有赞技术团队
有赞技术团队

博客园 - 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/