Skip to content

Commit

Permalink
Multiple: update for httpclient
Browse files Browse the repository at this point in the history
  • Loading branch information
meee1 committed Nov 15, 2022
1 parent 854eeac commit 11a7f7a
Show file tree
Hide file tree
Showing 21 changed files with 393 additions and 630 deletions.
12 changes: 6 additions & 6 deletions ExtLibs/DroneCAN/DroneCAN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -1059,15 +1060,14 @@ public string LookForUpdate(string devicename, double hwversion, bool usebeta =

var url = String.Format("{0}{1}/{2}/{3}", server, devicename, hwversion.ToString("0.0##", CultureInfo.InvariantCulture), "firmware.bin");
Console.WriteLine("LookForUpdate at " + url);
var req = WebRequest.Create(url);
((HttpWebRequest)req).UserAgent = Assembly.GetExecutingAssembly().GetName().Name;
req.Timeout = 4000; // miliseconds
req.Method = "HEAD";
var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", Assembly.GetExecutingAssembly().GetName().Name);
client.Timeout = TimeSpan.FromSeconds(4);
var req = client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url)).Result;

try
{
var res = (HttpWebResponse)req.GetResponse();
if (res.StatusCode == HttpStatusCode.OK)
if (req.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("LookForUpdate valid url " + url);
return url;
Expand Down
87 changes: 39 additions & 48 deletions ExtLibs/Utilities/Download.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,24 @@

namespace MissionPlanner.Utilities
{
public static class DownloadExt
{
public static DateTime LastModified(this HttpContentHeaders headers)
{
if (headers.Any(h => h.Key.Equals("Last-Modified")))
return DateTime.Parse(headers.First(h => h.Key.Equals("Last-Modified")).Value.First());
return DateTime.MinValue;
}

public static int ContentLength(this HttpContentHeaders headers)
{
if (headers.Any(h => h.Key.Equals("Content-Length")))
return int.Parse(headers.First(h => h.Key.Equals("Content-Length")).Value.First());
return -1;
}
}


public class DownloadStream : Stream
{
private long _length;
Expand Down Expand Up @@ -418,43 +436,39 @@ public static bool getFilefromNet(string url, string saveto, Action<int, string>
{
lock (log)
log.Info(url);
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(url);
if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
((HttpWebRequest)request).UserAgent = Settings.Instance.UserAgent;
request.Timeout = 10000;
// Set the Method property of the request to POST.
request.Method = "GET";
var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
client.Timeout = TimeSpan.FromSeconds(10);

// Get the response.
WebResponse response = request.GetResponse();
var response = client.GetAsync(url).Result;
// Display the status.
lock (log)
log.Info(((HttpWebResponse)response).StatusDescription);
if (((HttpWebResponse)response).StatusCode != HttpStatusCode.OK)
log.Info(response.ReasonPhrase);
if (!response.IsSuccessStatusCode)
return false;

if (File.Exists(saveto))
{
DateTime lastfilewrite = new FileInfo(saveto).LastWriteTime;
DateTime lasthttpmod = ((HttpWebResponse)response).LastModified;
DateTime lasthttpmod = response.Content.Headers.LastModified();

if (lasthttpmod < lastfilewrite)
{
if (((HttpWebResponse)response).ContentLength == new FileInfo(saveto).Length)
if (response.Content.Headers.ContentLength() == new FileInfo(saveto).Length)
{
lock (log)
log.Info("got LastModified " + saveto + " " + ((HttpWebResponse)response).LastModified +
log.Info("got LastModified " + saveto + " " + (response.Content.Headers).LastModified() +
" vs " + new FileInfo(saveto).LastWriteTime);
response.Close();
return true;
}
}
}

// Get the stream containing content returned by the server.
Stream dataStream = response.GetResponseStream();
Stream dataStream = response.Content.ReadAsStreamAsync().Result;

long bytes = response.ContentLength;
long bytes = response.Content.Headers.ContentLength();
long contlen = bytes;

byte[] buf1 = new byte[1024];
Expand Down Expand Up @@ -493,7 +507,6 @@ public static bool getFilefromNet(string url, string saveto, Action<int, string>

fs.Close();
dataStream.Close();
response.Close();

if (File.Exists(saveto))
{
Expand Down Expand Up @@ -528,34 +541,16 @@ public static bool CheckHTTPFileExists(string url)
if (url == null || url == "" || uri == null)
return false;

WebRequest webRequest = WebRequest.Create(url);
if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
((HttpWebRequest)webRequest).UserAgent = Settings.Instance.UserAgent;
webRequest.Timeout = 10000; // miliseconds
webRequest.Method = "HEAD";

HttpWebResponse response = null;

try
{
response = (HttpWebResponse)webRequest.GetResponse();
result = true;
}
catch
{
}
finally
{
if (response != null)
{
response.Close();
}
}
var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", Settings.Instance.UserAgent);
client.Timeout = TimeSpan.FromSeconds(10);
var resp = client.SendAsync(new HttpRequestMessage(HttpMethod.Head, url)).Result;
return resp.IsSuccessStatusCode;

return result;
}

//https://stackoverflow.com/questions/13606523/retrieving-partial-content-using-multiple-http-requsets-to-fetch-data-via-parlle
[Obsolete]
public static void ParallelDownloadFile(string uri, string filePath, int chunkSize = 0, Action<int,string> status = null)
{
if (uri == null)
Expand Down Expand Up @@ -653,14 +648,10 @@ public static long GetFileSize(string uri)
if (fileSizeCache.ContainsKey(uri) && fileSizeCache[uri] > 0)
return fileSizeCache[uri];

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(uri);
if (!String.IsNullOrEmpty(Settings.Instance.UserAgent))
((HttpWebRequest) request).UserAgent = Settings.Instance.UserAgent;
request.Method = "GET";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
var len = response.ContentLength;
response.Close();
var responce = client.GetAsync(uri);
var len = responce.GetAwaiter().GetResult().Content.Headers.ContentLength();
fileSizeCache[uri] = len;
responce.Result.Dispose();
return len;
}
}
Expand Down
46 changes: 19 additions & 27 deletions ExtLibs/Utilities/GitHubContent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Text;
using log4net;
Expand Down Expand Up @@ -78,18 +79,13 @@ public static List<FileInfo> GetDirContent(string owner, string repo, string pat

string url = String.Format("{0}/{1}/{2}{3}", githubapiurl, owner, repo, path);

WebRequest wr = WebRequest.Create(url);
((HttpWebRequest) wr).AllowAutoRedirect = true;
((HttpWebRequest) wr).UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
var response = wr.GetResponse();
var respstream = response.GetResponseStream();

string content = new StreamReader(respstream).ReadToEnd();

respstream.Close();

//WebClient wc = new WebClient();
//string content = wc.DownloadString(url);
var handler = new HttpClientHandler()
{
AllowAutoRedirect = true
};
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
string content = client.GetStringAsync(url).GetAwaiter().GetResult();

var output = JsonConvert.DeserializeObject<object[]>(content);

Expand Down Expand Up @@ -119,26 +115,22 @@ public static byte[] GetFileContent(string owner, string repo, string path)

string url = String.Format("{0}/{1}/{2}{3}", githubapiurl, owner, repo, path);

WebRequest wr = WebRequest.Create(url);
((HttpWebRequest) wr).AllowAutoRedirect = true;
((HttpWebRequest) wr).UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
using (var response = wr.GetResponse())
var handler = new HttpClientHandler()
{
var respstream = response.GetResponseStream();
AllowAutoRedirect = true
};
var client = new HttpClient(handler);
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
string content = client.GetStringAsync(url).GetAwaiter().GetResult();

string content = new StreamReader(respstream).ReadToEnd();
Dictionary<string, object> output = JsonConvert.DeserializeObject<Dictionary<string, object>>(content);

respstream.Close();
if (output == null)
return null;

Dictionary<string, object> output = JsonConvert.DeserializeObject<Dictionary<string, object>>(content);
byte[] filecontent = Convert.FromBase64String(output["content"].ToString());

if (output == null)
return null;

byte[] filecontent = Convert.FromBase64String(output["content"].ToString());

return filecontent;
}
return filecontent;
}
}
}
32 changes: 5 additions & 27 deletions ExtLibs/Utilities/Tracking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Http;
using log4net;

namespace MissionPlanner.Utilities
Expand Down Expand Up @@ -296,12 +297,9 @@ static void track(object temp)

try
{

var httpWebRequest = (HttpWebRequest)WebRequest.Create(secureTrackingEndpoint);
httpWebRequest.ServicePoint.Expect100Continue = false;
httpWebRequest.UserAgent = productName + " " + productVersion + " ("+ Environment.OSVersion.VersionString +")";
//httpWebRequest.ContentType = "text/plain";
httpWebRequest.Method = "POST";
var client = new HttpClient();
client.DefaultRequestHeaders.Add("User-Agent", productName + " " + productVersion + " (" + Environment.OSVersion.VersionString + ")");
client.Timeout = TimeSpan.FromSeconds(10);

string data = "";

Expand All @@ -321,29 +319,9 @@ static void track(object temp)

data += "&z=" + random.Next().ToString(CultureInfo.InvariantCulture);

httpWebRequest.ContentLength = data.Length;

log.Debug(data);

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{

streamWriter.Write(data);
streamWriter.Flush();

using (var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse())
{
if (httpResponse.StatusCode >= HttpStatusCode.OK && (int) httpResponse.StatusCode < 300)
{
// response is a gif file
log.Debug(httpResponse.StatusCode);
}
else
{
log.Debug(httpResponse.StatusCode);
}
}
}
client.PostAsync(secureTrackingEndpoint, new StringContent(data));
}
catch { }
}
Expand Down
Loading

0 comments on commit 11a7f7a

Please sign in to comment.