Http.cs
5.0 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using Newtonsoft.Json;
using RestSharp;
using RestSharp.Validation;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web;
namespace Model
{
public static class Http
{
public static bool Ping(string ip)
{
try
{
using System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
System.Net.NetworkInformation.PingReply result = ping.Send(ip, 2000);
bool rtn = result.Status == System.Net.NetworkInformation.IPStatus.Success;
if (rtn)
LogNet.log.Info(string.Format("Ping {0} OK", ip));
else
LogNet.log.Info(string.Format("Ping {0} timeout", ip));
return rtn;
}
catch (Exception ex)
{
LogNet.log.Error("Ping", ex);
return false;
}
}
public static string Get(string url)
{
//RestClient client = new(url) { Timeout = 60000 };
//RestRequest request = new(Method.GET);
//IRestResponse response = client.Execute(request);
//string s = response.Content;
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Get, url);
var response = client.SendAsync(request).Result;
response.EnsureSuccessStatusCode();
string s= response.Content.ReadAsStringAsync().Result;
LogNet.log.Info($"[GET][URL:{url}][Return:{s}]");
return FormatContent(s);
}
public static string Post(string url, Dictionary<string, string> param)
{
List<string> body = new();
foreach (string key in param.Keys)
body.Add(string.Format("{0}={1}", key, param[key]));
LogNet.log.Info("[Post]URL:" + url + " Body:" + string.Join(",", body));
RestClient client = new(url) { Timeout = 10000 };
RestRequest request = new(Method.POST);
request.AddHeader("Content-Type", "application/x-www-form-urlencoded");
foreach(string key in param.Keys)
request.AddParameter(key, param[key]);
IRestResponse response = client.Execute(request);
string s = response.Content;
LogNet.log.Info("Return:" + s);
return FormatContent(s);
}
public static string PostJson<T>(string url, Dictionary<string, string> headers, T jsonobject, int timeout = 10000, bool wlog=true)
{
RestClient client = new(url) { Timeout = timeout };
client.UseSerializer(new CustSerialize());
RestRequest request = new(Method.POST);
request.AddHeader("Content-Type", "application/json");
if (headers != null)
{
foreach (var head in headers)
{
request.AddHeader(head.Key, head.Value);
}
}
request.AddJsonBody(jsonobject);
IRestResponse response = client.Execute(request);
string s = response.Content;
if (wlog)
LogNet.log.Info("Return:" + s);
return s;
}
private static string FormatContent(string s)
{
if (string.IsNullOrWhiteSpace(s))
{
return "";
}
else
{
s = s.Replace("\n", "");
s = s.Replace("\r", "");
while (s.IndexOf(" ") >= 0)
s = s.Replace(" ", " ");
return s;
}
}
public static string GetToken(string url)
{
RestClient client = new(url) { Timeout = 60000 };
RestRequest request = new(Method.GET);
request.AddHeader("Userid", "app_neotel");
request.AddHeader("Password", "Neotel@478*");
request.AddHeader("Accept", "application/json");
request.AddHeader("Cookie", "ASP.NET_SessionId=5jxc55snxa5jtomdoutiw1mi");
IRestResponse response = client.Execute(request);
string s = response.Content;
LogNet.log.Info($"[GET][URL:{url}][Return:{s}]");
return FormatContent(s);
}
}
public class CustSerialize : RestSharp.Serialization.IRestSerializer
{
public string ContentType { get; set; } = "application/json";
public string[] SupportedContentTypes { get; set; } = new string[] { "application/json" };
public DataFormat DataFormat { get; set; } = DataFormat.Json;
public T Deserialize<T>(IRestResponse response)
{
return JsonConvert.DeserializeObject<T>(response.Content);
}
public string Serialize(object obj)
{
return JsonConvert.SerializeObject(obj);
}
public string Serialize(Parameter parameter)
{
return JsonConvert.SerializeObject(parameter.Value);
}
}
}