■ HttpClient 클래스의 DefaultRequestHeaders 속성을 사용해 사용자 ID/패스워드를 설정하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System; using System.Net.Http; using System.Net.Http.Headers; using System.Text; string userID = "admin"; string password = "1234"; using(HttpClient client = new HttpClient()) { byte[] credentialByteArray = Encoding.UTF8.GetBytes($"{userID}:{password}"); string credentialBase64 = Convert.ToBase64String(credentialByteArray, Base64FormattingOptions.None); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentialBase64); // 작업을 처리한다. } |