2012-03-27 6 views
3

私はAPIやWebサービスを持たない安全なWebサイトからデータを取得する必要があるC#プロジェクトに取り組んでいます。私の計画はログインし、必要なページにアクセスし、HTMLを解析してデータベースにログするために必要なデータビットを取得することです。今はコンソールアプリケーションでテストしていますが、最終的にAzureサービスバスアプリケーションに変換されます。WebサイトへのC#Console/Serverのアクセス

何かを得るには、login.cfmページでログインする必要があります。つまり、ページにユーザー名とパスワードの入力コントロールを読み込み、送信ボタンをクリックする必要があります。次に、解析するページに移動します。

私はコントロールの解析に「ブラウザ」がないので、さまざまなC#.NETクラスを使用してページにアクセスし、ユーザー名とパスワードを設定してサブミットをクリックしようとしていますが、 。

私が見ることができる例、または.NETのクラスは、このようなプロジェクトのために設計されたものですか?

ありがとうございます!

答えて

2

は、カスタムWebClientクラスを作成する必要がありますセッションクッキーの持続性のためにSystem.Net

にWebClientクラスを使用します。

#region webclient with cookies 
public class WebClientX : WebClient 
{ 
    public CookieContainer cookies = new CookieContainer(); 
    protected override WebRequest GetWebRequest(Uri location) 
    { 
     WebRequest req = base.GetWebRequest(location); 
     if (req is HttpWebRequest) 
      (req as HttpWebRequest).CookieContainer = cookies; 
     return req; 
    } 
    protected override WebResponse GetWebResponse(WebRequest request) 
    { 
     WebResponse res = base.GetWebResponse(request); 
     if (res is HttpWebResponse) 
      cookies.Add((res as HttpWebResponse).Cookies); 
     return res; 
    } 
} 
#endregion 

アドオンFirebugのか、フォームを送信する際に送信されるHTTPのPOSTデータを取得するためにクロームに組み込まれた開発ツールのようなブラウザを使用してください。これらのPOSTをWebClientXクラスを使用して送信し、応答HTMLを解析します。

の形式がわかっている場合、最も速くHTML を解析するには、単純なRegex.Matchを使用しています。したがって、開発ツールを使用してPOST、URL、およびHTMLコンテンツを記録した後、WebClientXを使用して同じタスクを実行することで、ブラウザのアクションを実行できます。

+0

ありがとう、これは私が探していたパスでした。 GetWebResponseを実行すると、ユーザー名とパスワードボックスに値を入力してログインするにはどうすればよいですか? –

+0

@Chris H - あなたはそれらのオーバーライドされた保護された関数を呼び出さない。必要なのは、WebClientのパブリックインターフェイスを学ぶことです。フォーム送信のようなHTTP POSTを行うには、WebClientのUploadValuesメソッドを使用します。 http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx –

+0

もう一度、以下に完全なコードを掲載しました。 –

0

[OK]をクリックすると、1ページにログインしてからログインした後の2ページ目のコードを読むことができます。

class Program 
     { 
      static void Main(string[] args) 
      { 

       string uriString = "http://www.remotesite.com/login.cfm"; 

       // Create a new WebClient instance. 
       WebClientX myWebClient = new WebClientX(); 

       // Create a new NameValueCollection instance to hold some custom parameters to be posted to the URL. 
       NameValueCollection myNameValueCollection = new NameValueCollection(); 

       // Add necessary parameter/value pairs to the name/value container. 
       myNameValueCollection.Add("userid", "myname"); 
       myNameValueCollection.Add("mypassword", "mypassword"); 

       Console.WriteLine("\nUploading to {0} ...", uriString); 
       // 'The Upload(String,NameValueCollection)' implicitly method sets HTTP POST as the request method.    
       byte[] responseArray = myWebClient.UploadValues(uriString, myNameValueCollection); 

       // Decode and display the response. 
       Console.WriteLine("\nResponse received was :\n{0}", Encoding.ASCII.GetString(responseArray)); 

       Console.WriteLine("\n\n\n pausing..."); 
       Console.ReadKey(); 

       // Go to 2nd page on the site to get additional data 
       Stream myStream = myWebClient.OpenRead("https://www.remotesite.com/status_results.cfm?t=8&prog=d"); 

       Console.WriteLine("\nDisplaying Data :\n"); 
       StreamReader sr = new StreamReader(myStream); 
       StringBuilder sb = new StringBuilder(); 

       using (StreamReader reader = new StreamReader(myStream, System.Text.Encoding.UTF8)) 
       { 
        string line; 
        while ((line = reader.ReadLine()) != null) 
        { 
         sb.Append(line + "\r\n"); 
        } 
       } 

       using (StreamWriter outfile = new StreamWriter(@"Logfile1.txt")) 
       { 
        outfile.Write(sb.ToString()); 
       } 

       Console.WriteLine(sb.ToString()); 

       Console.WriteLine("\n\n\n pausing..."); 
       Console.ReadKey(); 

      } 

     } 

     public class WebClientX : WebClient 
     { 
      public CookieContainer cookies = new CookieContainer(); 
      protected override WebRequest GetWebRequest(Uri location) 
      // public override WebRequest GetWebRequest(Uri location) 
      { 
       WebRequest req = base.GetWebRequest(location); 
       if (req is HttpWebRequest) 
        (req as HttpWebRequest).CookieContainer = cookies; 
       return req; 
      } 
      protected override WebResponse GetWebResponse(WebRequest request) 
      { 
       WebResponse res = base.GetWebResponse(request); 
       if (res is HttpWebResponse) 
        cookies.Add((res as HttpWebResponse).Cookies); 
       return res; 
      } 
     } 
関連する問題