티스토리 뷰

C#

C#: WinHttp를 이용한 Cookie값의 획득

개태형님 2016. 10. 6. 09:55

WinHttp를 사용하기 위해서는 winhttp.dll을 참조 후 프로젝트에서 using 해줘야 한다.

 

1. C:\Windows\System32\winhttp.dll 을 참조 추가

2. 프로젝트내에서 using WinHttp; 입력

        private List<string> GetCookie()
        {
            var winHttp = new WinHttpRequest();
            winHttp.Open("POST", "임의의 POST URL");
            winHttp.SetRequestHeader("Referer", "임의의 Referer URL");
            winHttp.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            winHttp.Send("임의의 전달 값");
            winHttp.WaitForResponse();

            // Header의 값들을 받는다.
            string[] headers = winHttp.GetAllResponseHeaders().Split(new String[] { "\r\n" }, StringSplitOptions.None);

            // Header에서 쿠키 분류 및 취득
            List<string> cookies = headers.Where(o => o.StartsWith("Set-Cookie: "))
                                          .Select(o => o.Replace("Set-Cookie: ", "")).ToList();
            return cookies;
        }
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday