Skip to content

Commit

Permalink
Generic Uti extension to download and convert to T class
Browse files Browse the repository at this point in the history
  • Loading branch information
rappen committed Jul 11, 2024
1 parent 7a841ec commit cfcdb6e
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions Rappen.XTB.Helpers/UrlUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using Rappen.XTB.Helpers.Controls;
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Web;
using System.Windows.Forms;

Expand Down Expand Up @@ -155,6 +157,27 @@ public static string GetEntityUrl(this EntityReference entref, ConnectionDetail
return string.Empty;
}

public static T DownloadXml<T>(this Uri uri, T defaultvalue = default(T))
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
var webRequestXml = HttpWebRequest.Create(uri) as HttpWebRequest;
webRequestXml.Accept = "text/html, application/xhtml+xml, */*";
try
{
using (var response = webRequestXml.GetResponse())
using (var content = response.GetResponseStream())
using (var reader = new StreamReader(content))
{
var strContent = reader.ReadToEnd();
return (T)XmlSerializerHelper.Deserialize(strContent, typeof(T));
}
}
catch
{
return defaultvalue;
}
}

private static string GetUrl(object holder)
{
if (holder is string url && url.Trim().StartsWith("http"))
Expand Down

0 comments on commit cfcdb6e

Please sign in to comment.