【问题标题】:Return JSON response inlcuding HTTP status code in a string在字符串中返回包含 HTTP 状态代码的 JSON 响应
【发布时间】:2015-09-18 13:34:02
【问题描述】:

如何在字符串中返回带有 HTTP 状态代码的 JSON 响应。

使用什么格式?

我试过下面的代码

protected override string ConvertResponseToString(HttpWebResponse httpResponse)
    {
        using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream(), Encoding.UTF8))
        {
            HttpStatusCode statusCode = ((HttpWebResponse)httpResponse).StatusCode;
            **return statusCode + streamReader.ReadToEnd();**
        }
    }

【问题讨论】:

    标签: c# json


    【解决方案1】:

    在您的项目中引用 .NET 框架程序集 System.Web.Helpers.dll。

    然后使用 JSON 类将数据对象转换为 JavaScript Object Notation (JSON) 格式的字符串:

    return Json.Encode(statusCode + streamReader.ReadToEnd());
    

    【讨论】:

    • 在不引用新 DLL 的情况下是否可行?我只想以正确的格式在 JSON 响应中附加 HTTP 状态代码。
    • 我假设 http 响应已经被格式化为 Json ......然后你可以这样做(位置 0 将是 { 字符): return streamReader.ReadToEnd().Insert(1,"statusCode:" +statusCode+",");
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-18
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 2018-07-07
    • 1970-01-01
    • 2018-03-09
    相关资源
    最近更新 更多