fork download
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Web;
  5. using System.Text;
  6.  
  7. namespace Library
  8. {
  9. // 例外処理もラップする?
  10. public class HttpWrapper : IDisposable
  11. {
  12. // 20130527更新
  13. HttpWebRequest httpReq = null;
  14. HttpWebResponse httpRes = null;
  15.  
  16. CookieCollection cookies = new CookieCollection();
  17.  
  18. // Request Property
  19. public string Accept { get; set; }
  20. //public Uri Address { get; }
  21. public bool AllowAutoRedirect { get; set; }
  22. public bool AllowWriteStreamBuffering { get; set; }
  23. // TODO: AuthenticationLevelプロパティを実装
  24. public DecompressionMethods AutomaticDecompression { get; set; }
  25. // TODO: CachePolicyプロパティを実装
  26. // TODO: ClientCertificatesプロパティ実装
  27. public string Connection { get; set; }
  28. public string ConnectionGroupName { get; set; }
  29.  
  30. public int TimeOut { get; set; }
  31. public string Proxy { get; set; }
  32. public string Referer { get; set; }
  33. public string UserAgent { get; set; }
  34.  
  35. // Response Property
  36. /// <summary> 応答の文字セットを取得します。 </summary>
  37. public string CharacterSet { get { return httpRes.CharacterSet;} }
  38.  
  39. /// <summary> 応答の本文をエンコードするために使用するメソッドを取得します。</summary>
  40. public string ContentEncoding { get { return httpRes.ContentEncoding; } }
  41. /// <summary> 要求で返されるコンテンツ長を取得します。 </summary>
  42. public long ContentLength { get { return httpRes.ContentLength; } }
  43. /// <summary> 応答のコンテンツ タイプを取得します。</summary>
  44. public string ContentType{ get { return httpRes.ContentType; } }
  45. /// <summary> この応答に関連付けられている Cookie を取得します。 </summary>
  46. public CookieCollection Cookies { get { return httpRes.Cookies; } }
  47. /// <summary> 応答に関連付けられているヘッダーをサーバーから取得します。</summary>
  48. public WebHeaderCollection Header { get { return httpRes.Headers; } }
  49. /// <summary> この応答がキャッシュから取得されたかどうかを示す Boolean 値を取得します。 </summary>
  50. public bool IsFromChache { get { return httpRes.IsFromCache; } }
  51. /// <summary> クライアントとサーバーの両方が認証されたかどうかを示す Boolean 値を取得します。</summary>
  52. public bool IsMutuallyAuthenticated { get { return httpRes.IsMutuallyAuthenticated; } }
  53. /// <summary> 応答の内容が最後に変更された日付と時刻を取得します。</summary>
  54. public DateTime LastModified { get { return httpRes.LastModified; } }
  55. /// <summary> 応答を返すために使用するメソッドを取得します。</summary>
  56. public string Method { get { return httpRes.Method; } }
  57. /// <summary> 応答で使用される HTTP プロトコルのバージョンを取得します。</summary>
  58. public Version ProtocolVersion { get { return httpRes.ProtocolVersion; } }
  59. /// <summary> 要求に応答したインターネット リソースの URI を取得します。 </summary>
  60. public Uri ResponseUri { get { return httpRes.ResponseUri; } }
  61. /// <summary> 応答を送信したサーバーの名前を取得します。</summary>
  62. public string Server { get { return httpRes.Server; } }
  63. /// <summary> 応答のステータスを取得します。</summary>
  64. public HttpStatusCode StatusCode { get { return httpRes.StatusCode; } }
  65. /// <summary> 応答で返されるステータス記述を取得します。 </summary>
  66. public string StatusDescription { get { return httpRes.StatusDescription; } }
  67.  
  68.  
  69.  
  70. #region IDisposable Interface
  71. private bool disposed = false;
  72.  
  73. public void Dispose()
  74. {
  75. Dispose(true);
  76. GC.SuppressFinalize(this);
  77. }
  78.  
  79. protected virtual void Dispose(bool disposing)
  80. {
  81. if (!disposed)
  82. if (disposing)
  83. {
  84. // マネージリソースの開放
  85. }
  86.  
  87. // アンマネージリソースの開放
  88. if (httpReq != null)
  89. httpReq = null;
  90. if (httpRes != null)
  91. httpRes = null;
  92.  
  93. disposed = true;
  94. }
  95. #endregion
  96.  
  97. public HttpWrapper(string reqUrl)
  98. {
  99. try
  100. { httpReq = (HttpWebRequest)WebRequest.Create(reqUrl); }
  101. catch (UriFormatException e)
  102. { throw new UriFormatException(e.ToString()); }
  103.  
  104. // メンバ変数を初期化
  105. Accept = null;
  106. TimeOut = 10000;
  107. Proxy = String.Empty;
  108. Referer = String.Empty;
  109. UserAgent = String.Empty;
  110.  
  111. //httpReq.CookieContainer = new CookieContainer();
  112. }
  113.  
  114. /// <summary>
  115. /// リクエストにCookieを付加するメソッド
  116. /// </summary>
  117. /// <param name="cookieName">Cookieの変数名</param>
  118. /// <param name="cookieValue">Cookieの値</param>
  119. /// <param name="cookieDomain">Cookieのドメイン</param>
  120. public void CookieAdd(string cookieName, string cookieValue, string cookieDomain)
  121. {
  122. Cookie tmpCookie = new Cookie(cookieName, cookieValue);
  123. tmpCookie.Domain = cookieDomain;
  124. // リクエストのCookieCollectionに追加
  125. cookies.Add(tmpCookie);
  126. }
  127.  
  128. public void CookieCollectionAdd(CookieCollection _cc)
  129. {
  130. cookies = _cc;
  131. }
  132.  
  133. public string GetString(System.Text.Encoding encode)
  134. {
  135. string _src = String.Empty;
  136.  
  137. if (httpReq == null)
  138. return null;
  139.  
  140. httpReq.Accept = Accept;
  141. httpReq.Timeout = TimeOut;
  142. httpReq.Referer = Referer;
  143. httpReq.UserAgent = UserAgent;
  144.  
  145. // TODO:ここバグ出るかも
  146. if (cookies != null)
  147. {
  148. httpReq.CookieContainer = new CookieContainer();
  149. httpReq.CookieContainer.Add(cookies);
  150. }
  151.  
  152. // Proxyが設定されている場合はProxyを使用
  153. if (Proxy != String.Empty)
  154. httpReq.Proxy = new WebProxy(Proxy);
  155.  
  156. try
  157. {
  158. httpRes = (HttpWebResponse)httpReq.GetResponse();
  159. }
  160. catch (WebException e)
  161. {
  162. throw new WebException(e.ToString());
  163. }
  164. // エラーの場合、詳細を例外に投げる
  165. if (400 < (int)httpRes.StatusCode)
  166. throw new WebException(httpRes.StatusDescription);
  167.  
  168. // エラーがない場合、最後まで読み出す
  169. // IOException: 転送先からデータを読み取れません。リモートホストに強制的に切断されました
  170. try
  171. {
  172. using (StreamReader sr = new StreamReader(httpRes.GetResponseStream(), encode))
  173. _src = sr.ReadToEnd();
  174. }
  175. catch (IOException e)
  176. {
  177. System.Windows.Forms.MessageBox.Show(e.ToString());
  178. }
  179.  
  180. return _src;
  181. }
  182.  
  183. /// <summary> HTTPステータスコードを返すメソッド </summary>
  184. /// <returns>HTTP ステータスコード</returns>
  185. public int? GetStatusCode()
  186. {
  187. if (httpRes == null)
  188. return null;
  189.  
  190. return (int)httpRes.StatusCode;
  191. }
  192.  
  193. public Stream GetStream()
  194. {
  195. Stream _st = null;
  196.  
  197. if (httpReq == null)
  198. return null;
  199.  
  200. httpReq.Accept = Accept;
  201. httpReq.Timeout = TimeOut;
  202. httpReq.Referer = Referer;
  203. httpReq.UserAgent = UserAgent;
  204.  
  205. // TODO:ここバグ出るかも
  206. if (cookies != null)
  207. {
  208. httpReq.CookieContainer = new CookieContainer();
  209. httpReq.CookieContainer.Add(cookies);
  210. }
  211.  
  212. if (Proxy != String.Empty)
  213. httpReq.Proxy = new WebProxy(Proxy);
  214.  
  215. // レスポンスの取得
  216. httpRes = (HttpWebResponse)httpReq.GetResponse();
  217.  
  218. // エラーの場合、詳細を例外に投げる
  219. if (400 < (int)httpRes.StatusCode)
  220. throw new WebException(httpRes.StatusDescription);
  221.  
  222. _st = httpRes.GetResponseStream();
  223.  
  224. return _st;
  225. }
  226. }
  227. }
  228.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(177,32): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
error CS5001: Program `prog.exe' does not contain a static `Main' method suitable for an entry point
Compilation failed: 2 error(s), 0 warnings
stdout
Standard output is empty