利用ASP.NET的WebRequest來判斷URL網址是否正常,並且結合IECapt取得URL的畫面
網路上有一個有名的snap shots的網站,主要是提供blog外掛超連結預覽的功能
小弟就去網路上找了一下資料,如何利用ASP.NET達到這個功能..
其實主要透過一支win的程式(IECapt)來取得url的畫面就可以做到了...
當然在抓取url的畫面前,可以先用WebRequest的GetResponse來判斷此url是否正常...c#範例
IECapt.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="IECapt.aspx.cs" Inherits="IECapt" %>
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>IECapt</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:TextBox ID="TextBox1" runat="server" Width="300px">http://blog.blueshop.com.tw/hent/</asp:TextBox>
12 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="取得畫面" />
13 <br />
14 <asp:Image ID="Image1" runat="server" /></div>
15 </form>
16 </body>
17 </html>
18
02
03 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04 <html xmlns="http://www.w3.org/1999/xhtml">
05 <head id="Head1" runat="server">
06 <title>IECapt</title>
07 </head>
08 <body>
09 <form id="form1" runat="server">
10 <div>
11 <asp:TextBox ID="TextBox1" runat="server" Width="300px">http://blog.blueshop.com.tw/hent/</asp:TextBox>
12 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="取得畫面" />
13 <br />
14 <asp:Image ID="Image1" runat="server" /></div>
15 </form>
16 </body>
17 </html>
18
IECapt.aspx.cs
01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 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 }
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 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 }
執行結果:
參考網址:
http://iecapt.sourceforge.net/
http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=47537
http://www.snap.com/snapshots.php