[C#] 图片抓取 →→→→→进入此内容的聊天室

来自 , 2019-12-24, 写在 C#, 查看 134 次.
URL http://www.code666.cn/view/fc03d482
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using EVTBT.Core;
  8. using EVTBT.DAL;
  9. using System.Data;
  10. using System.Threading;
  11. using System.IO;
  12. using System.Drawing;
  13. using System.Net;
  14.  
  15. public partial class Default2 : System.Web.UI.Page
  16. {
  17.     protected void Page_Load(object sender, EventArgs e)
  18.     {
  19.         DataTable DT = Common.NoPager2("EntCertificateInfo", "", "CertID,QualiName,QualiNO", "", "IsDel='0'").Tables[0];
  20.  
  21.         string loaction = " F:\\FoodSafe\\";
  22.         foreach (DataRow DR in DT.Rows)
  23.         {
  24.             //DataRow DR = DT.Rows[2];
  25.  
  26.             try
  27.             {
  28.                 string sss = "http://sbcx.saic.gov.cn:9080/tmois/wszhcx_getImageInputSteremSF.xhtml?regNum=" + DR["QualiNO"].ToString() + "&intcls=5&size=5";
  29.                 WebRequest request = WebRequest.Create(sss);
  30.                 request.Timeout = 30000;
  31.                 WebResponse response = request.GetResponse();
  32.                 Stream reader = response.GetResponseStream();
  33.                 byte[] buff = StreamToBytes(reader);
  34.                 if (buff.Length > 0)
  35.                 {
  36.                     string path = Server.MapPath("~/ImgCache/" + DR["CertID"].ToString() + "/" + DR["QualiNO"].ToString() + ".jpg");
  37.  
  38.                     string path2 = Server.MapPath("~/ImgCache/" + DR["CertID"].ToString() + "/");
  39.                     Directory.CreateDirectory(path2);
  40.                     Stream s = new FileStream(path, FileMode.Append);
  41.  
  42.                     int c = 0; //实际读取的字节数    
  43.                     s.Write(buff, 0, buff.Length);
  44.                     s.Close();
  45.                     s.Dispose();
  46.                     Common.Insert2("adm_adminlog", "UserIP,Des,Remark", "'" + DR["CertID"].ToString() + "','" + DR["QualiNO"].ToString() + "','" + path + "'");
  47.                 }
  48.                 reader.Close();
  49.                 reader.Dispose();
  50.                 response.Close();
  51.  
  52.             }
  53.  
  54.  
  55.  
  56.  
  57.             catch (Exception ex)
  58.             {
  59.  
  60.             }
  61.  
  62.  
  63.  
  64.         }
  65.     }
  66.  
  67.  
  68.     public static byte[] StreamToBytes(Stream stream)
  69.     {
  70.         List<byte> bytes = new List<byte>();
  71.         int temp = stream.ReadByte();
  72.         while (temp != -1)
  73.         {
  74.             bytes.Add((byte)temp);
  75.             temp = stream.ReadByte();
  76.         }
  77.  
  78.         return bytes.ToArray();
  79.     }
  80.  
  81.  
  82.  
  83.     public Bitmap Get_img(string URL, string location, string filename)
  84.     {
  85.         Bitmap img = null;
  86.         HttpWebRequest req;
  87.         HttpWebResponse res = null;
  88.         try
  89.         {
  90.             System.Uri httpUrl = new System.Uri(URL);
  91.             req = (HttpWebRequest)(WebRequest.Create(httpUrl));
  92.             req.Timeout = 180000; //设置超时值10秒
  93.             req.UserAgent = "MSIE 6.0";
  94.             req.Accept = "image/*";
  95.             req.Method = "GET";
  96.             res = (HttpWebResponse)(req.GetResponse());
  97.             img = new Bitmap(res.GetResponseStream());//获取图片流                
  98.             img.Save(@"F:/" + location + filename);//随机名
  99.         }
  100.  
  101.         catch (Exception ex)
  102.         {
  103.             string aa = ex.Message;
  104.             Common.Insert2("adm_adminLog", "Des", "'" + location + "  " + filename + "'");
  105.         }
  106.         finally
  107.         {
  108.             res.Close();
  109.         }
  110.         return img;
  111.     }
  112.  
  113. }

回复 "图片抓取"

这儿你可以回复上面这条便签

captcha