WebBrowser 객체를 이용해서 web페이지를 볼 때 DocumentText 메소드로 렌더링된 html을 읽어올 때 default가 utf-8 encoding type이라 이상태에서는 euc-kr의 한글은 깨진다
이 경우 스트림리더를 사용해서 실제 페이지의 encoding type으로 읽어들이면 된다.
샘플) WebBrowser.DocumentText 로 소스를 봤을경우.
<html>
<head>
<title><��̴Ͻ�Ʈ ���� �ϴ�></title>
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr" />
StreamReader를 이용해 페이지의 encoding type으로 읽어들인경우
<html>
<head>
<title><오늘은 어린이날 입니다></title>
<<C# Code>>
/// <summary>
/// WebBrowser's DocumentCompleted Event
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void webBrowserViewer_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
// ((WebBrowser)sender).Document.Window.Error += new HtmlElementErrorEventHandler(Window_Error);
WebBrowser oBrowser = sender as WebBrowser;
txtSource.Text = StreamConvertEUCKRtoUTF8(oBrowser);
}
/// <summary>
/// Using WebBrowser web page
/// 웹페이지 Encoding type 이 EUC-KR 이면 WebBrowser.DocumentText로 html로 렌더링된 text를 볼때 한글이 깨진다.
/// 아래처럼 스트림리더로 EUC-KR type으로 읽어온다.
/// </summary>
/// <param name="oBrowser"></param>
/// <returns></returns>
private String StreamConvertEUCKRtoUTF8(WebBrowser oBrowser)
{
String sResult = String.Empty;
Stream oStream;
StreamReader oStreamReader;
oStream = oBrowser.DocumentStream;
oStreamReader = new StreamReader(oStream, System.Text.Encoding.GetEncoding(oBrowser.Document.Encoding));
oStream.Position = 0;
sResult = oStreamReader.ReadToEnd();
return sResult;
}
'프로그래밍 > c#' 카테고리의 다른 글
c#에서 mariadb사용하기 (0) | 2018.11.10 |
---|---|
WebBrowser이용한 스크린스크래핑 (0) | 2018.05.09 |
using MySqlConnector (0) | 2018.05.07 |