ASP.NET(C#)经典采集代码

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using MSXML2;
using System.Text.RegularExpressions;
namespace wenweifeng
{
///


/// CaiJi 的摘要说明
///
public class CaiJi
{
public CaiJi()
{
//
// TODO: 在此处添加构造函数逻辑
//
}

~CaiJi()
{
Dispose();
}

public void Dispose()
{
GC.SuppressFinalize(
this);
}


日期随机函数#region 日期随机函数
/**********************************
* 函数名称:DateRndName
* 功能说明:日期随机函数
* 参 数:ra:随机数
* 调用示例:
* GetRemoteObj o = new GetRemoteObj();
* Random ra = new Random();
* string s = o.DateRndName(ra);
* Response.Write(s);
* o.Dispose();
* *******************************
*/

///
/// 日期随机函数
///
/// 随机数
///

public string DateRndName(Random ra)
{
DateTime d
= DateTime.Now;
string s = null, y, m, dd, h, mm, ss;
y
= d.Year.ToString();
m
= d.Month.ToString();
if (m.Length < 2) m = "0" + m;
dd
= d.Day.ToString();
if (dd.Length < 2) dd = "0" + dd;
h
= d.Hour.ToString();
if (h.Length < 2) h = "0" + h;
mm
= d.Minute.ToString();
if (mm.Length < 2) mm = "0" + mm;
ss
= d.Second.ToString();
if (ss.Length < 2) ss = "0" + ss;
s
+= y + ',' + m + ',' + dd + ',' + h + "-" + mm + "-" + ss;
s
+= ra.Next(1000000, 9999999).ToString();
return s;
}

#endregion


取得文件后缀#region 取得文件后缀
/**********************************
* 函数名称:GetFileExtends
* 功能说明:取得文件后缀
* 参 数:filename:文件名称
* 调用示例:
* GetRemoteObj o = new GetRemoteObj();
* string url = @"
http://www.baidu.com/img/logo.gif";
* string s = o.GetFileExtends(url);
* Response.Write(s);
* o.Dispose();
* *******************************
*/

///
/// 取得文件后缀
///
/// 文件名称
///

public string GetFileExtends(string filename)
{
string ext = null;
if (filename.IndexOf('.') > 0)
{
string[] fs = filename.Split('.');
ext
= fs[fs.Length - 1];
}

return ext;
}

#endregion


获取远程文件源代码#region 获取远程文件源代码
/**********************************
* 函数名称:GetRemoteHtmlCode
* 功能说明:获取远程文件源代码
* 参 数:Url:远程url
* 调用示例:
* GetRemoteObj o = new GetRemoteObj();
* string url = @"
http://www.baidu.com";
* string s = o.GetRemoteHtmlCode(url);
* Response.Write(s);
* o.Dispose();
* *******************************
*/

///
/// 获取远程文件源代码
///
/// 远程url
///

public string GetRemoteHtmlCode(string Url)
{
string s = "";
MSXML2.XMLHTTP _xmlhttp
= new MSXML2.XMLHTTPClass();
_xmlhttp.open(
"GET", Url, false, null, null);
_xmlhttp.send(
"");
if (_xmlhttp.readyState == 4)
{
s
= System.Text.Encoding.Default.GetString((byte[])_xmlhttp.responseBody);
}

return s;
}


#endregion


保存远程文件#region 保存远程文件
/**********************************
* 函数名称:RemoteSave
* 功能说明:保存远程文件
* 参 数:Url:远程url;Path:保存到的路径
* 调用示例:
* GetRemoteObj o = new GetRemoteObj();
* string s = "";
* string url = @"
http://www.baidu.com/img/logo.gif";
* string path =Server.MapPath("Html/");
* s = o.RemoteSave(url,path);
* Response.Write(s);
* o.Dispose();
* *****************************
*/

///
/// 保存远程文件
///
/// 远程url
/// 保存到的路径
///

public string RemoteSave(string Url, string Path)
{

Random ra
= new Random();
string newfilename = string.Empty;
string StringFileName = DateRndName(ra) + "." + GetFileExtends(Url);
if (GetFileExtends(Url).Trim().ToLower() != "jpg")
newfilename
= StringFileName;
else
newfilename
= DateRndName(ra) + "Addpic." + GetFileExtends(Url);
string StringFilePath = Path + StringFileName;
string newfilepath = Path + newfilename;
string retname = string.Empty;
try
{
MSXML2.XMLHTTP _xmlhttp
= new MSXML2.XMLHTTPClass();
_xmlhttp.open(
"GET", Url, false, null, null);
_xmlhttp.send(
"");
if (_xmlhttp.readyState == 4)
{
if (System.IO.File.Exists(StringFilePath))
System.IO.File.Delete(StringFilePath);
System.IO.FileStream fs
= new System.IO.FileStream(StringFilePath, System.IO.FileMode.CreateNew);
System.IO.BinaryWriter w
= new System.IO.BinaryWriter(fs);
w.Write((
byte[])_xmlhttp.responseBody);
w.Close();
fs.Close();
if (GetFileExtends(Url).Trim().ToLower() == "jpg")
{
function myfunction
= new function();
//myfunction.AddShuiYinWord(StringFilePath, newfilepath);
service myserver=new service();
myfunction.AddShuiYinPic(StringFilePath, newfilepath, HttpContext.Current.Server.MapPath(myserver.myweblogo));
}


}

else
throw new Exception(_xmlhttp.statusText);
}




 

你可能感兴趣的:(String,C#,null,url,Random,asp.net)