利用ASP.NET的WebRequest來判斷URL網址是否正常,並且結合IECapt取得URL的畫面

網路上有一個有名的snap shots的網站,主要是提供blog外掛超連結預覽的功能
小弟就去網路上找了一下資料,如何利用ASP.NET達到這個功能..
其實主要透過一支win的程式(IECapt)來取得url的畫面就可以做到了...
當然在抓取url的畫面前,可以先用WebRequest的GetResponse來判斷此url是否正常...c#範例

網路上有一個有名的snap shots的網站,主要是提供blog外掛超連結預覽的功能

小弟就去網路上找了一下資料,如何利用ASP.NET達到這個功能..

其實主要透過一支win的程式(IECapt)來取得url的畫面就可以做到了...

當然在抓取url的畫面前,可以先用WebRequest的GetResponse來判斷此url是否正常...c#範例

IECapt.aspx

   1:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile="IECapt.aspx.cs" Inherits="IECapt" %>
   2:   
   3:  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
   4:   
   5:  <html xmlns="http://www.w3.org/1999/xhtml" >
   6:  <head runat="server">
   7:      <title>IECapt</title>
   8:  </head>
   9:  <body>
  10:      <form id="form1" runat="server">
  11:      <div>
  12:          <asp:TextBox ID="TextBox1" runat="server" Width="300px">http://blog.blueshop.com.tw/hent/</asp:TextBox>
  13:          <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="取得畫面" />
  14:          <br />
  15:          <asp:Image ID="Image1" runat="server" /></div>
  16:      </form>
  17:  </body>
  18:  </html>

IECapt.aspx.cs

   1:  using System;
   2:  using System.Data;
   3:  using System.Configuration;
   4:  using System.Collections;
   5:  using System.Web;
   6:  using System.Web.Security;
   7:  using System.Web.UI;
   8:  using System.Web.UI.WebControls;
   9:  using System.Web.UI.WebControls.WebParts;
  10:  using System.Web.UI.HtmlControls;
  11:  using System.Diagnostics;
  12:  using System.Net;
  13:   
  14:  public partial class IECapt : System.Web.UI.Page
  15:  {
  16:      //IECapt下載網址,下載後請放在跟此檔案相同位置
  17:      //http://iecapt.sourceforge.net/    
  18:      protected void Page_Load(object sender, EventArgs e)
  19:      {
  20:   
  21:      }
  22:      protected void Button1_Click(object sender, EventArgs e)
  23:      {        
  24:          string URL = this.TextBox1.Text;
  25:          bool Ok  = true;//預設URL的狀態是正常的
  26:          WebRequest request = WebRequest.Create(URL);
  27:          request.Proxy = null;
  28:          try
  29:          {            
  30:              HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  31:              Response.Write("\n Site Exists.");
  32:          }
  33:          catch (Exception exc)
  34:          {
  35:              Response.Write("\n Site does not exists.");
  36:              Ok = false;
  37:          }
  38:   
  39:          //URL是正常運作,才抓取URL畫面
  40:          if (Ok != false)
  41:          {
  42:              Process proc = new Process();
  43:   
  44:              //IECapt.exe path
  45:              proc.StartInfo.FileName = Server.MapPath("~/IECapt.exe");
  46:   
  47:              //將取得的畫面暫存為image.png檔
  48:              proc.StartInfo.Arguments = this.TextBox1.Text + " " + Server.MapPath("~/image.png");
  49:              proc.Start();
  50:   
  51:              //等待IECapt程式執行完畢
  52:              proc.WaitForExit();
  53:   
  54:              this.Image1.ImageUrl = "image.png";
  55:          }
  56:   
  57:      }
  58:  }

執行結果:

IECapt

原始程式碼:IECapt.rar

參考網址:
http://iecapt.sourceforge.net/
http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=47537
http://www.snap.com/snapshots.php