返回在字符串中包含HTTP状态代码的JSON响应

时间:2015-09-18 02:08:38

标签: c# json

如何使用字符串中的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();**
        }
    }

1 个答案:

答案 0 :(得分:0)

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

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

return Json.Encode(statusCode + streamReader.ReadToEnd());
相关问题