FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Blog.Core/Blog.Core.Common/HttpRestSharp/HttpHelper.cs at master · githubsunyf/Blog.Core · GitHub
githubsunyf
Blog.Core
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Blog.Core
/
Blog.Core.Common
/
HttpRestSharp
/
HttpHelper.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
70 lines (60 loc) · 2.39 KB
Breadcrumbs
Blog.Core
/
Blog.Core.Common
/
HttpRestSharp
/
HttpHelper.cs
Copy path
File metadata and controls
70 lines (60 loc) · 2.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using
RestSharp
;
using
System
;
using
System
.
Collections
.
Generic
;
using
System
.
Net
;
using
System
.
Text
;
namespace
Blog
.
Core
.
Common
.
HttpRestSharp
{
/// <summary>
/// 基于 RestSharp 封装HttpHelper
/// </summary>
public
static
class
HttpHelper
{
/// <summary>
/// Get 请求
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="baseUrl">根域名:http://apk.neters.club/</param>
/// <param name="url">接口:api/xx/yy</param>
/// <param name="pragm">参数:id=2&name=老张</param>
/// <returns></returns>
public
static
T
GetApi
<
T
>
(
string
baseUrl
,
string
url
,
string
pragm
=
""
)
{
var
client
=
new
RestSharpClient
(
baseUrl
)
;
var
request
=
client
.
Execute
(
string
.
IsNullOrEmpty
(
pragm
)
?
new
RestRequest
(
url
,
Method
.
GET
)
:
new
RestRequest
(
$
"
{
url
}
?
{
pragm
}
"
,
Method
.
GET
)
)
;
if
(
request
.
StatusCode
!=
HttpStatusCode
.
OK
)
{
return
(
T
)
Convert
.
ChangeType
(
request
.
ErrorMessage
,
typeof
(
T
)
)
;
}
dynamic
temp
=
Newtonsoft
.
Json
.
JsonConvert
.
DeserializeObject
(
request
.
Content
,
typeof
(
T
)
)
;
//T result = (T)Convert.ChangeType(request.Content, typeof(T));
return
(
T
)
temp
;
}
/// <summary>
/// Post 请求
/// </summary>
/// <typeparam name="T">泛型</typeparam>
/// <param name="url">完整的url</param>
/// <param name="body">post body,可以匿名或者反序列化</param>
/// <returns></returns>
public
static
T
PostApi
<
T
>
(
string
url
,
object
body
=
null
)
{
var
client
=
new
RestClient
(
$
"
{
url
}
"
)
;
IRestRequest
queest
=
new
RestRequest
(
)
;
queest
.
Method
=
Method
.
POST
;
queest
.
AddHeader
(
"Accept"
,
"application/json"
)
;
queest
.
RequestFormat
=
DataFormat
.
Json
;
queest
.
AddBody
(
body
)
;
// 可以使用 JsonSerializer
var
result
=
client
.
Execute
(
queest
)
;
if
(
result
.
StatusCode
!=
HttpStatusCode
.
OK
)
{
return
(
T
)
Convert
.
ChangeType
(
result
.
ErrorMessage
,
typeof
(
T
)
)
;
}
dynamic
temp
=
Newtonsoft
.
Json
.
JsonConvert
.
DeserializeObject
(
result
.
Content
,
typeof
(
T
)
)
;
//T result = (T)Convert.ChangeType(request.Content, typeof(T));
return
(
T
)
temp
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL