9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

「(new HttpClient()).GetStringAsync("https://略");」が失敗する場合の対処

Last updated at Posted at 2018-06-25

現象

.Net Framework 4.5.2 on Windows 10.0.17134.112 で。

HttpClient cli = new HttpClient()
string body = await cli.GetStringAsync("https://略");

とやったら、GetStringAsync で例外 System.Net.Http.HttpRequestException が発生した。
メッセージは「この要求の送信中にエラーが発生しました。」。
HRESULT は 0x80131500 。

innerException は System.Net.WebException
メッセージは「接続が切断されました: 送信時に、予期しないエラーが発生しました。」。
HRESULT は 0x80131509 。

そのまた innerException は System.IO.IOException
メッセージは「リモート パーティがトランスポート ストリームを終了したため、認証に失敗しました。」。
HRESULT は 0x80131620 。

同URLを Chrome や Edge でアクセスするとちゃんと見えるし、証明書もちゃんとしている。
リモートパーティとは何なのか。なぜ認証に失敗するのか。

効果のあった対策( 私の場合 )

で。

効果のあった対策は new HttpClient() の前に

ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;

を実行することだった。

例外が出た環境の ServicePointManager.SecurityProtocol の値は Ssl3|Tls になっていて、TLS 1.2 が含まれていない。
一方今回アクセスしたサーバーは TLS 1.0TLS 1.1 をサポートしておらず、 TLS 1.2 をサポートしていた。

SecurityProtocolType.Tls12|= することで TLS 1.2 を 有効にしたら例外が出なくなり、ちゃんと body に値が入った。

というか。
https://qiita.com/tanj/items/31a0fd6b188952886de5
を発見したおかげで解決した。ありがとう!

9
5
1

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
9
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?