2017-10-10 2 views
0

HttpWebRequestを使用してHttpWebResponseを取得するC#コード(IIS上でホストされているWebアプリケーションです)があります。そこに私は任意のウェブサイトに要求を行う&応答文字列として応答を取得し、私は応答文字列を分析します。しかし、最近、ブラウザでページが読み込まれた後、JavaScriptがデータの取得を行うレスポンスを取得します。HttpWebRequestを使用してC#でhttpレスポンスを取得するときにJavaScriptを処理するには?

Firebug &でこれをデバッグしようとしましたが、応答の最後に、pageloadの後にdom要素を更新するJavaScript関数があります。私は私のC#コードで同じことをすることができる方法はありますか?私は今まで解決策ではないことをネットで調べました。

後、私が使用していたコードは次のとおりです。

HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

     foreach (Cookie cook in response.Cookies) 
     { 
      Cookie cookie = new Cookie(); 
      cookie.Name = cook.Name; 
      cookie.Value = cook.Value; 
      cookie.Domain = cook.Domain; 
      cookie.Expires = DateTime.Now.AddDays(10); 
      cookieList.Add(cookie); 
     } 

     string postData = string.Format("username=" + txtUserID.Text + "&password=" + txtPwd.Text + "&url=https://example.com/&game="); 
     byte[] postBytes = Encoding.UTF8.GetBytes(postData); 
     HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://login.example.com/Login/authenticate"); 
     req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0"; 
     req.KeepAlive = true; 
     req.AutomaticDecompression = DecompressionMethods.GZip; 

     ////set the cookie 
     req.CookieContainer = new CookieContainer(); 
     foreach (Cookie cook in cookieList) 
     { 
      Cookie cookie = new Cookie(); 
      cookie.Name = cook.Name; 
      cookie.Value = cook.Value; 
      cookie.Domain = cook.Domain; 
      cookie.Expires = DateTime.Now.AddDays(10); 
      req.CookieContainer.Add(cookie); 
     } 

     req.Headers.Add("Accept-Encoding", "gzip, deflate"); 
     req.Headers.Add("Accept-Language", "en-GB,en-US;q=0.8,en;q=0.6");//en-GB,en-US;q=0.8,en;q=0.6 
     req.Method = "POST"; 
     req.Host = "login.example.com"; 
     req.Referer = "https://login.example.com/Login/logout"; 
     req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; 

     req.ContentType = "application/x-www-form-urlencoded;"; 
     req.ContentLength = postBytes.Length; 

     //getting the request stream and posting data 
     StreamWriter requestwriter = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII); 
     requestwriter.Write(postData); 
     requestwriter.Close(); 
     HttpWebResponse myHttpWebResponse = (HttpWebResponse)req.GetResponse(); 
     Stream responseStream = myHttpWebResponse.GetResponseStream(); 
     StreamReader myStreamReader = new StreamReader(responseStream, Encoding.ASCII); 
     string responseString = myStreamReader.ReadToEnd(); 
     myStreamReader.Close(); 
     responseStream.Close(); 
     myHttpWebResponse.Close(); 
+1

ブラウザのように機能し、実際にjavascriptを実行する必要があります。 HttpRequestはこれを行うことができません。これを行うことができるライブラリやツールがいくつかあります(例:https://github.com/cefsharp/CefSharp/)。サードパーティ製のライブラリを使用したくない場合は、WebBrowserコントロールを利用することができます(お勧めしません)。 – Evk

+0

IISでホストされているWebアプリケーションでgithub.com/cefsharp/CefSharpを使用できますか? – user1400290

+0

そこには「オフスクリーン」レンダラがあります。これは、ユーザーインターフェイス(winformsやwpfなど)に依存しません。このレンダラーは、Webアプリケーションを含むあらゆる種類のアプリケーションで動作できます。 – Evk

答えて

0

私はようやく私のニーズに簡単な解決策を得ました。以下、私が続くリンクされています

まず次をインポートする必要があります: Link to tutorial

の後は結果を得ることができますコードで今すぐ

using System.Drawing; 
using OpenQA.Selenium; 
using OpenQA.Selenium.PhantomJS; 
using System.Text.RegularExpressions; 
using System.IO; 
using HtmlAgilityPack; 

コード:

 var options = new PhantomJSOptions(); 
     var driver = new PhantomJSDriver(options); 
     driver.Manage().Window.Size = new Size(1360, 728); 
     var size = driver.Manage().Window.Size; 

     driver.Navigate().GoToUrl("https://example.com/"); 
     string url = driver.Url; 
     //the driver can now provide you with what you need (it will execute the script) 
     //get the source of the page 
     var source = driver.PageSource; 
     //fully navigate the dom 
     var pathElement1 = driver.FindElementByName("username"); 
     var pathElement2 = driver.FindElementByName("password"); 
     var pathElement3 = driver.FindElementByXPath("//button[@class='SubmitButton']"); 

     pathElement1.Clear(); 
     pathElement1.SendKeys("username"); 
     pathElement2.Clear(); 
     pathElement2.SendKeys("password"); 
     pathElement3.Click(); 

     //Now get the response after login button click 
     source = driver.PageSource; 
関連する問題