将数据从c#发布到Influxdb

时间:2015-10-13 23:40:11

标签: c# curl http-post influxdb

我有下一个建议将数据发布到InfluxDB:

curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000'

但我不知道如何在c#中做到这一点。

到目前为止,这是我的代码:

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://192.168.0.102:8086/write?db=learning");
            httpWebRequest.ContentType = "*/*";
            httpWebRequest.Method = "POST";
            httpWebRequest.UseDefaultCredentials = false;
            httpWebRequest.Accept = "*/*";
            httpWebRequest.Credentials = new NetworkCredential("user", "pass");

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "AI1";
                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

3 个答案:

答案 0 :(得分:2)

我认为这不适合我。 这是我完美的解决方案

 WebClient client = new WebClient();           
 var reponse = client.UploadString("http://localhost:8086/write?db=mydb", "cpu_load_short,host=server01,region=us-west value=0.64 1434055562000000000");

答案 1 :(得分:1)

我在这里如何决定

string data, Uri address
try
    {
         var client = new HttpClient();
         var d = new StreamContent(GenerateStreamFromString(data));
         var response = await client.PostAsync(address, d);
         response.EnsureSuccessStatusCode();
    }
catch
    {
         Console.WriteLine("Error in writing to DB");
    }

public static Stream GenerateStreamFromString(string s)
{
    MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);
    writer.Write(s);
    writer.Flush();
    stream.Position = 0;
    return stream;
}

答案 2 :(得分:0)

我会这样使用System.Net.Http.HttpClient,或者甚至更好地使用InfluxDB.NET

perm_unlink