登录  | 立即注册

游客您好!登录后享受更多精彩

查看: 778|回复: 6

c#服务器无法在已发送 HTTP 标头之后设置状态。

[复制链接]

5

主题

0

回帖

45

积分

新手上路

积分
45
发表于 2024-1-26 18:07:03 | 显示全部楼层 |阅读模式
c# webapi 服务器无法在已发送 HTTP 标头之后设置状态。


                System.Web.HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.BadGateway;
                res.Add("code", 502);
                res.Add("msg", "多个存货编码中间请使用中英文逗号分割");
                byte[] resbyte = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res));
                System.Web.HttpContext.Current.Response.Headers.Add("ContentType","application/json");
                System.Web.HttpContext.Current.Response.OutputStream.Write(resbyte, 0, resbyte.Length);
                System.Web.HttpContext.Current.Response.FlushAsync();


修改为如下

                System.Web.HttpContext.Current.Response.Clear();
                System.Web.HttpContext.Current.Response.BufferOutput = true;
                System.Web.HttpContext.Current.Response.StatusCode = (int)HttpStatusCode.BadGateway;
                res.Add("code", 502);
                res.Add("msg", "多个存货编码中间请使用中英文逗号分割");
                byte[] resbyte = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(res));
                System.Web.HttpContext.Current.Response.Headers.Add("ContentType","application/json");
                System.Web.HttpContext.Current.Response.OutputStream.Write(resbyte, 0, resbyte.Length);
                System.Web.HttpContext.Current.Response.FlushAsync();

回复

使用道具 举报

0

主题

114

回帖

170

积分

注册会员

积分
170
发表于 2024-10-10 05:13:01 | 显示全部楼层
11111111111
回复

使用道具 举报

0

主题

140

回帖

209

积分

中级会员

积分
209
发表于 2024-10-10 10:46:38 | 显示全部楼层
感谢楼主分享,有你更精彩!!!
回复

使用道具 举报

0

主题

92

回帖

137

积分

注册会员

积分
137
发表于 2024-10-10 11:33:22 | 显示全部楼层
66666666666666
回复

使用道具 举报

0

主题

124

回帖

185

积分

注册会员

积分
185
发表于 2024-10-10 11:34:42 | 显示全部楼层
11111111111
回复

使用道具 举报

0

主题

108

回帖

161

积分

注册会员

积分
161
发表于 2024-10-10 12:21:30 | 显示全部楼层
66666666666666666
回复

使用道具 举报

0

主题

98

回帖

146

积分

注册会员

积分
146
发表于 2024-10-10 13:39:40 | 显示全部楼层
谢谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|小黑屋|九尾社区 ( 皖ICP备2021001835号 )

GMT+8, 2024-10-18 22:36 , Processed in 0.216496 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表