http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 4152|回复: 6

[求助] 用HttpHelper怎么下载图片啊,默认的方式会报错

[复制链接]
发表于 2018-1-5 09:39:06 | 显示全部楼层 |阅读模式
用HttpHelper怎么下载图片啊,默认的方式会报错

图片下载地址:

byte[] html = result.ResultByte;   图片地址下载下来用的ResultByte

TIM图片20180105093353.jpg
TIM图片20180105093312.png


1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
 楼主| 发表于 2018-1-5 09:40:44 | 显示全部楼层
[C#] 纯文本查看 复制代码
 // <summary>  
        /// byte[]转换成Image  
        /// </summary>  
        /// <param name="byteArrayIn">二进制图片流</param>  
        /// <returns>Image</returns>  
        public static System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
        {
            if (byteArrayIn == null)
                return null;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))
            {
                System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
                ms.Flush();
                return returnImage;
            }
        }


[C#] 纯文本查看 复制代码
/// <summary>
        /// Convert Byte[] to a picture and Store it in file
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="buffer"></param>
        /// <returns></returns>
        public static string CreateImageFromBytes(string fileName, byte[] buffer)
        {
            string file = fileName;
            Image image = byteArrayToImage(buffer);
            ImageFormat format = image.RawFormat;
            if (format.Equals(ImageFormat.Jpeg))
            {
                file += ".jpeg";
            }
            else if (format.Equals(ImageFormat.Png))
            {
                file += ".png";
            }
            else if (format.Equals(ImageFormat.Bmp))
            {
                file += ".bmp";
            }
            else if (format.Equals(ImageFormat.Gif))
            {
                file += ".gif";
            }
            else if (format.Equals(ImageFormat.Icon))
            {
                file += ".icon";
            }
            System.IO.FileInfo info = new System.IO.FileInfo(file);
            System.IO.Directory.CreateDirectory(info.Directory.FullName);
            File.WriteAllBytes(file, buffer);
            return file;
        }

发表于 2018-1-5 10:22:21 | 显示全部楼层
httphelper部分怎么写的应该是没有加相关属性,我测试完全正常
[C#] 纯文本查看 复制代码
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
    URL = "http://g.hiphotos.baidu.com/image/pic/item/b999a9014c086e060b71f53008087bf40bd1cbf4.jpg",//URL     必需项
    Method = "GET",//URL     可选项 默认为Get
    Timeout = 100000,//连接超时时间     可选项默认为100000
    ReadWriteTimeout = 30000,//写入Post数据超时时间     可选项默认为30000
    IsToLower = false,//得到的HTML代码是否转成小写     可选项默认转小写
    Cookie = "",//字符串Cookie     可选项
    UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",//用户的浏览器类型,版本,操作系统     可选项有默认值
    Accept = "text/html, application/xhtml+xml, */*",//    可选项有默认值
    ContentType = "text/html",//返回类型    可选项有默认值
    Referer ="http://g.hiphotos.baidu.com/image/pic/item/b999a9014c086e060b71f53008087bf40bd1cbf4.jpg",//来源URL     可选项
    Allowautoredirect = False,//是否根据301跳转     可选项
    AutoRedirectCookie = False,//是否自动处理Cookie     可选项
    //CerPath = "d:\123.cer",//证书绝对路径     可选项不需要证书时可以不写这个参数
    //Connectionlimit = 1024,//最大连接数     可选项 默认为1024
    Postdata = "",//Post数据     可选项GET时不需要写
    //ProxyIp = "192.168.1.105:2020",//代理服务器ID     可选项 不需要代理 时可以不设置这三个参数
    //ProxyPwd = "123456",//代理服务器密码     可选项
    //ProxyUserName = "administrator",//代理服务器账户名     可选项
    ResultType = ResultType.String,//返回数据类型,是Byte还是String
};
HttpResult result = http.GetHtml(item);
string html = result.Html;
string cookie = result.Cookie;
        

        /// <summary>
        /// 字节数组生成图片
        /// </summary>
        /// <param name="Bytes">字节数组</param>
        /// <returns>图片</returns>
        private Image byteArrayToImage(byte[] Bytes)
        {
            MemoryStream ms = new MemoryStream(Bytes);
            return Bitmap.FromStream(ms,true);
        }


直接复制就能用
 楼主| 发表于 2018-1-5 11:02:48 | 显示全部楼层
TIM图片20180105110322.png
您后面的函数是怎么调用的,没有啊
 楼主| 发表于 2018-1-5 11:06:40 | 显示全部楼层
111.png
 楼主| 发表于 2018-1-5 11:10:23 | 显示全部楼层
我保存是
[C#] 纯文本查看 复制代码
  HttpResult result = http.GetHtml(item);
            byte[] html = result.ResultByte;

发表于 2018-1-8 20:43:11 | 显示全部楼层
路过看看,学习下
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-4-19 14:00

© 2014-2021

快速回复 返回顶部 返回列表