1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Text;
- using System.Threading.Tasks;
- namespace cuidian.OpenApi.Http
- {
- public class Response
- {
- public Response(HttpWebResponse resp)
- {
- this.HttpWebResponse = resp;
- }
- public HttpWebResponse HttpWebResponse { get; private set; }
- /// <summary>
- /// 把响应流转换为文本。
- /// </summary>
- /// <returns>响应文本</returns>
- public string GetResponseAsString()
- {
- Encoding encoding = Encoding.GetEncoding(this.HttpWebResponse.CharacterSet);
- return this.GetResponseAsString(encoding);
- }
- /// <summary>
- /// 把响应流转换为文本。
- /// </summary>
- /// <param name="encoding">编码方式</param>
- /// <returns>响应文本</returns>
- public string GetResponseAsString(Encoding encoding)
- {
- System.IO.Stream stream = null;
- StreamReader reader = null;
- try
- {
- // 以字符流的方式读取HTTP响应
- stream = this.HttpWebResponse.GetResponseStream();
- reader = new StreamReader(stream, encoding);
- return reader.ReadToEnd();
- }
- finally
- {
- // 释放资源
- if (reader != null) reader.Close();
- if (stream != null) stream.Close();
- if (this.HttpWebResponse != null) this.HttpWebResponse.Close();
- }
- }
- }
- }
|