asp.net 如何接收post过来的json数据补充???

地菜 2014-07-01 11:53:57
关联问题 : 去了解

我用fiddle 做的测试


当: Content-Type: application/json 时
byte[] data = Request.BinaryRead(Request.TotalBytes); 无数据

但是当 Content-Type: application/js 或者 Content-Type: text/html 等其他时

byte[] data = Request.BinaryRead(Request.TotalBytes); 有数据 ,能正常读取

两种情况下 请求的相关参数都是
request TotalBytes: TotalBytes: 276
Form.Count: 0
ContentLength: 276
Params.Count: 48
Files.Count 0
Request.InputStream.length276

但是 ,Request.InputStream 用了各种方法都没有转换成字符串 ,或者说转换成空字符串

关联问题 : 去了解




...全文
2070 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
by_封爱 2014-07-01
  • 打赏
  • 举报
回复

<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler 
{
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        using (var reader = new System.IO.StreamReader(context.Request.InputStream))
        {
            String xmlData = reader.ReadToEnd();

            if (!string.IsNullOrEmpty(xmlData))
            {
                         //业务处理
             }
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }
}
当然上面是服务端 你懂的. 客户端么 我们依然可以模拟...

private string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }
其中里面包含关键字 post以及json.. 首先我为什么自信说我的代码可以呢?因为你看到的代码就是真正应用的代码.. 是硬件post到我ashx的json数据我做处理... 所以如果你有客户端模拟代码就更好 至少能看出来哪地方有问题
地菜 2014-07-01
  • 打赏
  • 举报
回复
引用 1 楼 diaodiaop 的回复:

<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
public class Handler : IHttpHandler 
{
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "application/json";
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        using (var reader = new System.IO.StreamReader(context.Request.InputStream))
        {
            String xmlData = reader.ReadToEnd();

            if (!string.IsNullOrEmpty(xmlData))
            {
                         //业务处理
             }
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }
}
当然上面是服务端 你懂的. 客户端么 我们依然可以模拟...

private string HttpPost(string Url, string postDataStr)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = Encoding.UTF8.GetByteCount(postDataStr);
            Stream myRequestStream = request.GetRequestStream();
            StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
            myStreamWriter.Write(postDataStr);
            myStreamWriter.Close();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();
            return retString;
        }
其中里面包含关键字 post以及json.. 首先我为什么自信说我的代码可以呢?因为你看到的代码就是真正应用的代码.. 是硬件post到我ashx的json数据我做处理... 所以如果你有客户端模拟代码就更好 至少能看出来哪地方有问题
fuck , Request.BinaryRead 会出问题, 但是 Request.InputStream确实没问题, 出问题的是 我先用了 Request.BinaryRead 没有把 stream.Seek(0, SeekOrigin.Begin);

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧