2016-04-25 7 views
0

HTTP/api/put APIを使用してデータ接続をOpenTSDBに配置しようとしています。 私はhttpclient、webRequest、およびHttpWebRequestを試しました。結果は常に400 - 不良要求:チャンク要求はサポートされていません。OpenTSDBを使用する.NETのHTTP api:400 Bad Request

私はapiテスター(DHC)でペイロードを試してもうまく動作しています。 私は非常に小さいペイロード( "x"のような普通の間違いでさえ)を送信しようとしましたが、返信は常に同じです。

は、ここに私のコードのインスタンスのいずれかです:私は明示的にfalse SendChunkedプロパティに設定

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

ノートのような他の要求、その:完璧

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; 

     } 

作品。 私は本当に間違ったことをしていると確信しています。 私はソケットをゼロから再実装したいと思っています。

答えて

0

私がここで共有したい解決策を見つけました。 私は自分のパケットを盗聴するのwiresharkを使用しました、と私は、このヘッダが付加されていることを発見しました:

Expect: 100-continue\r\n 

https://www.w3.org/Protocols/rfc2616/rfc2616-sec8.htmlの8.2.3を参照)

をこれが犯人です。私は投稿http://haacked.com/archive/2004/05/15/http-web-request-expect-100-continue.aspx/をPhil Haackが読んで、あなたがそれを止めるよう指示しない限り、HttpWebRequestがそのヘッダをデフォルトで置くことがわかりました。この記事では、ServicePointManagerを使用することで、これを行うことができます。私のメソッドの先頭に次のコードを置く

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; 
関連する問題