简体   繁体   English

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

[英]using OpenTSDB HTTP api in .NET : 400 Bad Request

I'm trying to use .net to put datapoints in OpenTSDB, using the HTTP /api/put API. 我正在尝试使用.net通过HTTP / api / put API将数据点放入OpenTSDB。 I've tried with httpclient, webRequest and HttpWebRequest. 我已经尝试使用httpclient,webRequest和HttpWebRequest。 The outcome is always 400 - bad request: chunked request not supported. 结果始终为400-错误的请求:不支持分块的请求。

I've tried my payload with an api tester (DHC) and works well. 我已经使用api测试器(DHC)尝试了有效负载,并且运行良好。 I've tried to send a very small payload (even plain wrong, like "x") but the reply is always the same. 我尝试发送非常小的有效负载(甚至是错误的,例如“ x”),但回复始终相同。

Here's one of my code instances: 这是我的代码实例之一:

    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    ;
    }

where I explicitly set to false the SendChunked property. 我在这里明确设置为false的SendChunked属性。

note that other requests, like: 请注意其他请求,例如:

 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;

        }

work flawlessly. 完美地工作。 I am sure I am doing something really wrong. 我确定我做错了什么。 I'd like to to reimplement HTTP in Sockets from scratch. 我想从头开始在套接字中重新实现HTTP。

I've found a solution I'd like to share here. 我在这里找到了想分享的解决方案。 I've used wireshark to sniff my packets, and I've found that this header is added: 我使用wireshark嗅探我的数据包,并且发现添加了此标头:

Expect: 100-continue\r\n

(see 8.2.3 of https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html ) (请参阅https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html的 8.2.3)

This is the culprit. 这是元凶。 I've read the post http://haacked.com/archive/2004/05/15/http-web-request-expect-100-continue.aspx/ by Phil Haack, and found that HttpWebRequest puts that header by default, unless you tell it to stop. 我已经阅读了Phil Haack的帖子http://haacked.com/archive/2004/05/15/http-web-request-expect-100-continue.aspx/ ,发现HttpWebRequest默认将该标头放入,除非您告诉它停止。 In this article I've found that using ServicePointManager I can do just this. 在本文中,我发现使用ServicePointManager可以做到这一点。

Putting the following code on top of my method, when declaring the http object, makes it work very well, and solves my issue: 在声明http对象时,将以下代码放在我的方法之上,使其工作得很好,并解决了我的问题:

            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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在.NET MVC API控制器中对400 post请求的错误请求 - 400 bad request for http post request in .NET MVC API controller 带有Google数据API的OAuth(.NET)返回HTTP 400:错误的请求 - OAuth with Google data API for .NET returning HTTP 400: Bad Request ASP.NET Web API HTTP 400错误请求 - ASP.NET Web API HTTP 400 Bad Request 无法使用 RestSharp 声明错误请求 [HTTP 400 错误]。 抛出 System.Net.Http.HttpRequestException - Cannot assert Bad Request [HTTP 400 error] using RestSharp. Throws System.Net.Http.HttpRequestException 使用Linkedin API时出现400错误的请求 - 400 bad request when using Linkedin api 尝试使用“ http:// localhost:61961 / Token”登录到Web API。 收到400错误的请求错误 - Trying to Login to the Web API using the “http://localhost:61961/Token”. Receiving 400 Bad Request Error ASP.NET HttpHandler可以处理http 400 - 错误请求吗? - Can an ASP.NET HttpHandler handle an http 400 - Bad Request? .net WCF获取HTTPS 400错误请求但可以正常使用Http - .net WCF GET HTTPS 400 Bad Request but works fine with Http System.Net.Http.HttpRequestException:400(错误请求)OnceSingleAsync FIREBASE - System.Net.Http.HttpRequestException: 400 (Bad Request) OnceSingleAsync FIREBASE Http 400请求.net - Http 400 Request .net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM