2012-02-05 19:53:08 +03:00
|
|
|
using System;
|
|
|
|
using System.IO;
|
2012-02-05 09:34:36 +03:00
|
|
|
using System.Linq;
|
|
|
|
using System.Net;
|
|
|
|
using System.Text;
|
2012-02-05 19:53:08 +03:00
|
|
|
using NLog;
|
2012-02-05 09:34:36 +03:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Ninject;
|
|
|
|
using NzbDrone.Common.Contract;
|
|
|
|
|
|
|
|
namespace NzbDrone.Common
|
|
|
|
{
|
|
|
|
|
|
|
|
public class RestProvider
|
|
|
|
{
|
2012-02-05 19:53:08 +03:00
|
|
|
|
2012-03-02 04:57:36 +03:00
|
|
|
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
|
2012-02-05 19:53:08 +03:00
|
|
|
|
2012-03-07 05:59:43 +03:00
|
|
|
private readonly EnvironmentProvider _environmentProvider;
|
2012-02-05 09:34:36 +03:00
|
|
|
|
|
|
|
|
|
|
|
[Inject]
|
2012-03-07 05:59:43 +03:00
|
|
|
public RestProvider(EnvironmentProvider environmentProvider)
|
2012-02-05 09:34:36 +03:00
|
|
|
{
|
2012-03-07 05:59:43 +03:00
|
|
|
_environmentProvider = environmentProvider;
|
2012-02-05 09:34:36 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public RestProvider()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-02-05 19:53:08 +03:00
|
|
|
private const int TIMEOUT = 15000;
|
2012-02-05 09:34:36 +03:00
|
|
|
private const string METHOD = "POST";
|
|
|
|
|
|
|
|
public virtual void PostData(string url, ReportBase reportBase)
|
|
|
|
{
|
2012-03-07 05:59:43 +03:00
|
|
|
reportBase.UGuid = EnvironmentProvider.UGuid;
|
|
|
|
reportBase.Version = _environmentProvider.Version.ToString();
|
|
|
|
reportBase.IsProduction = EnvironmentProvider.IsProduction;
|
2012-02-05 09:34:36 +03:00
|
|
|
|
|
|
|
PostData(url, reportBase as object);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static void PostData(string url, object message)
|
|
|
|
{
|
2012-02-05 19:53:08 +03:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var json = JsonConvert.SerializeObject(message);
|
|
|
|
|
|
|
|
var request = (HttpWebRequest)WebRequest.Create(url);
|
|
|
|
request.Timeout = TIMEOUT;
|
|
|
|
|
|
|
|
request.Proxy = WebRequest.DefaultWebProxy;
|
|
|
|
|
|
|
|
request.KeepAlive = false;
|
|
|
|
request.ProtocolVersion = HttpVersion.Version10;
|
|
|
|
request.Method = METHOD;
|
|
|
|
request.ContentType = "application/json";
|
|
|
|
|
|
|
|
byte[] postBytes = Encoding.UTF8.GetBytes(json);
|
|
|
|
request.ContentLength = postBytes.Length;
|
|
|
|
|
|
|
|
var requestStream = request.GetRequestStream();
|
|
|
|
requestStream.Write(postBytes, 0, postBytes.Length);
|
|
|
|
requestStream.Close();
|
|
|
|
|
|
|
|
var response = (HttpWebResponse)request.GetResponse();
|
|
|
|
var streamreader = new StreamReader(response.GetResponseStream());
|
|
|
|
streamreader.Close();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
e.Data.Add("URL", url);
|
|
|
|
throw;
|
|
|
|
}
|
2012-02-05 09:34:36 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|