URL에 접속해서 웹문서(html) 긁어오는..
using System; using System.IO; using System.Net; using System.Text; namespace Example.System.Net { public class WebRequestGetExample { public static void Main() { try { Console.WriteLine("http:// 뺀 주소를 입력 후 엔터키를 입력."); Console.Write("http://"); string url = Console.ReadLine(); //WebRequest request = // WebRequest.Create("http://contoso.com/default.html"); //WebRequest request = // WebRequest.Create("http://google.co.kr"); WebRequest request = WebRequest.Create("http://" + url); request.Credentials = CredentialCache.DefaultCredentials; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); /*if (response.StatusDescription != "OK") { Console.WriteLine("주소값이 유효하지 않거나, 열리지 않는 사이트입니다."); return; }*/ Console.WriteLine(response.StatusDescription); Console.WriteLine("Charset = " + response.CharacterSet); //Console.WriteLine("content type = " // + response.ContentType); //Console.WriteLine("content encoding = " // + response.ContentEncoding); //Console.WriteLine("content length = " // + response.ContentLength); Console.WriteLine("headers = " + response.Headers); return; Encoding encode; //if (response.CharacterSet == "utf-8") { // encode = Encoding.UTF8; //} if (response.CharacterSet.ToLower() == "utf-8") { encode = Encoding.UTF8; } else { encode = Encoding.Default; } Stream dataStream = response.GetResponseStream(); //StreamReader reader = // new StreamReader(dataStream); //StreamReader reader = // new StreamReader(dataStream, Encoding.Default); StreamReader reader = new StreamReader(dataStream, encode); string responseFromServer = reader.ReadToEnd(); Console.WriteLine(responseFromServer); reader.Close(); dataStream.Close(); response.Close(); } catch (WebException e) { Console.WriteLine("주소값이 유효하지 않거나 열리지 않는 사이트입니다."); Console.WriteLine(e.Message); } } } }
[출처] http://blog.daum.net/hervgardenbyzoo/8744494