LoginSignup
5
3

More than 5 years have passed since last update.

HTTPリクエストで417エラーが出たときの対処

Posted at

HTTPエラーコード417ってなんだ?

ってかんじでしばらくハマった。
Expectation failed(417)は、拡張ステータスコードが使えない時にサーバーが返すらしい。(拡張ステータスコード?って場合は参考リンク先へ)
C#でHTTPリクエストを書くと自動でヘッダにExpect: 100-Continueがついてしまい、サーバーに断られてしまったということだ。サーバーはIISなのだが、関係ないのでしょうか・・・

それで、コードに以下を追加することで解消する。

sample.cs
using System.Net;
using System.Net.Http;

ServicePointManager.Expect100Continue = false; // HTTPエラー(417)対応

var req = new HttpRequestMessage(HttpMethod.Post, TARGET_URL);
req.Content = new StringContent(msg, Encoding.UTF8, "text/xml");
HttpClient client = new HttpClient();
var res = await client.SendAsync(req);

参考リンク

WebRequestのExpectにデフォルトで100-continueがつく件
http://d.hatena.ne.jp/tkuro/20110208/1297181393
【エラーコード】HTTPステータスコードの原因一覧
https://liginc.co.jp/web/programming/164003

5
3
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3