QYYEE 5 роки тому
джерело
коміт
025c1a6b07

+ 1
- 2
实践/后端/项目/12.阿里云InfluxDB使用.md Переглянути файл

@@ -2,8 +2,7 @@
2 2
 
3 3
 普通连接数据库:
4 4
 ```
5
-./influx -ssl -username influxuser
6
- -password user@influxDB -host ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com -port 3242
5
+./influx -ssl -username influxuser -password user@influxDB -host ts-uf60ncdtuve8d41w3.influxdata.rds.aliyuncs.com -port 3242
7 6
 ```
8 7
 
9 8
 以json展示:

+ 224
- 0
实践/后端/项目/15.flurl的使用.md Переглянути файл

@@ -0,0 +1,224 @@
1
+# url拼接
2
+
3
+拼接`http://www.example.com/endpoint`
4
+
5
+```
6
+//通过string的扩展方法
7
+var url = "http://www.example.com"
8
+    .AppendPathSegment("endpoint");
9
+
10
+var url = new Url("http://www.example.com").AppendPathSegment();
11
+```
12
+
13
+查询字符串拼接`http://www.example.com/endpoint/api_key=sth&max_results=sth&q=sth`
14
+
15
+```
16
+var url = "http://www.example.com"
17
+    .AppendPathSegment("endpoint")
18
+    .SetQueryParams(new {
19
+        api_key = sth,
20
+        max_results = sth,
21
+        q = sth
22
+    });
23
+```
24
+
25
+`SetQueryParams`一般会覆盖前面的设置。但是,如果为相同的query变量设置不同的值,可以传入数组或集合。例如:`http://www.example.com/endpoint/x=1&x=2&x=3`
26
+
27
+```
28
+var url = "http://www.example.com"
29
+    .SetQueryParams("x", new[]{1, 2, 3});
30
+```
31
+
32
+关于编码,`flurl`会对查询字符串进行编码,对路径部分的保留字`/`、`%`不会编码,路径部分的空格会编码,路径部分的`?`会编码。
33
+
34
+`SetQueryParam`还有一个禁用query编码的重载方法。
35
+
36
+```
37
+var url = "http://www.example.com"
38
+    .AppendPathSegment("endpoint")
39
+    .SetQueryParam("x", "don%27t%20touch%20m", true)
40
+```
41
+
42
+默认会把空格编码成`%20`,但有时候希望把空格编码成`+`。
43
+
44
+```
45
+var url = "http://www.example.com"
46
+    .AppendPathSegment("endpoint")
47
+    .SetQueryParam("x","hi")
48
+    .ToString(true);
49
+```
50
+
51
+取出url地址中的不同部分,例如`http://www.example.com/with/path?x=1&y=3#foo`
52
+
53
+```
54
+var url = new Url("http://www.example.com/with/path?x=1&y=3#foo");
55
+var path = url.Path;//http://www.example.com/with/path
56
+var query= url.Query;//x=1&y=3
57
+var paramVal = url.QueryParams["y"];//3
58
+var fragment = url.Fragment; //foo
59
+```
60
+
61
+多个url部分拼接。
62
+
63
+```
64
+//http://www.foo.com/too/many/few?x=1&y=2
65
+var url = Url.Combine("http://foo.com/","/too/","/many/","few?","x=1","y=2");
66
+
67
+```
68
+
69
+其它API
70
+```
71
+string Path{get;set;}
72
+string Query Fragment{get;set;}
73
+QueryParamCollectio QueryParams{get;}
74
+
75
+Url AppendPathSegment();
76
+Url SetQueryParam();
77
+Url RemoveQueryParams();
78
+Url SetFragment();
79
+Url RemoveFragment();
80
+Url ResetToRoot();
81
+
82
+bool IsValid();
83
+string ToString(bool encodeSpaceAsPlus);
84
+
85
+string Combine(params string[] parts);
86
+QueryParamCollection ParseQueryParams(string query);
87
+string GetRoot(string url);
88
+string DecodeQueryParamValue(string value);
89
+string EncodeQueryParamValue(object value, bool encodeSpaceAsPlus);
90
+string EncodeILlegalCharacter(strint urlPart);
91
+```
92
+
93
+# Fluent HTTP
94
+
95
+需要引用两个插件
96
+```
97
+using Flurl;
98
+using Flurl.Http;
99
+```
100
+
101
+发送一般请求:
102
+```
103
+var getResp = await "".GetAsync();
104
+```
105
+
106
+发动GET请求:
107
+```
108
+var headResp = await "".HeadAsync();
109
+```
110
+
111
+获取强类型
112
+```
113
+T poco = await "".GetJsonAsync<T>();
114
+dynamic d = await "".GetJsonAsync();
115
+var list = await "".GetJsonListAsync();
116
+```
117
+
118
+获取string
119
+```
120
+string text = await "".GetStringAsync();
121
+```
122
+
123
+获取bytes
124
+```
125
+byte[] bytes = await "".GetBytesAsync();
126
+```
127
+
128
+获取流
129
+```
130
+Stream stream = await "".GetStreamAsync();
131
+```
132
+
133
+下载文件
134
+```
135
+var path = await "http://files.foo.com/image.jpg"
136
+    .DownloadFileAsync("c:\\downloads", filename);
137
+```
138
+
139
+发送json数据
140
+```
141
+await "".PostJsonAsync(new {a=1,b=2});
142
+```
143
+
144
+设置headers
145
+```
146
+await url.WithHeader("Accept","text/plain").GetJsonAsync();
147
+await url.WithHeaders(new {Accept="text/plain", User_Agent=""}).GetJosnAsync();
148
+```
149
+
150
+设置验证
151
+```
152
+一般验证:await url.WithBasicAuth("","").GetJsonAsync();
153
+OAUth验证:await url.WithOAuthBearerToken("mytoken").GetJsonAsync();
154
+```
155
+
156
+设置过期时间
157
+```
158
+await url.WithTimeout(10).DownloadFileAsync(); //10秒
159
+await url.WithTimeout(TimeSpan.FromMinutes(2)).DownloadFileAsync();
160
+```
161
+
162
+设置cookie
163
+```
164
+await url.WithCookie("name","value", expDate).HeadAsync();
165
+await url.WithCookies(new {c1=1, c2=2}, expDate).HeadAsync();
166
+```
167
+
168
+取消请求
169
+```
170
+var cts = new CancellationTokenSource();
171
+var task = url.GetAsync(cts.Token);
172
+cts.Cancel();
173
+```
174
+
175
+发送表单数据
176
+```
177
+await "".PostUrlEncodeAsync(new {user = "", pass=""});
178
+```
179
+
180
+发送表单数据,并且处理返回结果
181
+```
182
+T poco = await url.PostJsonAsync(data).ReceiveJson<T>();
183
+dynamic d = await url.PostUrlEncodeAsync(data).ReceiveJson();
184
+string s = await url.PostUrlEncodeAsync(data).ReceiveString();
185
+```
186
+
187
+使用原生的`HttpClient`
188
+```
189
+await url.PostAsync(httpContent);
190
+await url.SendJsonAsync(HttpMethod.Options, poco);
191
+await url.SendAsync(HttpMethod.Trace, httpContent, cancellationToken,HttpCompletionOption.ResponseHeaderRead);
192
+```
193
+
194
+# 异常处理
195
+
196
+一般处理,适用于XXX异常
197
+```
198
+try{
199
+    await url.PostJsonAsync(poco);
200
+}
201
+catch(FlurlHttpTimeoutException){
202
+    LogError("timeout");
203
+}
204
+catch(FlurHttpException ex){
205
+
206
+    LogError(ex.Message);
207
+}
208
+```
209
+
210
+使用扩展方法处理异常,适用于XXX异常
211
+```
212
+catch(FlurlHttpException ex){
213
+    TError e = ex.GetResponseJson<TError>();
214
+
215
+    dynamic d = ex.GetResponseJson();
216
+}
217
+```
218
+
219
+允许除了2XX的异常
220
+```
221
+url.AllowHttpStatus(HttpStatusCode.NotFound, HttpStatusCode.Conflict).GetAsync();
222
+url.AllowHttpStatus("400-404,6xx").GetAsync();
223
+url.AllowAnyHttpStatus().GetAsync();
224
+```

Завантаження…
Відмінити
Зберегти