Files
Aaru/DiscImageChef.Core/Remote.cs

97 lines
3.7 KiB
C#
Raw Normal View History

// /***************************************************************************
// The Disc Image Chef
// ----------------------------------------------------------------------------
//
// Filename : Remote.cs
// Author(s) : Natalia Portillo <claunia@claunia.com>
//
// Component : Core algorithms.
//
// --[ Description ] ----------------------------------------------------------
//
// Handles connections to DiscImageChef.Server.
//
// --[ License ] --------------------------------------------------------------
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 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/>.
//
// ----------------------------------------------------------------------------
// Copyright © 2011-2018 Natalia Portillo
// ****************************************************************************/
using System;
2017-06-03 01:18:36 +01:00
using System.IO;
using System.Net;
2017-12-19 19:33:46 +00:00
using System.Threading;
2017-12-21 14:30:38 +00:00
using System.Xml.Serialization;
using DiscImageChef.Metadata;
2017-06-03 01:18:36 +01:00
namespace DiscImageChef.Core
{
public static class Remote
{
2017-12-21 14:30:38 +00:00
public static void SubmitReport(DeviceReport report)
{
2017-06-03 01:18:36 +01:00
Thread submitThread = new Thread(() =>
{
try
{
#if DEBUG
System.Console.WriteLine("Uploading device report");
#else
DicConsole.DebugWriteLine("Submit stats", "Uploading device report");
#endif
MemoryStream xmlStream = new MemoryStream();
2017-12-21 14:30:38 +00:00
XmlSerializer xmlSer =
new XmlSerializer(typeof(DeviceReport));
2017-06-03 01:18:36 +01:00
xmlSer.Serialize(xmlStream, report);
xmlStream.Seek(0, SeekOrigin.Begin);
WebRequest request = WebRequest.Create("http://discimagechef.claunia.com/api/uploadreport");
2017-12-19 20:33:03 +00:00
((HttpWebRequest)request).UserAgent =
string.Format("DiscImageChef {0}", typeof(Version).Assembly.GetName().Version);
2017-06-03 01:18:36 +01:00
request.Method = "POST";
request.ContentLength = xmlStream.Length;
request.ContentType = "application/xml";
Stream reqStream = request.GetRequestStream();
xmlStream.CopyTo(reqStream);
reqStream.Close();
WebResponse response = request.GetResponse();
2017-12-19 20:33:03 +00:00
if(((HttpWebResponse)response).StatusCode != HttpStatusCode.OK) return;
2017-06-03 01:18:36 +01:00
Stream data = response.GetResponseStream();
StreamReader reader = new StreamReader(data ?? throw new InvalidOperationException());
2017-06-03 01:18:36 +01:00
string responseFromServer = reader.ReadToEnd();
data.Close();
response.Close();
xmlStream.Close();
}
catch(WebException)
{
// Can't connect to the server, do nothing
}
// ReSharper disable once RedundantCatchClause
2017-06-03 01:18:36 +01:00
catch
{
#if DEBUG
throw;
#endif
}
});
submitThread.Start();
}
}
2017-12-19 20:33:03 +00:00
}