Commit 1c626070 by liulongfei

HTTP扩展

parent 77eb5a22
......@@ -224,5 +224,105 @@ namespace VIZ.Framework.Core
return null;
}
}
/// <summary>
/// Get请求获取流
/// </summary>
/// <param name="url">请求url</param>
/// <param name="data">请求内容</param>
/// <param name="header">Header</param>
/// <param name="cookie">Cookie</param>
/// <returns>内存流</returns>
public static MemoryStream GetStream(string url, object data, Dictionary<string, string> header, CookieContainer cookie)
{
try
{
using (HttpClientHandler handler = new HttpClientHandler())
{
using (HttpClient client = new HttpClient(handler))
{
if (cookie != null)
{
handler.UseCookies = true;
handler.CookieContainer = cookie;
}
if (header != null)
{
foreach (var kv in header)
{
client.DefaultRequestHeaders.Add(kv.Key, kv.Value);
}
}
client.Timeout = TimeSpan.FromMilliseconds(TIME_OUT);
using (HttpResponseMessage response = client.GetAsync(url).Result)
using (Stream stream = response.Content.ReadAsStreamAsync().Result)
{
MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
return ms;
}
}
}
}
catch (Exception ex)
{
log.Error(ex);
return null;
}
}
/// <summary>
/// Post请求获取流
/// </summary>
/// <param name="url">请求url</param>
/// <param name="data">请求内容</param>
/// <param name="header">Header</param>
/// <param name="cookie">Cookie</param>
/// <returns>内存流</returns>
public static MemoryStream PostStream(string url, object data, Dictionary<string, string> header, CookieContainer cookie)
{
try
{
string postJson = data == null ? string.Empty : JsonConvert.SerializeObject(data);
using (HttpClientHandler handler = new HttpClientHandler())
{
if (cookie != null)
{
handler.UseCookies = true;
handler.CookieContainer = cookie;
}
using (HttpClient client = new HttpClient(handler))
{
if (header != null)
{
foreach (var kv in header)
{
client.DefaultRequestHeaders.Add(kv.Key, kv.Value);
}
}
client.Timeout = TimeSpan.FromMilliseconds(TIME_OUT);
StringContent content = new StringContent(postJson, Encoding.UTF8);
using (HttpResponseMessage response = client.PostAsync(url, content).Result)
using (Stream stream = response.Content.ReadAsStreamAsync().Result)
{
MemoryStream ms = new MemoryStream();
stream.CopyTo(ms);
return ms;
}
}
}
}
catch (Exception ex)
{
log.Error(ex);
return null;
}
}
}
}
\ No newline at end of file
......@@ -32,5 +32,22 @@ namespace VIZ.Framework.Core
return result;
}
/// <summary>
/// 添加列表
/// </summary>
/// <typeparam name="T">子项类型</typeparam>
/// <param name="list">源</param>
/// <param name="items">要添加的项</param>
public static void AddRange<T>(this ObservableCollection<T> list, IEnumerable<T> items)
{
if (list == null || items == null)
return;
foreach (T item in items)
{
list.Add(item);
}
}
}
}
using log4net;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace VIZ.Framework.Core
{
/// <summary>
/// 线程辅助类
/// </summary>
public static class ThreadHelper
{
/// <summary>
/// 日志
/// </summary>
private static readonly ILog log = LogManager.GetLogger(typeof(ThreadHelper));
/// <summary>
/// 安全的执行任务
/// </summary>
/// <param name="action">行为</param>
/// <returns>任务</returns>
public static Task SafeRun(Action action)
{
return Task.Run(() =>
{
try
{
action();
}
catch (Exception ex)
{
log.Error(ex);
}
});
}
}
}
......@@ -109,6 +109,7 @@
<Compile Include="Core\Helper\ObservableCollectionHelper.cs" />
<Compile Include="Core\Helper\ProcessHelper.cs" />
<Compile Include="Core\Helper\FPSHelper.cs" />
<Compile Include="Core\Helper\ThreadHelper.cs" />
<Compile Include="Core\Helper\ValueHelper.cs" />
<Compile Include="Core\Helper\XmlHelper.cs" />
<Compile Include="Core\Net\NetHelperMacInfo.cs" />
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment