OSDN Git Service

HttpTwitter.SendDirectMessageメソッドをTwitterApiクラスに置き換え
[opentween/open-tween.git] / OpenTween / Connection / HttpVarious.cs
index 8629104..f7cfeea 100644 (file)
@@ -4,7 +4,7 @@
 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
-//           (c) 2011      kim_upsilon (@kim_upslon) <https://upsilo.net/~upsilon/>
+//           (c) 2011      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
 // All rights reserved.
 // 
 // This file is part of OpenTween.
@@ -20,7 +20,7 @@
 // for more details. 
 // 
 // You should have received a copy of the GNU General public License along
-// with this program. if (not, see <http://www.gnu.org/licenses/>, or write to
+// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
 // Boston, MA 02110-1301, USA.
 
@@ -32,30 +32,28 @@ using System.Text;
 using System.Drawing;
 using System.IO;
 using System.Drawing.Drawing2D;
+using OpenTween.Connection;
 
 
 namespace OpenTween
 {
     public class HttpVarious : HttpConnection
     {
-        public string GetRedirectTo(string url)
+        public string GetRedirectTo(string url, int timeout = 5000)
         {
             try
             {
-                HttpWebRequest req = CreateRequest(HeadMethod, new Uri(url), null, false);
-                req.Timeout = 5000;
+                HttpWebRequest req = CreateRequest(HeadMethod, new Uri(url), null);
+                req.Timeout = timeout;
                 req.AllowAutoRedirect = false;
                 string data;
                 Dictionary<string, string> head = new Dictionary<string, string>();
-                HttpStatusCode ret = GetResponse(req, out data, head, false);
-                if (head.ContainsKey("Location"))
-                {
-                    return head["Location"];
-                }
-                else
-                {
-                    return url;
-                }
+                GetResponse(req, out data, head);
+
+                string location;
+                return head.TryGetValue("Location", out location)
+                    ? location
+                    : url;
             }
             catch (Exception)
             {
@@ -65,7 +63,7 @@ namespace OpenTween
 
         public Image GetImage(Uri url)
         {
-            return GetImage(url.ToString());
+            return GetImage(url.AbsoluteUri);
         }
 
         public Image GetImage(string url)
@@ -102,7 +100,7 @@ namespace OpenTween
         {
             try
             {
-                HttpWebRequest req = CreateRequest(GetMethod, new Uri(url), null, false);
+                HttpWebRequest req = CreateRequest(GetMethod, new Uri(url), null);
                 if (!String.IsNullOrEmpty(referer)) req.Referer = referer;
                 if (timeout < 3000 || timeout > 30000)
                 {
@@ -113,7 +111,7 @@ namespace OpenTween
                     req.Timeout = timeout;
                 }
                 Bitmap img;
-                HttpStatusCode ret = GetResponse(req, out img, null, false);
+                HttpStatusCode ret = GetResponse(req, out img, null);
                 if (ret == HttpStatusCode.OK)
                 {
                     errmsg = "";
@@ -142,8 +140,8 @@ namespace OpenTween
         {
             try
             {
-                HttpWebRequest req = CreateRequest(PostMethod, new Uri(Url), param, false);
-                HttpStatusCode res = this.GetResponse(req, null, false);
+                HttpWebRequest req = CreateRequest(PostMethod, new Uri(Url), param);
+                HttpStatusCode res = this.GetResponse(req, null);
                 if (res == HttpStatusCode.OK) return true;
                 return false;
             }
@@ -157,8 +155,8 @@ namespace OpenTween
         {
             try
             {
-                HttpWebRequest req = CreateRequest(PostMethod, new Uri(Url), param, false);
-                HttpStatusCode res = this.GetResponse(req, out content, null, false);
+                HttpWebRequest req = CreateRequest(PostMethod, new Uri(Url), param);
+                HttpStatusCode res = this.GetResponse(req, out content, null);
                 if (res == HttpStatusCode.OK) return true;
                 return false;
             }
@@ -190,7 +188,7 @@ namespace OpenTween
         {
             try
             {
-                HttpWebRequest req = CreateRequest(GetMethod, new Uri(Url), param, false);
+                HttpWebRequest req = CreateRequest(GetMethod, new Uri(Url), param);
                 if (timeout < 3000 || timeout > 100000)
                 {
                     req.Timeout = 10000;
@@ -200,7 +198,7 @@ namespace OpenTween
                     req.Timeout = timeout;
                 }
                 if (!String.IsNullOrEmpty(userAgent)) req.UserAgent = userAgent;
-                HttpStatusCode res = this.GetResponse(req, out content, null, false);
+                HttpStatusCode res = this.GetResponse(req, out content, null);
                 if (res == HttpStatusCode.OK)
                 {
                     errmsg = "";
@@ -220,30 +218,28 @@ namespace OpenTween
         public HttpStatusCode GetContent(string method, Uri Url, Dictionary<string, string> param, out string content, Dictionary<string, string> headerInfo, string userAgent)
         {
             //Searchで使用。呼び出し元で例外キャッチしている。
-            HttpWebRequest req = CreateRequest(method, Url, param, false);
+            HttpWebRequest req = CreateRequest(method, Url, param);
             req.UserAgent = userAgent;
-            return this.GetResponse(req, out content, headerInfo, false);
+            return this.GetResponse(req, out content, headerInfo);
         }
 
         public bool GetDataToFile(string Url, string savePath)
         {
             try
             {
-                HttpWebRequest req = CreateRequest(GetMethod, new Uri(Url), null, false);
+                HttpWebRequest req = CreateRequest(GetMethod, new Uri(Url), null);
                 req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
-                req.UserAgent = MyCommon.GetUserAgentString();
+                req.UserAgent = Networking.GetUserAgentString();
                 using (FileStream strm = new FileStream(savePath, FileMode.Create, FileAccess.Write))
                 {
                     try
                     {
-                        HttpStatusCode res = this.GetResponse(req, strm, null, false);
-                        strm.Close();
+                        HttpStatusCode res = this.GetResponse(req, strm, null);
                         if (res == HttpStatusCode.OK) return true;
                         return false;
                     }
                     catch (Exception)
                     {
-                        strm.Close();
                         return false;
                     }
                 }
@@ -262,9 +258,12 @@ namespace OpenTween
         public Image CheckValidImage(Image img, int width, int height)
         {
             if (img == null) return null;
-            Bitmap bmp = new Bitmap(width, height);
+
+            Bitmap bmp = null;
+
             try
             {
+                bmp = new Bitmap(width, height);
                 using (Graphics g = Graphics.FromImage(bmp))
                 {
                     g.InterpolationMode = InterpolationMode.HighQualityBicubic;
@@ -272,17 +271,25 @@ namespace OpenTween
                     g.DrawImage(img, 0, 0, width, height);
                 }
                 bmp.Tag = img.Tag;
-                return bmp;
+
+                Bitmap result = bmp;
+                bmp = null; //返り値のBitmapはDisposeしない
+                return result;
             }
             catch (Exception)
             {
-                bmp.Dispose();
+                bmp?.Dispose();
+
                 bmp = new Bitmap(width, height);
                 bmp.Tag = img.Tag;
-                return bmp;
+
+                Bitmap result = bmp;
+                bmp = null; //返り値のBitmapはDisposeしない
+                return result;
             }
             finally
             {
+                bmp?.Dispose();
                 img.Dispose();
             }
         }