OSDN Git Service

Port HttpVarious.vb to C#
authorKimura Youichi <kim.upsilon@bucyou.net>
Wed, 30 Nov 2011 15:57:26 +0000 (00:57 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Wed, 22 Feb 2012 10:47:44 +0000 (19:47 +0900)
Tween/Connection/HttpVarious.vb [deleted file]
Tween/Tween.vbproj
TweenCS/Connection/HttpConnection.cs
TweenCS/Connection/HttpVarious.cs [new file with mode: 0644]
TweenCS/TweenCS.csproj

diff --git a/Tween/Connection/HttpVarious.vb b/Tween/Connection/HttpVarious.vb
deleted file mode 100644 (file)
index de51751..0000000
+++ /dev/null
@@ -1,216 +0,0 @@
-' Tween - Client of Twitter
-' Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
-'           (c) 2008-2011 Moz (@syo68k)
-'           (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>
-' All rights reserved.
-' 
-' This file is part of Tween.
-' 
-' This program is free software; you can redistribute it and/or modify it
-' under the terms of the GNU General Public License as published by the Free
-' Software Foundation; either version 3 of the License, or (at your option)
-' any later version.
-' 
-' This program is distributed in the hope that it will be useful, but
-' WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-' or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
-' 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
-' the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
-' Boston, MA 02110-1301, USA.
-
-Imports System.Net
-
-Public Class HttpVarious
-    Inherits HttpConnection
-
-    Public Function GetRedirectTo(ByVal url As String) As String
-        Try
-            Dim req As HttpWebRequest = CreateRequest(HeadMethod, New Uri(url), Nothing, False)
-            req.Timeout = 5000
-            req.AllowAutoRedirect = False
-            Dim data As String = ""
-            Dim head As New Dictionary(Of String, String)
-            Dim ret As HttpStatusCode = GetResponse(req, data, head, False)
-            If head.ContainsKey("Location") Then
-                Return head("Location")
-            Else
-                Return url
-            End If
-        Catch ex As Exception
-            Return url
-        End Try
-    End Function
-
-    Public Overloads Function GetImage(ByVal url As Uri) As Image
-        Return GetImage(url.ToString, "", 10000, Nothing)
-    End Function
-
-    Public Overloads Function GetImage(ByVal url As String) As Image
-        Return GetImage(url, "", 10000, Nothing)
-    End Function
-
-    Public Overloads Function GetImage(ByVal url As String, ByVal timeout As Integer) As Image
-        Return GetImage(url, "", timeout, Nothing)
-    End Function
-
-    Public Overloads Function GetImage(ByVal url As String, ByVal referer As String) As Image
-        Return GetImage(url, referer, 10000, Nothing)
-    End Function
-
-    Public Overloads Function GetImage(ByVal url As String, ByVal referer As String, ByVal timeout As Integer, ByRef errmsg As String) As Image
-        Return GetImageInternal(AddressOf CheckValidImage, url, referer, timeout, errmsg)
-    End Function
-
-    Public Function GetIconImage(ByVal url As String, ByVal timeout As Integer) As Image
-        Return GetImageInternal(AddressOf CheckValidIconImage, url, "", timeout, Nothing)
-    End Function
-
-    Private Delegate Function CheckValidImageDelegate(ByVal img As Image, ByVal width As Integer, ByVal height As Integer) As Image
-
-    Private Overloads Function GetImageInternal(ByVal CheckImage As CheckValidImageDelegate, ByVal url As String, ByVal referer As String, ByVal timeout As Integer, ByRef errmsg As String) As Image
-        Try
-            Dim req As HttpWebRequest = CreateRequest(GetMethod, New Uri(url), Nothing, False)
-            If Not String.IsNullOrEmpty(referer) Then req.Referer = referer
-            If timeout < 3000 OrElse timeout > 30000 Then
-                req.Timeout = 10000
-            Else
-                req.Timeout = timeout
-            End If
-            Dim img As Bitmap = Nothing
-            Dim ret As HttpStatusCode = GetResponse(req, img, Nothing, False)
-            If errmsg IsNot Nothing Then
-                If ret = HttpStatusCode.OK Then
-                    errmsg = ""
-                Else
-                    errmsg = ret.ToString
-                End If
-            End If
-            If img IsNot Nothing Then img.Tag = url
-            If ret = HttpStatusCode.OK Then Return CheckImage(img, img.Width, img.Height)
-            Return Nothing
-        Catch ex As WebException
-            If errmsg IsNot Nothing Then
-                errmsg = ex.Message
-            End If
-            Return Nothing
-        Catch ex As Exception
-            Return Nothing
-        End Try
-    End Function
-
-    Public Function PostData(ByVal Url As String, ByVal param As Dictionary(Of String, String)) As Boolean
-        Try
-            Dim req As HttpWebRequest = CreateRequest(PostMethod, New Uri(Url), param, False)
-            Dim res As HttpStatusCode = Me.GetResponse(req, Nothing, False)
-            If res = HttpStatusCode.OK Then Return True
-            Return False
-        Catch ex As Exception
-            Return False
-        End Try
-    End Function
-
-    Public Function PostData(ByVal Url As String, ByVal param As Dictionary(Of String, String), ByRef content As String) As Boolean
-        Try
-            Dim req As HttpWebRequest = CreateRequest(PostMethod, New Uri(Url), param, False)
-            Dim res As HttpStatusCode = Me.GetResponse(req, content, Nothing, False)
-            If res = HttpStatusCode.OK Then Return True
-            Return False
-        Catch ex As Exception
-            Return False
-        End Try
-    End Function
-
-    Public Overloads Function GetData(ByVal Url As String, ByVal param As Dictionary(Of String, String), ByRef content As String, ByVal userAgent As String) As Boolean
-        Return GetData(Url, param, content, 100000, Nothing, userAgent)
-    End Function
-
-    Public Overloads Function GetData(ByVal Url As String, ByVal param As Dictionary(Of String, String), ByRef content As String) As Boolean
-        Return GetData(Url, param, content, 100000, Nothing, "")
-    End Function
-
-    Public Overloads Function GetData(ByVal Url As String, ByVal param As Dictionary(Of String, String), ByRef content As String, ByVal timeout As Integer) As Boolean
-        Return GetData(Url, param, content, timeout, Nothing, "")
-    End Function
-
-    Public Overloads Function GetData(ByVal Url As String, ByVal param As Dictionary(Of String, String), ByRef content As String, ByVal timeout As Integer, ByRef errmsg As String, ByVal userAgent As String) As Boolean
-        Try
-            Dim req As HttpWebRequest = CreateRequest(GetMethod, New Uri(Url), param, False)
-            If timeout < 3000 OrElse timeout > 100000 Then
-                req.Timeout = 10000
-            Else
-                req.Timeout = timeout
-            End If
-            If Not String.IsNullOrEmpty(userAgent) Then req.UserAgent = userAgent
-            Dim res As HttpStatusCode = Me.GetResponse(req, content, Nothing, False)
-            If res = HttpStatusCode.OK Then Return True
-            If errmsg IsNot Nothing Then
-                errmsg = res.ToString
-            End If
-            Return False
-        Catch ex As Exception
-            If errmsg IsNot Nothing Then
-                errmsg = ex.Message
-            End If
-            Return False
-        End Try
-    End Function
-
-    Public Function GetContent(ByVal method As String, ByVal Url As Uri, ByVal param As Dictionary(Of String, String), ByRef content As String, ByVal headerInfo As Dictionary(Of String, String), ByVal userAgent As String) As HttpStatusCode
-        'Searchで使用。呼び出し元で例外キャッチしている。
-        Dim req As HttpWebRequest = CreateRequest(method, Url, param, False)
-        req.UserAgent = userAgent
-        Return Me.GetResponse(req, content, headerInfo, False)
-    End Function
-
-    Public Function GetDataToFile(ByVal Url As String, ByVal savePath As String) As Boolean
-        Try
-            Dim req As HttpWebRequest = CreateRequest(GetMethod, New Uri(Url), Nothing, False)
-            req.AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip
-            req.UserAgent = GetUserAgentString()
-            Using strm As New System.IO.FileStream(savePath, IO.FileMode.Create, IO.FileAccess.Write)
-                Try
-                    Dim res As HttpStatusCode = Me.GetResponse(req, strm, Nothing, False)
-                    strm.Close()
-                    If res = HttpStatusCode.OK Then Return True
-                    Return False
-                Catch ex As Exception
-                    strm.Close()
-                    Return False
-                End Try
-            End Using
-        Catch ex As Exception
-            Return False
-        End Try
-    End Function
-
-    Private Function CheckValidIconImage(ByVal img As Image, ByVal width As Integer, ByVal height As Integer) As Image
-        Return CheckValidImage(img, 48, 48)
-    End Function
-
-    Public Overloads Function CheckValidImage(ByVal img As Image, ByVal width As Integer, ByVal height As Integer) As Image
-        If img Is Nothing Then Return Nothing
-        Dim bmp As New Bitmap(width, height)
-        Try
-            Using g As Graphics = Graphics.FromImage(bmp)
-                g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
-                g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
-                g.DrawImage(img, 0, 0, width, height)
-            End Using
-            bmp.Tag = img.Tag
-            Return bmp
-        Catch ex As Exception
-            bmp.Dispose()
-            bmp = New Bitmap(width, height)
-            bmp.Tag = img.Tag
-            Return bmp
-        Finally
-            img.Dispose()
-        End Try
-    End Function
-
-End Class
index 22beca1..6ad394f 100644 (file)
     <Compile Include="Connection\HttpOAuthApiProxy.vb" />
     <Compile Include="Connection\HttpConnectionOAuth.vb" />
     <Compile Include="Connection\HttpTwitter.vb" />
-    <Compile Include="Connection\HttpVarious.vb" />
     <Compile Include="Connection\Plixi.vb" />
     <Compile Include="Connection\TwitPic.vb" />
     <Compile Include="Connection\imgly.vb" />
index 014039e..c997a29 100644 (file)
@@ -303,7 +303,7 @@ namespace Tween
         ///<param name="withCookie">通信にcookieを使用する</param>
         ///<returns>HTTP応答のステータスコード</returns>
         protected HttpStatusCode GetResponse(HttpWebRequest webRequest,
-                                             ref Stream contentStream,
+                                             Stream contentStream,
                                              Dictionary<string, string> headerInfo,
                                              bool withCookie)
         {
@@ -364,7 +364,7 @@ namespace Tween
         ///<param name="withCookie">通信にcookieを使用する</param>
         ///<returns>HTTP応答のステータスコード</returns>
         protected HttpStatusCode GetResponse(HttpWebRequest webRequest,
-                                             ref string contentText,
+                                             out string contentText,
                                              Dictionary<string, string> headerInfo,
                                              bool withCookie)
         {
@@ -378,7 +378,6 @@ namespace Tween
                     //リダイレクト応答の場合は、リダイレクト先を設定
                     GetHeaderInfo(webRes, headerInfo);
                     //応答のストリームをテキストに書き出し
-                    if (contentText == null) throw new ArgumentNullException("contentText");
                     using (StreamReader sr = new StreamReader(webRes.GetResponseStream()))
                     {
                         contentText = sr.ReadToEnd();
diff --git a/TweenCS/Connection/HttpVarious.cs b/TweenCS/Connection/HttpVarious.cs
new file mode 100644 (file)
index 0000000..7546aba
--- /dev/null
@@ -0,0 +1,290 @@
+// Tween - Client of Twitter
+// Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
+//           (c) 2008-2011 Moz (@syo68k)
+//           (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/>
+// All rights reserved.
+// 
+// This file is part of Tween.
+// 
+// This program is free software; you can redistribute it and/or modify it
+// under the terms of the GNU General public License as published by the Free
+// Software Foundation; either version 3 of the License, or (at your option)
+// any later version.
+// 
+// This program is distributed in the hope that it will be useful, but
+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
+// 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
+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
+// Boston, MA 02110-1301, USA.
+
+using System;
+using System.Net;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Drawing;
+using System.IO;
+using System.Drawing.Drawing2D;
+
+
+namespace Tween
+{
+    public class HttpVarious : HttpConnection
+    {
+        public string GetRedirectTo(string url)
+        {
+            try
+            {
+                HttpWebRequest req = CreateRequest(HeadMethod, new Uri(url), null, false);
+                req.Timeout = 5000;
+                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;
+                }
+            }
+            catch (Exception)
+            {
+                return url;
+            }
+        }
+
+        public Image GetImage(Uri url)
+        {
+            return GetImage(url.ToString());
+        }
+
+        public Image GetImage(string url)
+        {
+            return GetImage(url, 10000);
+        }
+
+        public Image GetImage(string url, int timeout)
+        {
+            string errmsg;
+            return GetImage(url, "", timeout, out errmsg);
+        }
+
+        public Image GetImage(string url, string referer)
+        {
+            string errmsg;
+            return GetImage(url, referer, 10000, out errmsg);
+        }
+
+        public Image GetImage(string url, string referer, int timeout, out string errmsg)
+        {
+            return GetImageInternal(CheckValidImage, url, referer, timeout, out errmsg);
+        }
+
+        public Image GetIconImage(string url, int timeout)
+        {
+            string errmsg;
+            return GetImageInternal(CheckValidIconImage, url, "", timeout, out errmsg);
+        }
+
+        private delegate Image CheckValidImageDelegate(Image img, int width, int height);
+
+        private Image GetImageInternal(CheckValidImageDelegate CheckImage, string url, string referer, int timeout, out string errmsg)
+        {
+            try
+            {
+                HttpWebRequest req = CreateRequest(GetMethod, new Uri(url), null, false);
+                if (!String.IsNullOrEmpty(referer)) req.Referer = referer;
+                if (timeout < 3000 || timeout > 30000)
+                {
+                    req.Timeout = 10000;
+                }
+                else
+                {
+                    req.Timeout = timeout;
+                }
+                Bitmap img;
+                HttpStatusCode ret = GetResponse(req, out img, null, false);
+                if (ret == HttpStatusCode.OK)
+                {
+                    errmsg = "";
+                }
+                else
+                {
+                    errmsg = ret.ToString();
+                }
+                if (img != null) img.Tag = url;
+                if (ret == HttpStatusCode.OK) return CheckImage(img, img.Width, img.Height);
+                return null;
+            }
+            catch (WebException ex)
+            {
+                errmsg = ex.Message;
+                return null;
+            }
+            catch (Exception)
+            {
+                errmsg = "";
+                return null;
+            }
+        }
+
+        public bool PostData(string Url, Dictionary<string, string> param)
+        {
+            try
+            {
+                HttpWebRequest req = CreateRequest(PostMethod, new Uri(Url), param, false);
+                HttpStatusCode res = this.GetResponse(req, null, false);
+                if (res == HttpStatusCode.OK) return true;
+                return false;
+            }
+            catch (Exception)
+            {
+                return false;
+            }
+        }
+
+        public bool PostData(string Url, Dictionary<string, string> param, out string content)
+        {
+            try
+            {
+                HttpWebRequest req = CreateRequest(PostMethod, new Uri(Url), param, false);
+                HttpStatusCode res = this.GetResponse(req, out content, null, false);
+                if (res == HttpStatusCode.OK) return true;
+                return false;
+            }
+            catch (Exception)
+            {
+                content = null;
+                return false;
+            }
+        }
+
+        public bool GetData(string Url, Dictionary<string, string> param, out string content, string userAgent)
+        {
+            string errmsg;
+            return GetData(Url, param, out content, 100000, out errmsg, userAgent);
+        }
+
+        public bool GetData(string Url, Dictionary<string, string> param, out string content)
+        {
+            return GetData(Url, param, out content, 100000);
+        }
+
+        public bool GetData(string Url, Dictionary<string, string> param, out string content, int timeout)
+        {
+            string errmsg;
+            return GetData(Url, param, out content, timeout, out errmsg, "");
+        }
+
+        public bool GetData(string Url, Dictionary<string, string> param, out string content, int timeout, out string errmsg, string userAgent)
+        {
+            try
+            {
+                HttpWebRequest req = CreateRequest(GetMethod, new Uri(Url), param, false);
+                if (timeout < 3000 || timeout > 100000)
+                {
+                    req.Timeout = 10000;
+                }
+                else
+                {
+                    req.Timeout = timeout;
+                }
+                if (!String.IsNullOrEmpty(userAgent)) req.UserAgent = userAgent;
+                HttpStatusCode res = this.GetResponse(req, out content, null, false);
+                if (res == HttpStatusCode.OK)
+                {
+                    errmsg = "";
+                    return true;
+                }
+                errmsg = res.ToString();
+                return false;
+            }
+            catch (Exception ex)
+            {
+                content = null;
+                errmsg = ex.Message;
+                return false;
+            }
+        }
+
+        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);
+            req.UserAgent = userAgent;
+            return this.GetResponse(req, out content, headerInfo, false);
+        }
+
+        public bool GetDataToFile(string Url, string savePath)
+        {
+            try
+            {
+                HttpWebRequest req = CreateRequest(GetMethod, new Uri(Url), null, false);
+                req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
+                req.UserAgent = MyCommon.GetUserAgentString();
+                using (FileStream strm = new FileStream(savePath, FileMode.Create, FileAccess.Write))
+                {
+                    try
+                    {
+                        HttpStatusCode res = this.GetResponse(req, strm, null, false);
+                        strm.Close();
+                        if (res == HttpStatusCode.OK) return true;
+                        return false;
+                    }
+                    catch (Exception)
+                    {
+                        strm.Close();
+                        return false;
+                    }
+                }
+            }
+            catch (Exception)
+            {
+                return false;
+            }
+        }
+
+        private Image CheckValidIconImage(Image img, int width, int height)
+        {
+            return CheckValidImage(img, 48, 48);
+        }
+
+        public Image CheckValidImage(Image img, int width, int height)
+        {
+            if (img == null) return null;
+            Bitmap bmp = new Bitmap(width, height);
+            try
+            {
+                using (Graphics g = Graphics.FromImage(bmp))
+                {
+                    g.InterpolationMode = InterpolationMode.HighQualityBicubic;
+                    g.PixelOffsetMode = PixelOffsetMode.HighQuality;
+                    g.DrawImage(img, 0, 0, width, height);
+                }
+                bmp.Tag = img.Tag;
+                return bmp;
+            }
+            catch (Exception)
+            {
+                bmp.Dispose();
+                bmp = new Bitmap(width, height);
+                bmp.Tag = img.Tag;
+                return bmp;
+            }
+            finally
+            {
+                img.Dispose();
+            }
+        }
+    }
+}
\ No newline at end of file
index c1b8956..3b089f8 100644 (file)
@@ -49,6 +49,7 @@
       <SubType>Component</SubType>
     </Compile>
     <Compile Include="ApiInformation.cs" />
+    <Compile Include="Connection\HttpVarious.cs" />
     <Compile Include="Connection\IHttpConnection.cs" />
     <Compile Include="Connection\IMultimediaShareService.cs" />
     <Compile Include="DataModel.cs" />