// (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.
// 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.
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)
{
public Image GetImage(Uri url)
{
- return GetImage(url.ToString());
+ return GetImage(url.AbsoluteUri);
}
public Image GetImage(string url)
{
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)
{
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 = "";
{
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;
}
{
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;
}
{
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;
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 = "";
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;
}
}
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;
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();
}
}