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.
10 // This file is part of OpenTween.
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)
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
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.
29 using System.Collections.Generic;
34 using System.Drawing.Drawing2D;
35 using OpenTween.Connection;
40 public class HttpVarious : HttpConnection
42 public string GetRedirectTo(string url, int timeout = 5000)
46 HttpWebRequest req = CreateRequest(HeadMethod, new Uri(url), null);
47 req.Timeout = timeout;
48 req.AllowAutoRedirect = false;
50 Dictionary<string, string> head = new Dictionary<string, string>();
51 GetResponse(req, out data, head);
54 return head.TryGetValue("Location", out location)
64 public Image GetImage(Uri url)
66 return GetImage(url.AbsoluteUri);
69 public Image GetImage(string url)
71 return GetImage(url, 10000);
74 public Image GetImage(string url, int timeout)
77 return GetImage(url, "", timeout, out errmsg);
80 public Image GetImage(string url, string referer)
83 return GetImage(url, referer, 10000, out errmsg);
86 public Image GetImage(string url, string referer, int timeout, out string errmsg)
88 return GetImageInternal(CheckValidImage, url, referer, timeout, out errmsg);
91 public Image GetIconImage(string url, int timeout)
94 return GetImageInternal(CheckValidIconImage, url, "", timeout, out errmsg);
97 private delegate Image CheckValidImageDelegate(Image img, int width, int height);
99 private Image GetImageInternal(CheckValidImageDelegate CheckImage, string url, string referer, int timeout, out string errmsg)
103 HttpWebRequest req = CreateRequest(GetMethod, new Uri(url), null);
104 if (!String.IsNullOrEmpty(referer)) req.Referer = referer;
105 if (timeout < 3000 || timeout > 30000)
111 req.Timeout = timeout;
114 HttpStatusCode ret = GetResponse(req, out img, null);
115 if (ret == HttpStatusCode.OK)
121 errmsg = ret.ToString();
123 if (img != null) img.Tag = url;
124 if (ret == HttpStatusCode.OK) return CheckImage(img, img.Width, img.Height);
127 catch (WebException ex)
139 public bool PostData(string Url, Dictionary<string, string> param)
143 HttpWebRequest req = CreateRequest(PostMethod, new Uri(Url), param);
144 HttpStatusCode res = this.GetResponse(req, null);
145 if (res == HttpStatusCode.OK) return true;
154 public bool PostData(string Url, Dictionary<string, string> param, out string content)
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;
170 public bool GetData(string Url, Dictionary<string, string> param, out string content, string userAgent)
173 return GetData(Url, param, out content, 100000, out errmsg, userAgent);
176 public bool GetData(string Url, Dictionary<string, string> param, out string content)
178 return GetData(Url, param, out content, 100000);
181 public bool GetData(string Url, Dictionary<string, string> param, out string content, int timeout)
184 return GetData(Url, param, out content, timeout, out errmsg, "");
187 public bool GetData(string Url, Dictionary<string, string> param, out string content, int timeout, out string errmsg, string userAgent)
191 HttpWebRequest req = CreateRequest(GetMethod, new Uri(Url), param);
192 if (timeout < 3000 || timeout > 100000)
198 req.Timeout = timeout;
200 if (!String.IsNullOrEmpty(userAgent)) req.UserAgent = userAgent;
201 HttpStatusCode res = this.GetResponse(req, out content, null);
202 if (res == HttpStatusCode.OK)
207 errmsg = res.ToString();
218 public HttpStatusCode GetContent(string method, Uri Url, Dictionary<string, string> param, out string content, Dictionary<string, string> headerInfo, string userAgent)
220 //Searchで使用。呼び出し元で例外キャッチしている。
221 HttpWebRequest req = CreateRequest(method, Url, param);
222 req.UserAgent = userAgent;
223 return this.GetResponse(req, out content, headerInfo);
226 public bool GetDataToFile(string Url, string savePath)
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))
237 HttpStatusCode res = this.GetResponse(req, strm, null);
238 if (res == HttpStatusCode.OK) return true;
253 private Image CheckValidIconImage(Image img, int width, int height)
255 return CheckValidImage(img, 48, 48);
258 public Image CheckValidImage(Image img, int width, int height)
260 if (img == null) return null;
266 bmp = new Bitmap(width, height);
267 using (Graphics g = Graphics.FromImage(bmp))
269 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
270 g.PixelOffsetMode = PixelOffsetMode.HighQuality;
271 g.DrawImage(img, 0, 0, width, height);
276 bmp = null; //返り値のBitmapはDisposeしない
283 bmp = new Bitmap(width, height);
287 bmp = null; //返り値のBitmapはDisposeしない