• R/O
  • HTTP
  • SSH
  • HTTPS

提交

標籤
無標籤

Frequently used words (click to add to your profile)

javac++androidlinuxc#objective-cqtwindows誰得cocoapythonphprubygameguibathyscaphec翻訳omegat計画中(planning stage)frameworktwittertestdomvb.netdirectxbtronarduinopreviewerゲームエンジン

OpenTweenのfork


Commit MetaInfo

修訂5527f424eec6679f71983a0c6d75bc36cd253469 (tree)
時間2012-02-22 19:47:44
作者Kimura Youichi <kim.upsilon@bucy...>
CommiterKimura Youichi

Log Message

Port HttpVarious.vb to C#

Change Summary

差異

--- a/Tween/Connection/HttpVarious.vb
+++ /dev/null
@@ -1,216 +0,0 @@
1-' Tween - Client of Twitter
2-' Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3-' (c) 2008-2011 Moz (@syo68k)
4-' (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5-' (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6-' (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7-' All rights reserved.
8-'
9-' This file is part of Tween.
10-'
11-' This program is free software; you can redistribute it and/or modify it
12-' under the terms of the GNU General Public License as published by the Free
13-' Software Foundation; either version 3 of the License, or (at your option)
14-' any later version.
15-'
16-' This program is distributed in the hope that it will be useful, but
17-' WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18-' or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
19-' for more details.
20-'
21-' You should have received a copy of the GNU General Public License along
22-' with this program. If not, see <http://www.gnu.org/licenses/>, or write to
23-' the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
24-' Boston, MA 02110-1301, USA.
25-
26-Imports System.Net
27-
28-Public Class HttpVarious
29- Inherits HttpConnection
30-
31- Public Function GetRedirectTo(ByVal url As String) As String
32- Try
33- Dim req As HttpWebRequest = CreateRequest(HeadMethod, New Uri(url), Nothing, False)
34- req.Timeout = 5000
35- req.AllowAutoRedirect = False
36- Dim data As String = ""
37- Dim head As New Dictionary(Of String, String)
38- Dim ret As HttpStatusCode = GetResponse(req, data, head, False)
39- If head.ContainsKey("Location") Then
40- Return head("Location")
41- Else
42- Return url
43- End If
44- Catch ex As Exception
45- Return url
46- End Try
47- End Function
48-
49- Public Overloads Function GetImage(ByVal url As Uri) As Image
50- Return GetImage(url.ToString, "", 10000, Nothing)
51- End Function
52-
53- Public Overloads Function GetImage(ByVal url As String) As Image
54- Return GetImage(url, "", 10000, Nothing)
55- End Function
56-
57- Public Overloads Function GetImage(ByVal url As String, ByVal timeout As Integer) As Image
58- Return GetImage(url, "", timeout, Nothing)
59- End Function
60-
61- Public Overloads Function GetImage(ByVal url As String, ByVal referer As String) As Image
62- Return GetImage(url, referer, 10000, Nothing)
63- End Function
64-
65- Public Overloads Function GetImage(ByVal url As String, ByVal referer As String, ByVal timeout As Integer, ByRef errmsg As String) As Image
66- Return GetImageInternal(AddressOf CheckValidImage, url, referer, timeout, errmsg)
67- End Function
68-
69- Public Function GetIconImage(ByVal url As String, ByVal timeout As Integer) As Image
70- Return GetImageInternal(AddressOf CheckValidIconImage, url, "", timeout, Nothing)
71- End Function
72-
73- Private Delegate Function CheckValidImageDelegate(ByVal img As Image, ByVal width As Integer, ByVal height As Integer) As Image
74-
75- 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
76- Try
77- Dim req As HttpWebRequest = CreateRequest(GetMethod, New Uri(url), Nothing, False)
78- If Not String.IsNullOrEmpty(referer) Then req.Referer = referer
79- If timeout < 3000 OrElse timeout > 30000 Then
80- req.Timeout = 10000
81- Else
82- req.Timeout = timeout
83- End If
84- Dim img As Bitmap = Nothing
85- Dim ret As HttpStatusCode = GetResponse(req, img, Nothing, False)
86- If errmsg IsNot Nothing Then
87- If ret = HttpStatusCode.OK Then
88- errmsg = ""
89- Else
90- errmsg = ret.ToString
91- End If
92- End If
93- If img IsNot Nothing Then img.Tag = url
94- If ret = HttpStatusCode.OK Then Return CheckImage(img, img.Width, img.Height)
95- Return Nothing
96- Catch ex As WebException
97- If errmsg IsNot Nothing Then
98- errmsg = ex.Message
99- End If
100- Return Nothing
101- Catch ex As Exception
102- Return Nothing
103- End Try
104- End Function
105-
106- Public Function PostData(ByVal Url As String, ByVal param As Dictionary(Of String, String)) As Boolean
107- Try
108- Dim req As HttpWebRequest = CreateRequest(PostMethod, New Uri(Url), param, False)
109- Dim res As HttpStatusCode = Me.GetResponse(req, Nothing, False)
110- If res = HttpStatusCode.OK Then Return True
111- Return False
112- Catch ex As Exception
113- Return False
114- End Try
115- End Function
116-
117- Public Function PostData(ByVal Url As String, ByVal param As Dictionary(Of String, String), ByRef content As String) As Boolean
118- Try
119- Dim req As HttpWebRequest = CreateRequest(PostMethod, New Uri(Url), param, False)
120- Dim res As HttpStatusCode = Me.GetResponse(req, content, Nothing, False)
121- If res = HttpStatusCode.OK Then Return True
122- Return False
123- Catch ex As Exception
124- Return False
125- End Try
126- End Function
127-
128- 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
129- Return GetData(Url, param, content, 100000, Nothing, userAgent)
130- End Function
131-
132- Public Overloads Function GetData(ByVal Url As String, ByVal param As Dictionary(Of String, String), ByRef content As String) As Boolean
133- Return GetData(Url, param, content, 100000, Nothing, "")
134- End Function
135-
136- 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
137- Return GetData(Url, param, content, timeout, Nothing, "")
138- End Function
139-
140- 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
141- Try
142- Dim req As HttpWebRequest = CreateRequest(GetMethod, New Uri(Url), param, False)
143- If timeout < 3000 OrElse timeout > 100000 Then
144- req.Timeout = 10000
145- Else
146- req.Timeout = timeout
147- End If
148- If Not String.IsNullOrEmpty(userAgent) Then req.UserAgent = userAgent
149- Dim res As HttpStatusCode = Me.GetResponse(req, content, Nothing, False)
150- If res = HttpStatusCode.OK Then Return True
151- If errmsg IsNot Nothing Then
152- errmsg = res.ToString
153- End If
154- Return False
155- Catch ex As Exception
156- If errmsg IsNot Nothing Then
157- errmsg = ex.Message
158- End If
159- Return False
160- End Try
161- End Function
162-
163- 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
164- 'Searchで使用。呼び出し元で例外キャッチしている。
165- Dim req As HttpWebRequest = CreateRequest(method, Url, param, False)
166- req.UserAgent = userAgent
167- Return Me.GetResponse(req, content, headerInfo, False)
168- End Function
169-
170- Public Function GetDataToFile(ByVal Url As String, ByVal savePath As String) As Boolean
171- Try
172- Dim req As HttpWebRequest = CreateRequest(GetMethod, New Uri(Url), Nothing, False)
173- req.AutomaticDecompression = DecompressionMethods.Deflate Or DecompressionMethods.GZip
174- req.UserAgent = GetUserAgentString()
175- Using strm As New System.IO.FileStream(savePath, IO.FileMode.Create, IO.FileAccess.Write)
176- Try
177- Dim res As HttpStatusCode = Me.GetResponse(req, strm, Nothing, False)
178- strm.Close()
179- If res = HttpStatusCode.OK Then Return True
180- Return False
181- Catch ex As Exception
182- strm.Close()
183- Return False
184- End Try
185- End Using
186- Catch ex As Exception
187- Return False
188- End Try
189- End Function
190-
191- Private Function CheckValidIconImage(ByVal img As Image, ByVal width As Integer, ByVal height As Integer) As Image
192- Return CheckValidImage(img, 48, 48)
193- End Function
194-
195- Public Overloads Function CheckValidImage(ByVal img As Image, ByVal width As Integer, ByVal height As Integer) As Image
196- If img Is Nothing Then Return Nothing
197- Dim bmp As New Bitmap(width, height)
198- Try
199- Using g As Graphics = Graphics.FromImage(bmp)
200- g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
201- g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
202- g.DrawImage(img, 0, 0, width, height)
203- End Using
204- bmp.Tag = img.Tag
205- Return bmp
206- Catch ex As Exception
207- bmp.Dispose()
208- bmp = New Bitmap(width, height)
209- bmp.Tag = img.Tag
210- Return bmp
211- Finally
212- img.Dispose()
213- End Try
214- End Function
215-
216-End Class
--- a/Tween/Tween.vbproj
+++ b/Tween/Tween.vbproj
@@ -139,7 +139,6 @@
139139 <Compile Include="Connection\HttpOAuthApiProxy.vb" />
140140 <Compile Include="Connection\HttpConnectionOAuth.vb" />
141141 <Compile Include="Connection\HttpTwitter.vb" />
142- <Compile Include="Connection\HttpVarious.vb" />
143142 <Compile Include="Connection\Plixi.vb" />
144143 <Compile Include="Connection\TwitPic.vb" />
145144 <Compile Include="Connection\imgly.vb" />
--- a/TweenCS/Connection/HttpConnection.cs
+++ b/TweenCS/Connection/HttpConnection.cs
@@ -303,7 +303,7 @@ namespace Tween
303303 ///<param name="withCookie">通信にcookieを使用する</param>
304304 ///<returns>HTTP応答のステータスコード</returns>
305305 protected HttpStatusCode GetResponse(HttpWebRequest webRequest,
306- ref Stream contentStream,
306+ Stream contentStream,
307307 Dictionary<string, string> headerInfo,
308308 bool withCookie)
309309 {
@@ -364,7 +364,7 @@ namespace Tween
364364 ///<param name="withCookie">通信にcookieを使用する</param>
365365 ///<returns>HTTP応答のステータスコード</returns>
366366 protected HttpStatusCode GetResponse(HttpWebRequest webRequest,
367- ref string contentText,
367+ out string contentText,
368368 Dictionary<string, string> headerInfo,
369369 bool withCookie)
370370 {
@@ -378,7 +378,6 @@ namespace Tween
378378 //リダイレクト応答の場合は、リダイレクト先を設定
379379 GetHeaderInfo(webRes, headerInfo);
380380 //応答のストリームをテキストに書き出し
381- if (contentText == null) throw new ArgumentNullException("contentText");
382381 using (StreamReader sr = new StreamReader(webRes.GetResponseStream()))
383382 {
384383 contentText = sr.ReadToEnd();
--- /dev/null
+++ b/TweenCS/Connection/HttpVarious.cs
@@ -0,0 +1,290 @@
1+// Tween - Client of Twitter
2+// Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3+// (c) 2008-2011 Moz (@syo68k)
4+// (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5+// (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6+// (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7+// (c) 2011 kim_upsilon (@kim_upslon) <https://upsilo.net/~upsilon/>
8+// All rights reserved.
9+//
10+// This file is part of Tween.
11+//
12+// This program is free software; you can redistribute it and/or modify it
13+// under the terms of the GNU General public License as published by the Free
14+// Software Foundation; either version 3 of the License, or (at your option)
15+// any later version.
16+//
17+// This program is distributed in the hope that it will be useful, but
18+// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19+// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
20+// for more details.
21+//
22+// You should have received a copy of the GNU General public License along
23+// with this program. if (not, see <http://www.gnu.org/licenses/>, or write to
24+// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25+// Boston, MA 02110-1301, USA.
26+
27+using System;
28+using System.Net;
29+using System.Collections.Generic;
30+using System.Linq;
31+using System.Text;
32+using System.Drawing;
33+using System.IO;
34+using System.Drawing.Drawing2D;
35+
36+
37+namespace Tween
38+{
39+ public class HttpVarious : HttpConnection
40+ {
41+ public string GetRedirectTo(string url)
42+ {
43+ try
44+ {
45+ HttpWebRequest req = CreateRequest(HeadMethod, new Uri(url), null, false);
46+ req.Timeout = 5000;
47+ req.AllowAutoRedirect = false;
48+ string data;
49+ Dictionary<string, string> head = new Dictionary<string, string>();
50+ HttpStatusCode ret = GetResponse(req, out data, head, false);
51+ if (head.ContainsKey("Location"))
52+ {
53+ return head["Location"];
54+ }
55+ else
56+ {
57+ return url;
58+ }
59+ }
60+ catch (Exception)
61+ {
62+ return url;
63+ }
64+ }
65+
66+ public Image GetImage(Uri url)
67+ {
68+ return GetImage(url.ToString());
69+ }
70+
71+ public Image GetImage(string url)
72+ {
73+ return GetImage(url, 10000);
74+ }
75+
76+ public Image GetImage(string url, int timeout)
77+ {
78+ string errmsg;
79+ return GetImage(url, "", timeout, out errmsg);
80+ }
81+
82+ public Image GetImage(string url, string referer)
83+ {
84+ string errmsg;
85+ return GetImage(url, referer, 10000, out errmsg);
86+ }
87+
88+ public Image GetImage(string url, string referer, int timeout, out string errmsg)
89+ {
90+ return GetImageInternal(CheckValidImage, url, referer, timeout, out errmsg);
91+ }
92+
93+ public Image GetIconImage(string url, int timeout)
94+ {
95+ string errmsg;
96+ return GetImageInternal(CheckValidIconImage, url, "", timeout, out errmsg);
97+ }
98+
99+ private delegate Image CheckValidImageDelegate(Image img, int width, int height);
100+
101+ private Image GetImageInternal(CheckValidImageDelegate CheckImage, string url, string referer, int timeout, out string errmsg)
102+ {
103+ try
104+ {
105+ HttpWebRequest req = CreateRequest(GetMethod, new Uri(url), null, false);
106+ if (!String.IsNullOrEmpty(referer)) req.Referer = referer;
107+ if (timeout < 3000 || timeout > 30000)
108+ {
109+ req.Timeout = 10000;
110+ }
111+ else
112+ {
113+ req.Timeout = timeout;
114+ }
115+ Bitmap img;
116+ HttpStatusCode ret = GetResponse(req, out img, null, false);
117+ if (ret == HttpStatusCode.OK)
118+ {
119+ errmsg = "";
120+ }
121+ else
122+ {
123+ errmsg = ret.ToString();
124+ }
125+ if (img != null) img.Tag = url;
126+ if (ret == HttpStatusCode.OK) return CheckImage(img, img.Width, img.Height);
127+ return null;
128+ }
129+ catch (WebException ex)
130+ {
131+ errmsg = ex.Message;
132+ return null;
133+ }
134+ catch (Exception)
135+ {
136+ errmsg = "";
137+ return null;
138+ }
139+ }
140+
141+ public bool PostData(string Url, Dictionary<string, string> param)
142+ {
143+ try
144+ {
145+ HttpWebRequest req = CreateRequest(PostMethod, new Uri(Url), param, false);
146+ HttpStatusCode res = this.GetResponse(req, null, false);
147+ if (res == HttpStatusCode.OK) return true;
148+ return false;
149+ }
150+ catch (Exception)
151+ {
152+ return false;
153+ }
154+ }
155+
156+ public bool PostData(string Url, Dictionary<string, string> param, out string content)
157+ {
158+ try
159+ {
160+ HttpWebRequest req = CreateRequest(PostMethod, new Uri(Url), param, false);
161+ HttpStatusCode res = this.GetResponse(req, out content, null, false);
162+ if (res == HttpStatusCode.OK) return true;
163+ return false;
164+ }
165+ catch (Exception)
166+ {
167+ content = null;
168+ return false;
169+ }
170+ }
171+
172+ public bool GetData(string Url, Dictionary<string, string> param, out string content, string userAgent)
173+ {
174+ string errmsg;
175+ return GetData(Url, param, out content, 100000, out errmsg, userAgent);
176+ }
177+
178+ public bool GetData(string Url, Dictionary<string, string> param, out string content)
179+ {
180+ return GetData(Url, param, out content, 100000);
181+ }
182+
183+ public bool GetData(string Url, Dictionary<string, string> param, out string content, int timeout)
184+ {
185+ string errmsg;
186+ return GetData(Url, param, out content, timeout, out errmsg, "");
187+ }
188+
189+ public bool GetData(string Url, Dictionary<string, string> param, out string content, int timeout, out string errmsg, string userAgent)
190+ {
191+ try
192+ {
193+ HttpWebRequest req = CreateRequest(GetMethod, new Uri(Url), param, false);
194+ if (timeout < 3000 || timeout > 100000)
195+ {
196+ req.Timeout = 10000;
197+ }
198+ else
199+ {
200+ req.Timeout = timeout;
201+ }
202+ if (!String.IsNullOrEmpty(userAgent)) req.UserAgent = userAgent;
203+ HttpStatusCode res = this.GetResponse(req, out content, null, false);
204+ if (res == HttpStatusCode.OK)
205+ {
206+ errmsg = "";
207+ return true;
208+ }
209+ errmsg = res.ToString();
210+ return false;
211+ }
212+ catch (Exception ex)
213+ {
214+ content = null;
215+ errmsg = ex.Message;
216+ return false;
217+ }
218+ }
219+
220+ public HttpStatusCode GetContent(string method, Uri Url, Dictionary<string, string> param, out string content, Dictionary<string, string> headerInfo, string userAgent)
221+ {
222+ //Searchで使用。呼び出し元で例外キャッチしている。
223+ HttpWebRequest req = CreateRequest(method, Url, param, false);
224+ req.UserAgent = userAgent;
225+ return this.GetResponse(req, out content, headerInfo, false);
226+ }
227+
228+ public bool GetDataToFile(string Url, string savePath)
229+ {
230+ try
231+ {
232+ HttpWebRequest req = CreateRequest(GetMethod, new Uri(Url), null, false);
233+ req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
234+ req.UserAgent = MyCommon.GetUserAgentString();
235+ using (FileStream strm = new FileStream(savePath, FileMode.Create, FileAccess.Write))
236+ {
237+ try
238+ {
239+ HttpStatusCode res = this.GetResponse(req, strm, null, false);
240+ strm.Close();
241+ if (res == HttpStatusCode.OK) return true;
242+ return false;
243+ }
244+ catch (Exception)
245+ {
246+ strm.Close();
247+ return false;
248+ }
249+ }
250+ }
251+ catch (Exception)
252+ {
253+ return false;
254+ }
255+ }
256+
257+ private Image CheckValidIconImage(Image img, int width, int height)
258+ {
259+ return CheckValidImage(img, 48, 48);
260+ }
261+
262+ public Image CheckValidImage(Image img, int width, int height)
263+ {
264+ if (img == null) return null;
265+ Bitmap bmp = new Bitmap(width, height);
266+ try
267+ {
268+ using (Graphics g = Graphics.FromImage(bmp))
269+ {
270+ g.InterpolationMode = InterpolationMode.HighQualityBicubic;
271+ g.PixelOffsetMode = PixelOffsetMode.HighQuality;
272+ g.DrawImage(img, 0, 0, width, height);
273+ }
274+ bmp.Tag = img.Tag;
275+ return bmp;
276+ }
277+ catch (Exception)
278+ {
279+ bmp.Dispose();
280+ bmp = new Bitmap(width, height);
281+ bmp.Tag = img.Tag;
282+ return bmp;
283+ }
284+ finally
285+ {
286+ img.Dispose();
287+ }
288+ }
289+ }
290+}
\ No newline at end of file
--- a/TweenCS/TweenCS.csproj
+++ b/TweenCS/TweenCS.csproj
@@ -49,6 +49,7 @@
4949 <SubType>Component</SubType>
5050 </Compile>
5151 <Compile Include="ApiInformation.cs" />
52+ <Compile Include="Connection\HttpVarious.cs" />
5253 <Compile Include="Connection\IHttpConnection.cs" />
5354 <Compile Include="Connection\IMultimediaShareService.cs" />
5455 <Compile Include="DataModel.cs" />