using System; using System.Collections.Generic; using System.Text; using COSXML.Network; using System.IO; using COSXML.Transfer; using COSXML.Model.Object; namespace COSXML.Model { public class CosResult { public string Key { get; protected set; } /// /// http code /// public int httpCode; /// /// http message /// public string httpMessage; /// /// http response headers /// public Dictionary> responseHeaders; /// /// raw http response body /// public string RawContentBodyString { set; get; } /// /// check successful /// /// public bool IsSuccessful() { return httpCode >= 200 && httpCode < 300; } /// /// exchange infor between request and result /// /// internal virtual void ExternInfo(CosRequest cosRequest) { if (cosRequest is ObjectRequest) { Key = ((ObjectRequest)cosRequest).Key; } } /// /// parse status line and headers /// internal virtual void InternalParseResponseHeaders() { } /// /// parse response body, such as download files. /// /// input stream /// body mime type /// body length internal virtual void ParseResponseBody(Stream inputStream, string contentType, long contentLength) { } /// /// get result message /// /// public virtual string GetResultInfo() { StringBuilder resultBuilder = new StringBuilder(); resultBuilder.Append(httpCode).Append(" ").Append(httpMessage).Append("\n"); if (responseHeaders != null) { foreach (KeyValuePair> element in responseHeaders) { resultBuilder.Append(element.Key).Append(": ").Append(element.Value[0]).Append("\n"); } } return resultBuilder.ToString(); } } public class CosDataResult : CosResult { /// /// body数据 /// protected T _data; internal override void ParseResponseBody(Stream inputStream, string contentType, long contentLength) { if (contentLength != 0) { _data = XmlParse.Deserialize(inputStream); } } public override string GetResultInfo() { var info = base.GetResultInfo(); var methodInfo = typeof(T).GetMethod("GetInfo"); if (methodInfo != null && _data != null) { info = info + "\n" + methodInfo.Invoke(_data, null); } return info; } } }