1
|
//接口地址:http://192.168.234.215:9000/CRM/GetCustomerForNewIns --查询接口
|
2
|
// http://192.168.234.215:9000/Insurance/SaveNewIns --保存保单接口
|
3
|
//接口调用方法及示例
|
4
|
///appId="A01DA1EA687C436786F950BD45CB999E"
|
5
|
///appKey="9C27CE71D3AB4593A136E0F6F51B04A9"
|
6
|
///appSecret="4B1638C8FD3D44B6B387B478EBED0C8D"
|
7
|
#region 第一步:定义返回参数、请求参数
|
8
|
public class ReturnModel
|
9
|
{
|
10
|
public string code { get; set; }
|
11
|
public string result { get; set; }
|
12
|
public bool IsSuccess { get; set; }
|
13
|
public object msg { get; set; }
|
14
|
}
|
15
|
public class PostParamModel
|
16
|
{
|
17
|
public string jsonStr { get; set; }
|
18
|
}
|
19
|
#endregion
|
20
|
|
21
|
public class Test
|
22
|
{
|
23
|
#region 第二步:定义加签方法
|
24
|
/// <summary>
|
25
|
/// 加签
|
26
|
/// </summary>
|
27
|
/// <param name="param"></param>
|
28
|
/// <param name="prefAuthorization"></param>
|
29
|
/// <returns></returns>
|
30
|
private static Dictionary<string, string> getSign(string param, string prefAuthorization = "BTBPM ")
|
31
|
{
|
32
|
//待拼接未处理的字符串
|
33
|
string strConcat = "";
|
34
|
//待生成的签名
|
35
|
string strSign = "";
|
36
|
|
37
|
//配置文件配置以下三个参数,参数值由接口提供方提供,详见上面注释
|
38
|
string appId = "";//配置文件获取或直接用上面的值
|
39
|
string appKey = "";//配置文件获取或直接用上面的值
|
40
|
string appSecret = "";//配置文件获取或直接用上面的值
|
41
|
|
42
|
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
|
43
|
int Timestamp = (int)ts.TotalSeconds;
|
44
|
|
45
|
string AppSecret = appSecret;
|
46
|
string Authorization = prefAuthorization + appId;
|
47
|
string AppKey = appKey;
|
48
|
|
49
|
string SignMethod = "md5";
|
50
|
|
51
|
#region 根据参数名称的ASCII码表的顺序排序。
|
52
|
Dictionary<string, string> headers = new Dictionary<string, string>();
|
53
|
headers.Add("Authorization", Authorization);
|
54
|
headers.Add("App-Key", AppKey);
|
55
|
headers.Add("Timestamp", Timestamp.ToString());
|
56
|
headers.Add("Sign-Method", SignMethod);
|
57
|
headers.Add("param", param);
|
58
|
|
59
|
var retHeaders = (from entry in headers
|
60
|
orderby entry.Key ascending
|
61
|
select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
|
62
|
#endregion
|
63
|
|
64
|
#region 拼接的字符串
|
65
|
foreach (KeyValuePair<string, string> k in retHeaders)
|
66
|
{
|
67
|
strConcat += k.Key + k.Value;
|
68
|
}
|
69
|
strConcat = AppSecret + strConcat + AppSecret;
|
70
|
#endregion
|
71
|
|
72
|
#region 将字符串用md5加密后,再生成大写的十六进制
|
73
|
MD5 md5 = MD5.Create();
|
74
|
byte[] bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(strConcat.ToString()));
|
75
|
|
76
|
// 将MD5输出的二进制结果转换为大写的十六进制
|
77
|
StringBuilder result = new StringBuilder();
|
78
|
|
79
|
string _md5 = BitConverter.ToString(bytes).Replace("-", "");
|
80
|
|
81
|
for (int i = 0; i < bytes.Length; i++)
|
82
|
{
|
83
|
string hex = bytes[i].ToString("X2");
|
84
|
if (hex.Length == 1)
|
85
|
{
|
86
|
result.Append("0");
|
87
|
}
|
88
|
result.Append(hex);
|
89
|
}
|
90
|
|
91
|
strSign = result.ToString();
|
92
|
#endregion
|
93
|
|
94
|
Dictionary<string, string> header = new Dictionary<string, string>();
|
95
|
|
96
|
header.Add("Authorization", Authorization);
|
97
|
header.Add("App-Key", AppKey);
|
98
|
header.Add("Timestamp", Timestamp.ToString());
|
99
|
header.Add("Sign-Method", SignMethod);
|
100
|
header.Add("Sign", strSign);
|
101
|
|
102
|
return header;
|
103
|
}
|
104
|
#endregion
|
105
|
|
106
|
#region 第三步:定义web请求方法
|
107
|
//POST方式发送得结果
|
108
|
public static string doPostRequest(out string errMsg, string url, string data, Dictionary<string, string> headerSign)
|
109
|
{
|
110
|
errMsg = "";
|
111
|
|
112
|
string strURL = url;
|
113
|
System.Net.HttpWebRequest request;
|
114
|
request = (System.Net.HttpWebRequest)WebRequest.Create(strURL);
|
115
|
request.Method = "POST";
|
116
|
request.ContentType = "application/json;charset=UTF-8";
|
117
|
|
118
|
foreach (KeyValuePair<string, string> kp in headerSign)
|
119
|
{
|
120
|
request.Headers.Add(kp.Key, kp.Value);
|
121
|
}
|
122
|
|
123
|
string paraUrlCoded = data;
|
124
|
byte[] payload;
|
125
|
payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded);
|
126
|
request.ContentLength = payload.Length;
|
127
|
Stream writer = request.GetRequestStream();
|
128
|
writer.Write(payload, 0, payload.Length);
|
129
|
writer.Close();
|
130
|
System.Net.HttpWebResponse response;
|
131
|
response = (System.Net.HttpWebResponse)request.GetResponse();
|
132
|
System.IO.Stream s;
|
133
|
s = response.GetResponseStream();
|
134
|
string StrDate = "";
|
135
|
string strValue = "";
|
136
|
StreamReader Reader = new StreamReader(s, Encoding.UTF8);
|
137
|
while ((StrDate = Reader.ReadLine()) != null)
|
138
|
{
|
139
|
strValue += StrDate + "\r\n";
|
140
|
}
|
141
|
return strValue;
|
142
|
}
|
143
|
#endregion
|
144
|
|
145
|
#region 第四步:定义接口请求方法
|
146
|
/// <summary>
|
147
|
/// 调用接口
|
148
|
/// </summary>
|
149
|
/// <param name="err_msg">请求结果信息</param>
|
150
|
/// <param name="url">请求接口地址</param>
|
151
|
/// <param name="postParam">请求参数Model,实体类模型</param>
|
152
|
/// <returns>是否成功</returns>
|
153
|
protected bool RequestToAPI<T>(out string msg, string url, T postParam)
|
154
|
{
|
155
|
msg = "";
|
156
|
string jsonStr = JsonConvert.SerializeObject(postParam);
|
157
|
Dictionary<string, string> header = new Dictionary<string, string>();
|
158
|
header = getSign(jsonStr);
|
159
|
|
160
|
PostParamModel pm = new PostParamModel();
|
161
|
pm.jsonStr = jsonStr;
|
162
|
string jsonPm = JsonConvert.SerializeObject(pm);
|
163
|
|
164
|
string ret = doPostRequest(out msg, url, jsonPm, header);
|
165
|
ReturnModel response = JsonConvert.DeserializeObject<ReturnModel>(ret);
|
166
|
if (response == null)
|
167
|
{
|
168
|
msg = "请求失败";
|
169
|
return false;
|
170
|
}
|
171
|
|
172
|
if (response.IsSuccess && response.code == "28000")
|
173
|
{
|
174
|
msg = response.result;
|
175
|
return true;
|
176
|
}
|
177
|
else
|
178
|
{
|
179
|
msg = response.result;
|
180
|
return false;
|
181
|
}
|
182
|
}
|
183
|
#endregion
|
184
|
|
185
|
//示例:
|
186
|
public class Person
|
187
|
{
|
188
|
public string Name { get; set; }
|
189
|
public int Age { get; set; }
|
190
|
public bool Sex { get; set; }
|
191
|
public decimal Height { get; set; }
|
192
|
}
|
193
|
public void Test()
|
194
|
{
|
195
|
//接口接收参数的实体模型
|
196
|
Person p = new Person
|
197
|
{
|
198
|
Name = "小明",
|
199
|
Age = 18,
|
200
|
Sex = true,
|
201
|
Height = 175
|
202
|
};
|
203
|
string msg = "";
|
204
|
bool ret = RequestToAPI(out msg, "http://192.168.1.1/CRM/TestApi", p);
|
205
|
if (ret)
|
206
|
{
|
207
|
//...
|
208
|
}
|
209
|
else
|
210
|
{
|
211
|
//...
|
212
|
}
|
213
|
}
|
214
|
}
|