0
0

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 1 year has passed since last update.

C++でHTTP通信 HTTPヘッダを追加する<Visual Studio 2022>

Posted at

C++でサクッとHTTP通信にヘッダーを追加する。
いろんな記事を見たけど・・・
コレ!っていうサンプルが無かった!

bool Session::UpdateStatus() {
    Windows::Web::Http::HttpClient httpClient;
	Windows::Web::Http::HttpResponseMessage httpResponseMessage;
	std::wstring httpResponseBody;
    Uri requestUri{ L"<uri>"};

    //★★ここでヘッダー項目を追加している★★
    httpClient.DefaultRequestHeaders().TryAppendWithoutValidation(L"Authorization", L"<認証用文字列>" );

    //POSTデータ
    HttpStringContent content( L"{ \"data\" : 2 }", UnicodeEncoding::Utf8, L"application/json" );
    
    try {
        httpResponseMessage = httpClient.PostAsync(requestUri, content).get();
        if( !httpResponseMessage.IsSuccessStatusCode() ) {
            return false;
        }
    }catch( winrt::hresult_error const& ex ) {
        return false;
    }

	return true;
}

■参考記事

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?