Extract website images and Images
Protected void sub_Click (object sender, EventArgs e)
{
Int I = 0;
String url = this. url. Text;
Directory. CreateDirectory ("D:/aspnetjia/" + url );
String result = GetWebRequest (url );
// This. content. InnerText = result;
MatchCollection results = Regex. Matches (result, @ "] * src = ([" "'] *) (? <Src> [^ '"] *) \ 1 [^>] *>", RegexOptions. IgnoreCase );
String [] d = new string [results. Count];
Foreach (Match m in results)
{
D [I] = m. Groups ["src"]. Value;
I ++;
}
For (int j = 0; j <d. Length; j ++)
{
String jieguo = "";
Int jj = j + 1;
String jie = d [j]. ToString ();
// If (jie. Contains (url ))
//{
// Jie. Replace (url ,"");
//}
If (jie. Contains ("www.") | jie. Contains ("http "))
{
Jieguo = "<a href = '" + jie + "'> </a> ";
}
Else
{
Jieguo = "<a href = '" + url + "/" + jie + "'> </a> ";
}
Random rand = new Random (); // defines a Random number, to prevent you from uploading images with duplicate names
String [] strSpil = jie. Split ('.'); // separate the address with A. Number (img/1.jpg)
String strEnd = strSpil [strSpil. length-1]. toLower (); // obtain the following ("jpg", "gif", "bmp", "png", "jpeg", "JPG", "GIF ", "BMP", "PNG", "JPEG ")
String strName = DateTime. Now. ToString ("yyyymmmddhhss") + rand. Next (100,999 9). ToString (); // get a different name.
String strPointEnd = strName + "." + strEnd;
System. Net. WebClient myWebClient = new System. Net. WebClient ();
If (jie. Contains ("http "))
{
MyWebClient. DownloadFile (jie, "D:/aspnetjia/" + url + "/" + strPointEnd );
}
Else
{
MyWebClient. DownloadFile (url + "/" + jie, "D:/aspnetjia/" + url + "/" + strPointEnd );
}
// Response. Write ("downloading" + j + "...");
}
Response. Write ("Please check the aspnetjia folder on your d disk ");
}
Private string GetWebRequest (string url)
{
Uri uri = new Uri (url );
WebRequest myReq = WebRequest. Create (uri );
WebResponse result = myReq. GetResponse ();
Stream receviceStream = result. GetResponseStream ();
StreamReader readerOfStream = new StreamReader (receviceStream, System. Text. Encoding. GetEncoding ("UTF-8 "));
String strHTML = readerOfStream. ReadToEnd ();
ReaderOfStream. Close ();
ReceviceStream. Close ();
Result. Close ();
Return strHTML;
}
Reprinted from: http://www.aspnetjia.com