Replacekeywords.cs
6.9 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Model;
using Newtonsoft.Json.Linq;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using Http = Model.Http;
namespace BLL
{
//请求处理关键字
public class Replacekeywords
{
public static string Replacekeywordss(Dictionary<string, string> macroKey)
{
string jsondata = null;
Config config = new Config();
#region 验证配置数据是否正确
//未找到任何ns100设置的关键字信息
if (macroKey == null || macroKey.Count < 0)
{
LogNet.log.Error("更换关键字:未找到任何关键字信息,请重新设置!");
return jsondata;
}
if (config.Method != Method.POST && config.Method != Method.GET)
{
LogNet.log.Error("更换关键字:请求方式参数不正确!");
return jsondata;
}
if (config.IsTokenRequired && config.Tokenurl == null)
{
LogNet.log.Error("更换关键字:需要请求token,请填写tokenurl!");
return jsondata;
}
if (string.IsNullOrWhiteSpace(config.Url) || config.KeyWords.Length < 0)
{
LogNet.log.Error("更换关键字:URL和关键字不能为空");
return jsondata;
}
config.filteredData = macroKey
.Where(kvp => config.KeyWords.Contains(kvp.Key))
.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
//判断结果中是否包含指定关键字
bool allinkey = config.filteredData.Count > 0; /*== config.KeyWords.Count();*/
if (!allinkey)
{
LogNet.log.Error($"更换关键字:关键字未包含;{config.KeyWords}");
return jsondata;
}
#endregion
#region 根据请求方式,设置请求数据
// 创建一个请求
var request = new RestRequest(config.Method);
// 需要token
if (config.IsTokenRequired)
{
string username = ConfigHelper.Config.Get("SelectHttpPN_Auth_UserID", "app_neotel");
string password = ConfigHelper.Config.Get("SelectHttpPN_Auth_Password", "Neo@123");
string responseJson = Http.GetToken(config.Tokenurl, username, password);
// 解析 JSON
JObject jsonObject = JObject.Parse(responseJson);
// 提取 Token 的值
string tokenValue = (string)jsonObject["Token"];
request.AddHeader("Authorization", tokenValue);
LogNet.log.Error($"更换关键字:请求token:{tokenValue}");
}
jsondata = SendHttpRequest(config, request);
if (string.IsNullOrWhiteSpace(jsondata))
{
return jsondata;
}
#endregion
return jsondata;
}
public static string SendHttpRequest(Config config, RestRequest request)
{
string json = "";
if (request.Method == Method.GET)
{
List<string> parstringParts = new List<string>();
foreach (var item in config.KeyWords) // 客户指定的关键字
{
// 尝试从识别出来的关键字数据中获取值
if (config.filteredData.TryGetValue(item, out var value))
{
parstringParts.Add(value);
}
else
{
// 如果未找到值,则使用关键字的分割后数据
string[] parts = item.Split('=');
if (parts.Length > 1)
{
parstringParts.Add(parts[1]);
}
}
}
string parstring = string.Join(",", parstringParts);
//parstring = "Y023070966,WPG0001,1,infobar";
config.Url += $"&customloadmethodparms={parstring}&loadtype=NEXT&readonly=true&rowcap=-1";
var client = new RestClient(config.Url);
LogNet.log.Error($"更换关键字:请求Url:{config.Url}");
// 禁用自动重定向
client.FollowRedirects = false;
var response = client.Execute(request);
if ((int)response.StatusCode >= 300 && (int)response.StatusCode < 400)
{
// 获取重定向后的新 URL
string newUrl = response.Headers.FirstOrDefault(header => header.Name.Equals("Location", StringComparison.OrdinalIgnoreCase))?.Value.ToString();
// 创建一个新的 RestClient 实例,用于新的 URL
var newClient = new RestClient(newUrl);
// 发送请求到新的 URL
var newResponse = newClient.Execute(request);
json = newResponse.Content;
}
else
{
json = response.Content;
}
}
LogNet.log.Error($"更换关键字:{json}");
return json;
}
public class Config
{
//是否需要请求token
public Method Method { get; set; } = ConfigHelper.Config.Get("SelectHttpPN_RequestMethod", "GET") == "POST" ? Method.POST : Method.GET;
//是否需要请求token
public string ContentType { get; set; } = ConfigHelper.Config.Get("SelectHttpPN_ContentType", "application/json");
//是否需要请求token
public bool IsTokenRequired { get; set; } = ConfigHelper.Config.Get("SelectHttpPN_IsTokenRequired", true);
//请求tokenurl
public string Tokenurl { get; set; } = ConfigHelper.Config.Get("SelectHttpPN_Tokenurl", "");
//请求正常数据
public string Url { get; set; } = ConfigHelper.Config.Get("SelectHttpPN_Url", "");
//关键字,请用,号分割
public string[] KeyWords { get; set; } = ConfigHelper.Config.Get("SelectHttpPN_KeyWords", "").Split(',');
/// <summary>
/// 此字段在需要存放,请求数据的键值对
/// </summary>
public Dictionary<string, string> filteredData { get; set; }
}
public class ResponseItem
{
public string Name { get; set; }
public string Value { get; set; }
}
public class ResponseDatas
{
public List<List<ResponseItem>> Items { get; set; }
public string Message { get; set; }
public int MessageCode { get; set; }
}
}
}