- namespace DBUtility
- {
- public class common
- {
- public static string GetPageHTML(string url)
- {
- HttpWebRequest wr = WebRequest.Create(url) as HttpWebRequest;
- try
- {
- wr.Method = "get";
- wr.Accept = "*/*";
- wr.Headers.Add("Accept-Language: zh-cn");
- wr.Headers.Add("UA-CPU: x86");
- wr.Headers.Add("Accept-Encoding: gzip, deflate");
- wr.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Embedded Web Browser from: http://bsalsa.com/; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
- wr.KeepAlive = true;
- wr.ServicePoint.Expect100Continue = false;
- wr.AllowAutoRedirect = false;
- wr.Timeout = 30000;
- HttpWebResponse wre = wr.GetResponse() as HttpWebResponse;
- StreamReader sreader = new StreamReader(wre.GetResponseStream(), Encoding.Default);
- string sHtml = sreader.ReadToEnd();
- wre.Close();
- return sHtml;
- }
- catch
- {
- return "";
- }
- }
- public static int SaveImageFromWeb(string imgUrl, string path,string imgname)
- {
- path = "e://test";
- string imgName = imgname+imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf(".") );
- path = path + "//" + imgName;
- string defaultType = ".jpg";
- string[] imgTypes = new string[] { ".jpg", ".jpeg", ".png", ".gif", ".bmp" };
- string imgType = imgUrl.ToString().Substring(imgUrl.ToString().LastIndexOf("."));
- foreach (string it in imgTypes)
- {
- if (imgType.ToLower().Equals(it))
- break;
- if (it.Equals(".bmp"))
- imgType = defaultType;
- }
- //try
- //{
- HttpWebRequest request = (HttpWebRequest)WebRequest.Create(imgUrl);
- request.UserAgent = "Mozilla/6.0 (MSIE 6.0; Windows NT 5.1; Natas.Robot)";
- request.Timeout = 10000;
- WebResponse response = request.GetResponse();
- Stream stream = response.GetResponseStream();
- if (response.ContentType.ToLower().StartsWith("image/"))
- {
- byte[] arrayByte = new byte[1024];
- int imgLong = (int)response.ContentLength;
- int l = 0;
- // CreateDirectory(path);
- FileStream fso = new FileStream(path, FileMode.Create);
- while (l < imgLong)
- {
- int i = stream.Read(arrayByte, 0, 1024);
- fso.Write(arrayByte, 0, i);
- l += i;
- }
- fso.Close();
- stream.Close();
- response.Close();
- return 1;
- }
- else
- {
- return 0;
- }
- //}
- //catch
- //{
- // return 0;
- //}
- }
- }
- }
数据采集之-页面读取,图片采集 类
最新推荐文章于 2024-06-25 15:12:18 发布