C, C++, C#
C# HttpWebRequest 웹 파싱하기
WindowsHyun
2017. 6. 28. 15:01
반응형
"프로젝트 - 참조추가" 를 클릭하셔서 아래 사진과 같이 체크를 해주시길 바랍니다.
//---------------------------------------- // WebRequest 사용하기 위하여 using System.Net; using System.IO; using System.Web; //---------------------------------------- static HttpWebRequest request; static HttpWebResponse response; static StreamReader readerPost; static Stream dataStrem; static string resResult = string.Empty;
참조를 추가 하신 뒤 해당 HttpWebRequest를 사용하기 위하여 선언을 해줍니다.
선언은 메인 부분이나 상단에 작성해주시면 됩니다.
아래 코드처럼 함수 안에서 바로 불러와 사용할수 있게 하기 위하여 미리 선언을 하였습니다.
string httpWebGET( string url, string Referer ) { request = (HttpWebRequest)WebRequest.Create(url); request.Method = "GET"; request.Referer = Referer; request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)"; request.ContentType = "application/x-www-form-urlencoded"; request.Host = splitParsing(url, "http://", "/"); request.KeepAlive = true; request.AllowAutoRedirect = false; response = (HttpWebResponse)request.GetResponse(); readerPost = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8, true); // Encoding.GetEncoding("EUC-KR") resResult = readerPost.ReadToEnd(); //Getcookie = response.GetResponseHeader("Set-Cookie"); // 쿠키정보 값을 확인하기 위해서 readerPost.Close(); response.Close(); return resResult; }
예제 :
RTB1.Text = httpWebGET("http://windowshyun.tistory.com", "http://windowshyun.tistory.com");
RTB1.Text에 http://windowshyun.tistory.com의 웹페이지 소스코드를 전부 가져옵니다.
httpWebGet에서 코드를 보시면 splitParsing라는 함수가 있느데 해당 함수는 이전글에서 특정 글자 파싱하기에서 사용하는 함수를 사용한것 입니다.
해당 함수를 같이 사용하시면 정상적으로 파싱이 됩니다.
반응형