すごいはまった。
こんな感じで、JSONデータをPOSTするリクエスト。
string url = @"http://localhost/****"; using (var client = new HttpClient()) { HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url); request.Content = new StringContent(@"{***}", Encoding.UTF8, "application/json"); var response = await client.SendAsync(request); var restext = await response.Content.ReadAsStringAsync(); Console.WriteLine(restext); }
そしたら、結果が↓になる。サーバはApache。
502 Proxy Error
リバースプロキシでバックエンドに飛ばしてるんですが、
Apacheのログをdebugレベルにしてログを見ると、
# access.log "POST /hogeurl HTTP/1.1" 502 341 "- - - - - 202 Keep-Alive" # error.log [proxy_http:error] [pid 14412:tid 1408] (20014)Internal error (specific information not available): [client ::1:50107] AH01102: error reading status line from remote server 127.0.0.1:3002 [proxy_http:debug] [pid 14412:tid 1408] mod_proxy_http.c(1311): [client ::1:50107] AH01105: NOT Closing connection to client although reading from backend server 127.0.0.1:3002 failed. [proxy:error] [pid 14412:tid 1408] [client ::1:50107] AH00898: Error reading from remote server returned by /hogeurl
みたいな感じ。何もわからん。
アプリ側のログには何も書かれてないのでそもそも、アプリまで到達してないっぽい。
普通のGetはちゃんと行くし、PostManで同じPOST投げてもちゃんと上手くいく。
という事は、きっと変なヘッダがついてるんだろうと。
で、C#って発行するリクエストの最終的なHeaderってどうやって見ればいいんですかね?
Apache側でログの指定ってヘッダのキー指定で一つ一つ見るしかなさそうなので、
変なヘッダ入ってないか確認するのが出来ないのですが・・・。
良いやり方ないもんか。。。
まーいいや。結論から言うと、C#のHTTPClientで発行するPOSTにはデフォで↓がつくっぽい。
Expect: 100-Continue
拡張ヘッダという事みたいですが、これが悪さしてるっぽい。というかApacheと相性悪い?
これを送らないようにするために、↓にする。
string url = @"http://localhost/****"; using (var client = new HttpClient()) { ServicePointManager.Expect100Continue = false;//これ!! HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url); request.Content = new StringContent(@"{***}", Encoding.UTF8, "application/json"); var response = await client.SendAsync(request); var restext = await response.Content.ReadAsStringAsync(); Console.WriteLine(restext); }
もーね・・・。って感じ。
以下、上に行きつくまでに試した事。
まず、プロキシがおかしいのかとApacheでプロキシまわりのエラーが起きたときに
とりあえずやってみる設定は↓。
# httpd.conf SetEnv force-proxy-request-1.0 1 SetEnv proxy-nokeepalive 1
そしたら、↓を返すようになった。
417 Expectation Failed
そのほか、POSTManで指定してるヘッダ入れてみたり色々してみて、
この417から「Expect: 100-Continue」に行き着いた感じ。
何かこー、アレな感じですね。。。