Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
VIZ.Framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
刘龙飞
VIZ.Framework
Commits
1c626070
Commit
1c626070
authored
Dec 08, 2022
by
liulongfei
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
HTTP扩展
parent
77eb5a22
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
159 additions
and
0 deletions
+159
-0
VIZ.Framework.Core/Core/Helper/HttpHelper.cs
+101
-0
VIZ.Framework.Core/Core/Helper/ObservableCollectionHelper.cs
+17
-0
VIZ.Framework.Core/Core/Helper/ThreadHelper.cs
+40
-0
VIZ.Framework.Core/VIZ.Framework.Core.csproj
+1
-0
No files found.
VIZ.Framework.Core/Core/Helper/HttpHelper.cs
View file @
1c626070
...
@@ -224,5 +224,105 @@ namespace VIZ.Framework.Core
...
@@ -224,5 +224,105 @@ namespace VIZ.Framework.Core
return
null
;
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
VIZ.Framework.Core/Core/Helper/ObservableCollectionHelper.cs
View file @
1c626070
...
@@ -32,5 +32,22 @@ namespace VIZ.Framework.Core
...
@@ -32,5 +32,22 @@ namespace VIZ.Framework.Core
return
result
;
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
);
}
}
}
}
}
}
VIZ.Framework.Core/Core/Helper/ThreadHelper.cs
0 → 100644
View file @
1c626070
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
);
}
});
}
}
}
VIZ.Framework.Core/VIZ.Framework.Core.csproj
View file @
1c626070
...
@@ -109,6 +109,7 @@
...
@@ -109,6 +109,7 @@
<Compile Include="Core\Helper\ObservableCollectionHelper.cs" />
<Compile Include="Core\Helper\ObservableCollectionHelper.cs" />
<Compile Include="Core\Helper\ProcessHelper.cs" />
<Compile Include="Core\Helper\ProcessHelper.cs" />
<Compile Include="Core\Helper\FPSHelper.cs" />
<Compile Include="Core\Helper\FPSHelper.cs" />
<Compile Include="Core\Helper\ThreadHelper.cs" />
<Compile Include="Core\Helper\ValueHelper.cs" />
<Compile Include="Core\Helper\ValueHelper.cs" />
<Compile Include="Core\Helper\XmlHelper.cs" />
<Compile Include="Core\Helper\XmlHelper.cs" />
<Compile Include="Core\Net\NetHelperMacInfo.cs" />
<Compile Include="Core\Net\NetHelperMacInfo.cs" />
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment