2013-11-20 2 views
1

AJAX 업데이트 패널이있는 사이트를 웹 스크렙으로보고 있습니다. 제대로 구성된 HTTP 요청 (HttpWebRequest)을 사용하여 웹 사이트에 로그인 할 수 있었고 UpdatePanel의 내용을 가져 오기 위해 POST 요청을 보낼 수 있지만 실제 데이터가 아니라 자리 표시 자 텍스트가 있습니다.C#으로 AJAX 업데이트 패널을 긁는 방법?

다음
6259|updatePanel|ctl00_uxContentPlaceHolder_uxUpdatePnl| 
<table cellpadding="0" cellspacing="0" border="0" width="100%" id="transtable"> 
    <tr> 
     <td> 
      <p> 
       <div id="ctl00_uxContentPlaceHolder_UpdateProgress2" style="display:none;"> 

        <div> 
         <img src="../Include/Images/loading.gif" alt="progressImg" /> 
         <span id="ProgressMsg" style="font-size: small">Please, wait ... </span> 
        </div> 

       </div> 
      </p> 
     </td> 
    </tr> 
    <tr> 
     <td></td> 
    </tr> 
    <tr> 
     <td></td> 
    </tr> 
</table> 

가 예상 결과입니다 : 내가 얻을 응답의 요약 버전 여기

// Already sent POST request with username and password to get session id, cookie etc 
// Create POST data and convert it to a byte array. This includes viewstate, eventvalidation etc. 
postData = String.Format("ctl00%24ScriptManager1=ctl00%24uxContentPlaceHolder%24Panel%7Cctl00%24uxContentPlaceHolder%24uxTimer&__EVENTTARGET=ctl00%24uxContentPlaceHolder%24uxTimer"); 
postData = hiddenFields.Aggregate(postData, (current, field) => current + ("&" + Uri.EscapeDataString(field.Key) + "=" + Uri.EscapeDataString(field.Value))); 

byteArray = Encoding.UTF8.GetBytes(postData); 

// Set the ContentType property of the WebRequest. 
request.Headers.Add("X-MicrosoftAjax", "Delta=true"); 
request.ContentType = "application/x-www-form-urlencoded"; 
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36"; 
request.Referer = "https://www.example.com/Registered/MyAcount.aspx?menu=My%20account"; 
request.Host = "www.example.com"; 
// Set the ContentLength property of the WebRequest. 
request.ContentLength = byteArray.Length; 
// Get the request stream. 
dataStream = request.GetRequestStream(); 
// Write the data to the request stream. 
dataStream.Write(byteArray, 0, byteArray.Length); 
// Close the Stream object. 
dataStream.Close(); 
// Get the response. 

response = (HttpWebResponse)request.GetResponse(); 
_container.Add(response.Cookies); 

using (var reader = new StreamReader(response.GetResponseStream())) 
{ 
    // Read the content. 
    responseFromServer = reader.ReadToEnd(); 
} 

response.Close(); 

됩니다 : 여기

내가 요청이 된 UpdatePanel 데이터를 얻을 수 있도록 코드입니다 :

2577|updatePanel|ctl00_uxContentPlaceHolder_uxUpdatePnl| 
<table cellspacing="0" border="0" id="ctl00_uxContentPlaceHolder_uxMyCards" style="width:100%;border-collapse:collapse;"> 
    <tr> 
     <th align="left" scope="col" style="font-size:12px;font-weight:bold;height:40px;">Card number</th> 
     <th align="left" scope="col" style="font-size:12px;font-weight:bold;">Account holder</th> 
     <th align="left" scope="col" style="font-size:12px;font-weight:bold;">Balance money</th> 
     <th align="left" scope="col" style="font-size:12px;font-weight:bold;">Type</th> 
    </tr> 
    <tr> 
     <td valign="top" style="font-size:12px;width:110px;"> 
      <a id="ctl00_uxContentPlaceHolder_uxMyCards_ctl02_uxManageAccount" href="ManageMyCard.aspx?menu=Manage my card&amp;cno=GgxQxwWICtY4hnlrIZfFzdqc8KMXxVp9" style="font-size:11px;">308425020219083</a> 
     </td> 
     <td valign="top" style="font-size:12px;width:130px;"> 
      My Name 
     </td> 
     <td align="left" valign="top" style="font-size:12px;width:100px;"> 
      $1.50 
     </td> 
     <td valign="top" style="font-size:12px;width:110px;"></td> 
    </tr> 
    <tr> 
     <td valign="top" style="font-size:12px;width:110px;"> 
      <a id="ctl00_uxContentPlaceHolder_uxMyCards_ctl03_uxManageAccount" href="ManageMyCard.aspx?menu=Manage my card&amp;cno=hkbnmVzj%2ftrs%2fVLXK0rBQhB0enOO%7b4Uf" style="font-size:11px;">308425026724813</a> 
     </td> 
     <td valign="top" style="font-size:12px;width:130px;"> 
      My Name 
     </td> 
     <td align="left" valign="top" style="font-size:12px;width:100px;"> 
      $4.04 
     </td> 
     <td valign="top" style="font-size:12px;width:110px;"></td> 
    </tr> 
</table> 

페이지가 요청되고 응답이 데이터보다 먼저 전송됩니다. 실제로로드됩니다. 응답을 보내기 전에 모든 데이터가로드 될 때까지 HttpWebRequest를 대기 상태로 만들 수있는 방법이 있습니까?

실제로 도움이 될만한 것이 있으면 실제 HTTP 요청을 게시 할 수 있지만 브라우저에서 만든 것과 거의 동일하게 보입니다. 사람들이 뛰어 들어서 물어보기 전에 내가하고있는 일에 대한 API가 없으며 어떤 방식 으로든 불법이 아닙니다.

편집 :이를 위해 HttpWebRequest를 사용하는 것이 좋습니다. 셀렌

답변

0

나는이 작업을했는데 HTTP 요청에서 __EVENTTARGET을 두 번 보내고있었습니다. UpdatePanel은 모든 데이터를 올바르게로드합니다.

0

당신이 얻을 수있는 모든 것은 서버의 HTML이기 때문에 페이지에 HTTP 요청을하면이 작업을 수행 할 수 없습니다. 페이지의 JavaScript는 평가되지 않으므로 UpdatePanel의 내용을 가져 오지 않습니다. 하나의 옵션은 핸들러에 요청하여 UpdatePanel의 내용을 반환합니다. 두 번째 옵션은 PhantomJS과 같은 헤드리스 테스트 도구를 사용하여 실제로 페이지를 렌더링하고 페이지에서 JavaScript를 실행할 수 있다는 것입니다. UpdatePanel이 업데이트되고 업데이트 된 컨텐츠를 얻을 수 있습니다.

+0

하지만 자바 스크립트는 데이터를 가져 오기 위해 HTTP 요청을 트리거해야합니다. 그것이 내가 위에 모방하고있는 HTTP 요청입니다. Fiddler에는 브라우저에서 HTTP 요청을 보내고 UpdatePanel에서 콘텐츠를 검색합니다. 코드에서 정확히 동일한 HTTP 요청을 만들면 아무것도 얻지 못합니다 ... – ljenkins

관련 문제