mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-08-11 04:41:23 +00:00
Cookbook body links referenced sibling pages with a bare slug (e.g. `c14-keep-alive`). Under the pretty-URL layout each page lives in its own directory, so these resolve against the page's own directory and 404. Prefix them with `../` to match the convention already used in the tour and llm-app sections. Verified clean with `docs-gen check`.
1.7 KiB
1.7 KiB
title, order, status
| title | order | status |
|---|---|---|
| C04. リダイレクトを追従する | 4 | draft |
cpp-httplibはデフォルトではリダイレクト(HTTP 3xx)を追従しません。サーバーから302 Foundが返ってきても、そのままステータスコード302のレスポンスとして受け取ります。
自動で追従してほしいときは、set_follow_location(true)を呼びましょう。
リダイレクトを追従する
httplib::Client cli("http://example.com");
cli.set_follow_location(true);
auto res = cli.Get("/old-path");
if (res && res->status == 200) {
std::cout << res->body << std::endl;
}
set_follow_location(true)を設定すると、Locationヘッダーを見て新しいURLに自動でリクエストを投げ直します。最終的なレスポンスがresに入ります。
HTTPからHTTPSへのリダイレクト
httplib::Client cli("http://example.com");
cli.set_follow_location(true);
auto res = cli.Get("/");
多くのサイトはHTTPアクセスをHTTPSへリダイレクトします。set_follow_location(true)を有効にしておけば、こうしたケースも透過的に扱えます。スキームやホストが変わっても自動で追従します。
Warning: HTTPSへのリダイレクトを追従するには、cpp-httplibをOpenSSL(または他のTLSバックエンド)付きでビルドしておく必要があります。TLSサポートがないと、HTTPSへのリダイレクトは失敗します。
Note: リダイレクトを追従すると、リクエストの実行時間は伸びます。タイムアウトの設定はC12. タイムアウトを設定するを参照してください。