简体   繁体   中英

Return JSON response inlcuding HTTP status code in a string

How to return JSON response with HTTP status code in a string.

What format to use?

I tried the code below

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

Reference the .NET framework assembly System.Web.Helpers.dll in your project.

Then use the JSON class which converts a data object to a string that is in the JavaScript Object Notation (JSON) format:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM