在 .NET 中使用 OpenTSDB HTTP api:400 错误请求

using OpenTSDB HTTP api in .NET : 400 Bad Request

我正在尝试使用 .net 将数据点放入 OpenTSDB,使用 HTTP /api/put API。 我试过 httpclient、webRequest 和 HttpWebRequest。结果始终为 400 - 错误请求:不支持分块请求。

我已经使用 api 测试仪 (DHC) 测试了我的有效负载并且运行良好。 我试图发送一个非常小的有效负载(甚至是完全错误的,比如 "x"),但回复总是一样的。

这是我的代码实例之一:

    public async static Task  PutAsync(DataPoint dataPoint)
    {
        try
        {
            HttpWebRequest http = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:4242/api/put");
            http.SendChunked = false;
            http.Method = "POST";

            http.ContentType = "application/json";

            Encoding encoder = Encoding.UTF8;
            byte[] data = encoder.GetBytes( dataPoint.ToJson() + Environment.NewLine);
            http.Method = "POST";
            http.ContentType = "application/json; charset=utf-8";
            http.ContentLength = data.Length;
            using (Stream stream = http.GetRequestStream())
            {
                stream.Write(data, 0, data.Length);
                stream.Close();
            }

            WebResponse response = http.GetResponse();

            var streamOutput = response.GetResponseStream();
            StreamReader sr = new StreamReader(streamOutput);
            string content = sr.ReadToEnd();
            Console.WriteLine(content);
        }
        catch   (WebException exc)
        {
            StreamReader reader = new StreamReader(exc.Response.GetResponseStream());
            var content = reader.ReadToEnd();
        }

                    return    ;
    }

我明确将 SendChunked 设置为 false 属性。

请注意其他请求,例如:

 public static async Task<bool> Connect(Uri uri)
        {
            HttpWebRequest http = (HttpWebRequest)WebRequest.Create("http://127.0.0.1:4242/api/version");
            http.SendChunked = false;
            http.Method = "GET";
            // http.Headers.Clear();
            //http.Headers.Add("Content-Type", "application/json");
            http.ContentType = "application/json";
            WebResponse response = http.GetResponse();

            var stream =   response.GetResponseStream();
            StreamReader sr = new StreamReader(stream);
            string content = sr.ReadToEnd();
            Console.WriteLine(content);
            return true;

        }

工作完美。 我确定我做错了什么。 我想从头开始在 Sockets 中重新实现 HTTP。

我找到了一个解决方案,我想在这里分享。 我使用 wireshark 嗅探我的数据包,我发现添加了这个 header:

Expect: 100-continue\r\n

(参见 https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html 的 8.2.3)

这就是罪魁祸首。我读过 Phil Haack 的 post http://haacked.com/archive/2004/05/15/http-web-request-expect-100-continue.aspx/,发现 HttpWebRequest 默认设置 header,除非你告诉它停止。在这篇文章中,我发现使用 ServicePointManager 我可以做到这一点。

在声明 http object 时,将以下代码放在我的方法之上,使其运行良好,并解决了我的问题:

            var uri = new Uri("http://127.0.0.1:4242/api/put");
            var spm = ServicePointManager.FindServicePoint(uri);
            spm.Expect100Continue = false;
            HttpWebRequest http = (HttpWebRequest)WebRequest.Create(uri); 
            http.SendChunked = false;