2014-04-02 4 views
0

나는 이름, 직원 ID, 급여, 출석 날짜가있는 웹 사이트가 있습니다. 기본적으로 웹 사이트를 열고 각 값을 수동으로 입력했습니다.웹 사이트를 자동으로 열고 작업을 수행하는 방법

해당 웹 사이트를 자동으로 열고 C#을 사용하여 존경받는 항목에 값을 할당하고 싶습니다. 그물. 나는 그것이 가능할 수 있는지 여부를 모른다. 누구든지 제게 방법이나 대안을 제시 할 수 있습니까?

+0

"웹 스크래핑"또는 "스크린 스크래핑"을 찾으십시오. –

+0

존, 미안해. – user3040784

+0

HttpClient를 사용하여 HTTP 요청을 보내고 응답을받을 수 있습니다. 웹 브라우저의 디버거를 사용하여 어떤 요청과 응답이 전송되는지 파악하고 클라이언트와 모방하는 것이 좋습니다. –

답변

0

WebClientSystem.Net 네임 스페이스를 사용하여 프로그래밍 방식으로 URL을 실행할 수 있습니다. url은 url을로드하는 동안 추출 할 수있는 쿼리 문자열을 포함 할 수 있습니다. 그러면 응답은 string으로 디코딩 될 수 있으며 기본적으로 html로되어 있으므로 사용자의 편의에 따라 사용할 수 있습니다.

다음은 기능을 수행해야하는 방법입니다.

public static string ReadUrlToString(string url, string method, string postParams, string userName, string password, string domain) 
{ 
    var wc = new WebClient(); 

    // set the user agent to IE6 
    wc.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705;)"); 

    AssignCredentials(wc, userName, password, domain); 

    try 
    { 
     if (Is.EqualString(method, "POST")) 
     { 
      // Eg "field1=value1&field2=value2" 
      var bret = wc.UploadData(url, "POST", Encoding.UTF8.GetBytes(postParams)); 

      return Encoding.UTF8.GetString(bret); 
     } 

     // actually execute the GET request 
     return wc.DownloadString(url); 
    } 
    catch (WebException we) 
    { 
     // WebException.Status holds useful information 
     //throw as needed 
    } 
    catch (Exception ex) 
    { 
     // other errors 
     //throw as needed 
    } 
} 

방법은하기와 같다.

private static void AssignCredentials(WebClient wc, string userName, string password, string domain) 
{ 
    if (Is.NotEmptyString(userName)) 
    { 
     wc.Credentials = Is.EmptyString(domain) 
           ? new NetworkCredential(userName, password) 
           : new NetworkCredential(userName, password, domain); 
    } 
} 

희망이 있습니다.

관련 문제