using System;
using System.IO;
using System.Net;
using System.Text;
namespace POSV.HttpRequest
{
///
/// 扩展HttpRequest
///
public static class HttpRequestExtensions
{
///
/// 结果返回字符串
///
/// HttpResult
/// string
public static string ToStringResult(this HttpHelper.HttpResult result) {
string encodingName = HttpHelper.GetEncodingFromHeaders(result.Header);
Encoding encoding;
if (encodingName == null)
encoding = HttpHelper.DefaultEncoding;
else {
try {
encoding = Encoding.GetEncoding(encodingName);
} catch (ArgumentException) {
encoding = Encoding.UTF8;
}
}
string rtlString = string.Empty;
using (result.Result) {
using (StreamReader reader = new StreamReader(result.Result, encoding)) {
rtlString = reader.ReadToEnd();
}
}
return rtlString;
}
///
/// 结果返回字节
///
/// HttpResult
/// byte[]
public static byte[] ToBytesResult(this HttpHelper.HttpResult result) {
byte[] bytes = new byte[result.ContentLength];
using (result.Result) {
result.Result.Read(bytes, 0, bytes.Length);
}
return bytes;
}
}
///
/// Http连接操作帮助类
///
public partial class HttpHelper
{
///
/// 请求响应结果
///
public class HttpResult
{
///
/// Http请求返回的Cookie
///
public string Cookie { get; set; }
///
/// 获取响应的字符集
///
public string CharacterSet { get; set; }
///
/// 获取用于对响应体进行编码的方法
///
public string ContentEncoding { get; set; }
///
/// Cookie对象集合
///
public CookieCollection CookieCollection { get; set; }
///
/// 返回的网络数据库,请注意部分属于不能读取。比如Length
///
public Stream Result { get; set; }
///
/// 获取请求返回的内容的长度。
///
public long ContentLength { get; set; }
///
/// 获取或设置 Content-type HTTP 标头的值。
///
public string ContentType { get; set; }
///
/// 客户端和服务器是否都已经过身份验证
///
public bool IsMutuallyAuthenticated { get; set; }
///
/// 此响应是否为从缓存中获取的
///
public bool IsFromCache { get; set; }
///
/// 获取最后一次修改响应内容的日期和时间
///
public DateTime LastModified { get; set; }
///
/// Http Verb
///
public string Method { get; set; }
///
/// Http 协议版本
///
public Version ProtocolVersion { get; set; }
///
/// 响应的服务器的名称
///
public string Server { get; set; }
///
/// header对象
///
public WebHeaderCollection Header { get; set; }
///
/// 返回状态说明
///
public string StatusDescription { get; set; }
///
/// 返回状态码,默认为OK
///
public HttpStatusCode StatusCode { get; set; }
///
/// 异常
///
public Exception Exception { get; set; }
///
/// 是否已被停止或者超时停止
///
public bool IsCanceled { get; set; }
///
/// 是否已完成
///
public bool IsCompleted { get; set; }
///
/// 是否由于未经处理异常的原因而完成
///
public bool IsFaulted { get; set; }
}
}
}