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

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - Steven Xiao

解决Asp.net 程序在 IIS 5.1 上运行不支持转换Decimal类型小数点的问题 - Steven Xiao 把XML 文件转换为 String 字符串 - Steven Xiao C#语言规范 3.0 版 分享DotNetBar控件制作office 2007风格界面的视频教程(winform office 2007 风格) XMLHttpRequest对象 - Steven Xiao SQL 2005实现单表分页的查询语句 分享实现web用户控件调用.aspx页面里的方法(从而达到访问母页面中控件的目的) - Steven Xiao 软件开发者面试百问 分享一个DotNetBar做的三层架构的winForm程序 - Steven Xiao 分享一个不错的js提示信息代码(tooltips) - Steven Xiao - 博客园 dropdownlist实现树型结构的栏目信息 - Steven Xiao asp.net 2.0实现多语言(二) - Steven Xiao 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/