Files
TouchSocket/RRQMSocket.Http/Helper/ResponseHelper.cs
2021-05-08 00:05:30 +08:00

97 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//------------------------------------------------------------------------------
// 此代码版权归作者本人若汝棋茗所有
// 源代码使用协议遵循本仓库的开源协议及附加协议若本仓库没有设置则按MIT开源协议授权
// CSDN博客https://blog.csdn.net/qq_40374647
// 哔哩哔哩视频https://space.bilibili.com/94253567
// Gitee源代码仓库https://gitee.com/RRQM_Home
// Github源代码仓库https://github.com/RRQM
// 交流QQ群234762506
// 感谢您的下载和使用
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
using RRQMCore.Helper;
using System;
using System.IO;
namespace RRQMSocket.Http
{
/// <summary>
/// 响应扩展
/// </summary>
public static class ResponseHelper
{
/// <summary>
/// 从文件
/// </summary>
/// <param name="response"></param>
/// <param name="fileName"></param>
/// <returns></returns>
public static HttpResponse FromFile(this HttpResponse response, string fileName)
{
if (!File.Exists(fileName))
{
response.SetContent("<html><body><h1>404 -RRQM Not Found</h1></body></html>");
response.StatusCode = "404";
response.Content_Type = "text/html";
return response;
}
var content = File.ReadAllBytes(fileName);
response.SetContent(content);
return response.FromSuccess();
}
/// <summary>
/// 从Xml格式
/// </summary>
/// <param name="response"></param>
/// <param name="xmlText"></param>
/// <returns></returns>
public static HttpResponse FromXML(this HttpResponse response, string xmlText)
{
response.SetContent(xmlText);
response.Content_Type = "text/xml";
return response.FromSuccess();
}
/// <summary>
/// 从Json
/// </summary>
/// <param name="response"></param>
/// <param name="jsonText"></param>
/// <returns></returns>
public static HttpResponse FromJson(this HttpResponse response, string jsonText)
{
response.SetContent(jsonText);
response.Content_Type = "text/json";
return response.FromSuccess();
}
/// <summary>
/// 从文本
/// </summary>
/// <param name="response"></param>
/// <param name="text"></param>
/// <returns></returns>
public static HttpResponse FromText(this HttpResponse response, string text)
{
response.SetContent(text);
response.Content_Type = "text/plain";
return response.FromSuccess();
}
/// <summary>
/// 返回成功
/// </summary>
/// <param name="response"></param>
/// <returns></returns>
public static HttpResponse FromSuccess(this HttpResponse response)
{
response.StatusCode = "200";
response.SetHeader(ResponseHeader.Server, $"RRQMSocket.Http {HttpResponse.ServerVersion}");
response.SetHeader(ResponseHeader.Date, DateTime.Now.ToGMTString("r"));
return response;
}
}
}