用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

图片抓取

2015-08-11 作者: 莫名其妙举报

[c#]代码库

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EVTBT.Core;
using EVTBT.DAL;
using System.Data;
using System.Threading;
using System.IO;
using System.Drawing;
using System.Net;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable DT = Common.NoPager2("EntCertificateInfo", "", "CertID,QualiName,QualiNO", "", "IsDel='0'").Tables[0];

        string loaction = " F:\\FoodSafe\\";
        foreach (DataRow DR in DT.Rows)
        {
            //DataRow DR = DT.Rows[2];

            try
            {
                string sss = "http://sbcx.saic.gov.cn:9080/tmois/wszhcx_getImageInputSteremSF.xhtml?regNum=" + DR["QualiNO"].ToString() + "&intcls=5&size=5";
                WebRequest request = WebRequest.Create(sss);
                request.Timeout = 30000;
                WebResponse response = request.GetResponse();
                Stream reader = response.GetResponseStream();
                byte[] buff = StreamToBytes(reader);
                if (buff.Length > 0)
                {
                    string path = Server.MapPath("~/ImgCache/" + DR["CertID"].ToString() + "/" + DR["QualiNO"].ToString() + ".jpg");

                    string path2 = Server.MapPath("~/ImgCache/" + DR["CertID"].ToString() + "/");
                    Directory.CreateDirectory(path2);
                    Stream s = new FileStream(path, FileMode.Append);

                    int c = 0; //实际读取的字节数    
                    s.Write(buff, 0, buff.Length);
                    s.Close();
                    s.Dispose();
                    Common.Insert2("adm_adminlog", "UserIP,Des,Remark", "'" + DR["CertID"].ToString() + "','" + DR["QualiNO"].ToString() + "','" + path + "'");
                }
                reader.Close();
                reader.Dispose();
                response.Close();

            }




            catch (Exception ex)
            {

            }



        }
    }


    public static byte[] StreamToBytes(Stream stream)
    {
        List<byte> bytes = new List<byte>();
        int temp = stream.ReadByte();
        while (temp != -1)
        {
            bytes.Add((byte)temp);
            temp = stream.ReadByte();
        }

        return bytes.ToArray();
    }



    public Bitmap Get_img(string URL, string location, string filename)
    {
        Bitmap img = null;
        HttpWebRequest req;
        HttpWebResponse res = null;
        try
        {
            System.Uri httpUrl = new System.Uri(URL);
            req = (HttpWebRequest)(WebRequest.Create(httpUrl));
            req.Timeout = 180000; //设置超时值10秒
            req.UserAgent = "MSIE 6.0";
            req.Accept = "image/*";
            req.Method = "GET";
            res = (HttpWebResponse)(req.GetResponse());
            img = new Bitmap(res.GetResponseStream());//获取图片流                
            img.Save(@"F:/" + location + filename);//随机名
        }

        catch (Exception ex)
        {
            string aa = ex.Message;
            Common.Insert2("adm_adminLog", "Des", "'" + location + "  " + filename + "'");
        }
        finally
        {
            res.Close();
        }
        return img;
    }

}


网友评论    (发表评论)

共2 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...