OSDN Git Service

HttpTwitter.GetConfigurationメソッドをTwitterApiクラスに置き換え
[opentween/open-tween.git] / OpenTween / Connection / HttpVarious.cs
1 // OpenTween - 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_upsilon) <https://upsilo.net/~upsilon/>
8 // All rights reserved.
9 // 
10 // This file is part of OpenTween.
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 using OpenTween.Connection;
36
37
38 namespace OpenTween
39 {
40     public class HttpVarious : HttpConnection
41     {
42         public string GetRedirectTo(string url, int timeout = 5000)
43         {
44             try
45             {
46                 HttpWebRequest req = CreateRequest(HeadMethod, new Uri(url), null);
47                 req.Timeout = timeout;
48                 req.AllowAutoRedirect = false;
49                 string data;
50                 Dictionary<string, string> head = new Dictionary<string, string>();
51                 GetResponse(req, out data, head);
52
53                 string location;
54                 return head.TryGetValue("Location", out location)
55                     ? location
56                     : url;
57             }
58             catch (Exception)
59             {
60                 return url;
61             }
62         }
63
64         public Image GetImage(Uri url)
65         {
66             return GetImage(url.AbsoluteUri);
67         }
68
69         public Image GetImage(string url)
70         {
71             return GetImage(url, 10000);
72         }
73
74         public Image GetImage(string url, int timeout)
75         {
76             string errmsg;
77             return GetImage(url, "", timeout, out errmsg);
78         }
79
80         public Image GetImage(string url, string referer)
81         {
82             string errmsg;
83             return GetImage(url, referer, 10000, out errmsg);
84         }
85
86         public Image GetImage(string url, string referer, int timeout, out string errmsg)
87         {
88             return GetImageInternal(CheckValidImage, url, referer, timeout, out errmsg);
89         }
90
91         public Image GetIconImage(string url, int timeout)
92         {
93             string errmsg;
94             return GetImageInternal(CheckValidIconImage, url, "", timeout, out errmsg);
95         }
96
97         private delegate Image CheckValidImageDelegate(Image img, int width, int height);
98
99         private Image GetImageInternal(CheckValidImageDelegate CheckImage, string url, string referer, int timeout, out string errmsg)
100         {
101             try
102             {
103                 HttpWebRequest req = CreateRequest(GetMethod, new Uri(url), null);
104                 if (!String.IsNullOrEmpty(referer)) req.Referer = referer;
105                 if (timeout < 3000 || timeout > 30000)
106                 {
107                     req.Timeout = 10000;
108                 }
109                 else
110                 {
111                     req.Timeout = timeout;
112                 }
113                 Bitmap img;
114                 HttpStatusCode ret = GetResponse(req, out img, null);
115                 if (ret == HttpStatusCode.OK)
116                 {
117                     errmsg = "";
118                 }
119                 else
120                 {
121                     errmsg = ret.ToString();
122                 }
123                 if (img != null) img.Tag = url;
124                 if (ret == HttpStatusCode.OK) return CheckImage(img, img.Width, img.Height);
125                 return null;
126             }
127             catch (WebException ex)
128             {
129                 errmsg = ex.Message;
130                 return null;
131             }
132             catch (Exception)
133             {
134                 errmsg = "";
135                 return null;
136             }
137         }
138
139         public bool PostData(string Url, Dictionary<string, string> param)
140         {
141             try
142             {
143                 HttpWebRequest req = CreateRequest(PostMethod, new Uri(Url), param);
144                 HttpStatusCode res = this.GetResponse(req, null);
145                 if (res == HttpStatusCode.OK) return true;
146                 return false;
147             }
148             catch (Exception)
149             {
150                 return false;
151             }
152         }
153
154         public bool PostData(string Url, Dictionary<string, string> param, out string content)
155         {
156             try
157             {
158                 HttpWebRequest req = CreateRequest(PostMethod, new Uri(Url), param);
159                 HttpStatusCode res = this.GetResponse(req, out content, null);
160                 if (res == HttpStatusCode.OK) return true;
161                 return false;
162             }
163             catch (Exception)
164             {
165                 content = null;
166                 return false;
167             }
168         }
169
170         public bool GetData(string Url, Dictionary<string, string> param, out string content, string userAgent)
171         {
172             string errmsg;
173             return GetData(Url, param, out content, 100000, out errmsg, userAgent);
174         }
175
176         public bool GetData(string Url, Dictionary<string, string> param, out string content)
177         {
178             return GetData(Url, param, out content, 100000);
179         }
180
181         public bool GetData(string Url, Dictionary<string, string> param, out string content, int timeout)
182         {
183             string errmsg;
184             return GetData(Url, param, out content, timeout, out errmsg, "");
185         }
186
187         public bool GetData(string Url, Dictionary<string, string> param, out string content, int timeout, out string errmsg, string userAgent)
188         {
189             try
190             {
191                 HttpWebRequest req = CreateRequest(GetMethod, new Uri(Url), param);
192                 if (timeout < 3000 || timeout > 100000)
193                 {
194                     req.Timeout = 10000;
195                 }
196                 else
197                 {
198                     req.Timeout = timeout;
199                 }
200                 if (!String.IsNullOrEmpty(userAgent)) req.UserAgent = userAgent;
201                 HttpStatusCode res = this.GetResponse(req, out content, null);
202                 if (res == HttpStatusCode.OK)
203                 {
204                     errmsg = "";
205                     return true;
206                 }
207                 errmsg = res.ToString();
208                 return false;
209             }
210             catch (Exception ex)
211             {
212                 content = null;
213                 errmsg = ex.Message;
214                 return false;
215             }
216         }
217
218         public HttpStatusCode GetContent(string method, Uri Url, Dictionary<string, string> param, out string content, Dictionary<string, string> headerInfo, string userAgent)
219         {
220             //Searchで使用。呼び出し元で例外キャッチしている。
221             HttpWebRequest req = CreateRequest(method, Url, param);
222             req.UserAgent = userAgent;
223             return this.GetResponse(req, out content, headerInfo);
224         }
225
226         public bool GetDataToFile(string Url, string savePath)
227         {
228             try
229             {
230                 HttpWebRequest req = CreateRequest(GetMethod, new Uri(Url), null);
231                 req.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
232                 req.UserAgent = Networking.GetUserAgentString();
233                 using (FileStream strm = new FileStream(savePath, FileMode.Create, FileAccess.Write))
234                 {
235                     try
236                     {
237                         HttpStatusCode res = this.GetResponse(req, strm, null);
238                         if (res == HttpStatusCode.OK) return true;
239                         return false;
240                     }
241                     catch (Exception)
242                     {
243                         return false;
244                     }
245                 }
246             }
247             catch (Exception)
248             {
249                 return false;
250             }
251         }
252
253         private Image CheckValidIconImage(Image img, int width, int height)
254         {
255             return CheckValidImage(img, 48, 48);
256         }
257
258         public Image CheckValidImage(Image img, int width, int height)
259         {
260             if (img == null) return null;
261
262             Bitmap bmp = null;
263
264             try
265             {
266                 bmp = new Bitmap(width, height);
267                 using (Graphics g = Graphics.FromImage(bmp))
268                 {
269                     g.InterpolationMode = InterpolationMode.HighQualityBicubic;
270                     g.PixelOffsetMode = PixelOffsetMode.HighQuality;
271                     g.DrawImage(img, 0, 0, width, height);
272                 }
273                 bmp.Tag = img.Tag;
274
275                 Bitmap result = bmp;
276                 bmp = null; //返り値のBitmapはDisposeしない
277                 return result;
278             }
279             catch (Exception)
280             {
281                 bmp?.Dispose();
282
283                 bmp = new Bitmap(width, height);
284                 bmp.Tag = img.Tag;
285
286                 Bitmap result = bmp;
287                 bmp = null; //返り値のBitmapはDisposeしない
288                 return result;
289             }
290             finally
291             {
292                 bmp?.Dispose();
293                 img.Dispose();
294             }
295         }
296     }
297 }