WebBrowser[feat)WebView2]

S:)·2024년 5월 16일

C#

목록 보기
1/3

WebBrowser

  • WebBrowser 는 Internet Explorer에 있는 컨트롤을 구현하는 데 사용된다.

22년 6월 Internet Explorer가 지원 종료 되었지만 , 여전히 사용 가능하다.

  • WebBrowser 은 IE 기반으로 , Edge 는 WebView2 사용 권장하고있다.
    (WebView2는 WebBrowser 에 있는 모든 기능을 제공한다.)

기능

1. Navigate

: 호출 하고자 하는 웹 주소를 그대로 navigate로 호출

  • GET방식
string url = www.google.com 
webbrowser.navigate(url)
  • POST방식
string PostData = string.Format("param={0}&param={1}", value, value); 
byte[] param = Encoding.UTF8.GetBytes(PostData); //바이트 형식으로 받은 다음 넣어주면 된다.
webbrowser.navigate(url, "POST", param ,  "Content-Type: application/x-www-form-urlencoded")

2. WebBrowser.DocumentCompleted

: WebBrowser 컨트롤이 문서 로드를 완료할 때 발생합니다.

 private void PrintDocument(object sender,  WebBrowserDocumentCompletedEventArgs e)
{
   //문서가 완전히 로드 되었는지 확인하는 것이 좋다. 
   if (this.wbReport.ReadyState == WebBrowserReadyState.Complete){  
   
   event();
    }
} 

3.InvokeScript

: 현재 로드된 문서에 의해 구현되 스크립트 함수를 실행 (호출한 문서에 정의함 함수를 호출)

test.jsp

function showMsg(){
	return "This is test";
}

main.cs

webbrowser.navigate(test.jsp);
WebBrowserDocumentCompletedEventArgs e)
{   
   if (this.wbReport.ReadyState == WebBrowserReadyState.Complete){     
  var str = webbrowser.Documnet.InvokeScript("showMsg");
  messagebox(str);
    }
} 

결과 > "This is test"

profile
일단 저장

0개의 댓글