时间:2021-07-01 10:21:17 帮助过:12人阅读
核心类:
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Net.Http; 7 using System.Security.Cryptography.X509Certificates; 8 9 public class InfluxDBClient 10 { 11 string _baseAddress; 12 string _username; 13 string _password; 14 16 17 /// <summary> 18 /// 构造函数 19 /// </summary> 20 /// <param name="baseAddress"></param> 21 /// <param name="username"></param> 22 /// <param name="password"></param> 23 public InfluxDBClient(string baseAddress, string username, string password) 24 { 25 this._baseAddress = baseAddress; 26 this._username = username; 27 this._password = password; 28 } 29 30 31 32 /// <summary> 33 /// 读 34 /// </summary> 35 /// <param name="database"></param> 36 /// <param name="sql"></param> 37 /// <returns></returns> 38 public string Query(string database, string sql) 39 { 40 string pathAndQuery = string.Format("/query?db={0}&q={1}", database, sql); 41 string url = _baseAddress + pathAndQuery; 42 43 string result = HttpHelper.Get(url, _username, _password); 44 return result; 45 } 46 49 50 51 52 /// <summary> 53 /// 写 54 /// </summary> 55 /// <param name="database"></param> 56 /// <param name="sql"></param> 57 /// <returns></returns> 58 public string Write(string database, string sql) 59 { 60 string pathAndQuery = string.Format("/write?db={0}&precision=s", database); 61 string url = _baseAddress + pathAndQuery; 62 63 string result = HttpHelper.Post(url, sql, _username, _password); 64 return result; 65 } 66 }
http帮助类
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Text;
6 using System.Threading.Tasks;
7
9 public class HttpHelper
10 {
13 /// <summary>
14 ///
15 /// </summary>
16 /// <param name="uri"></param>
17 /// <param name="username"></param>
18 /// <param name="password"></param>
19 /// <returns></returns>
20 public static string Get(string uri, string username, string password)
21 {
22 string result = string.Empty;
23
24 WebClient client = new WebClient();
25
26 if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
27 {
28 client.Credentials = GetCredentialCache(uri, username, password);
29 client.Headers.Add("Authorization", GetAuthorization(username, password));
30 }
31 return client.DownloadString(uri);
32 }
33
34
35
36
37 /// <summary>
38 ///
39 /// </summary>
40 /// <param name="uri"></param>
41 /// <param name="paramStr"></param>
42 /// <param name="username"></param>
43 /// <param name="password"></param>
44 /// <returns></returns>
45 public static string Post(string uri, string paramStr, string username, string password)
46 {
47 string result = string.Empty;
48
49 WebClient client = new WebClient();
50
51 // 采取POST方式必须加的Header
52 client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
53
54 byte[] postData = Encoding.UTF8.GetBytes(paramStr);
55
56 if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
57 {
58 client.Credentials = GetCredentialCache(uri, username, password);
59 client.Headers.Add("Authorization", GetAuthorization(username, password));
60 }
61
62 byte[] responseData = client.UploadData(uri, "POST", postData); // 得到返回字符流
63 return Encoding.UTF8.GetString(responseData);// 解码
64 }
65
66
67
68
69
70
71 private static CredentialCache GetCredentialCache(string uri, string username, string password)
72 {
73 string authorization = string.Format("{0}:{1}", username, password);
74 CredentialCache credCache = new CredentialCache();
75 credCache.Add(new Uri(uri), "Basic", new NetworkCredential(username, password));
76 return credCache;
77 }
78
79
80
81
82 private static string GetAuthorization(string username, string password)
83 {
84 string authorization = string.Format("{0}:{1}", username, password);
85 return "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(authorization));
86 }
87
88