프로그램 코드로 웹패이지가 열리지 않는 경우 – user agent header

웹 브라우저로는 잘 열리는 url이 아래와 같은 코드로는 열리지 않는 경우가 있다.

HttpClient httpClient = new();

HttpResponseMessage httpResponseMessage = await httpClient.GetAsync("https://opendart.fss.or.kr/api/corpCode.xml?crtfc_key=" + ApiKey);

httpResponseMessage.Dispose();

httpClient.Dispose();

서버가 http의 user agent header를 요구하기 때문이다.

HTTP headers let the client and the server pass additional information with a message in a request or response.
HTTP headers, mdn web docs

agent는 어려운 단어다. 흔히 대리인이라 번역하지만 computer와 관련되어서는 그렇지 않다.

(computing) In the client-server model, the part of the system that performs information preparation and exchange on behalf of a client or server.
wiktionary

클라이언트나 서버에서 정보를 관리하는 것이라는 설명이다. user agent header에서 agent는 애플리캐이션을 말하며 user agent는 웹 브라우저를 뜻한다.

The HTTP User-Agent request header is a characteristic string that lets servers and network peers identify the application, operating system, vendor, and/or version of the requesting user agent.
User-Agent, mdn web docs

서버가 유저 애이전트에 대한 정보를 요구하는 건 웹 브라우저로 접속을 시도하는지 나쁜 의도를 가진 보트로 하는 건지를 알려는 거다. 위 서버의 경우에는 특별한 의미 없다. 악성 애플리캐이션이라 해도 아래의 한 줄 코드면 해결이 되기 때문이다.

httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0");

모질라는 웹 브라우저인 파이어폭스를 만드는 회사로 웹 표준에 관여하고 있다.

Apple, Mozilla, and Opera allowed the W3C to publish the specification under the W3C copyright, while keeping a version with the less restrictive license on the WHATWG site. … In 2019, the WHATWG and W3C signed an agreement to collaborate on a single version of HTML going forward: this document.
History, HTML Living Standard

모질라의 파이어폭스가 웹 브라우저의 표준이라는 뜻은 아니고 그저 모질라라는 단체가 오래 전부터 웹에 관련된 일을 많이 해 와서 그냥 먹어주는 거다. 5.0이라는 것도 그냥 붙여 놓은 거다. 버전을 나타내는 게 아니다.