惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园_首页
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
Jina AI
Jina AI
C
Check Point Blog
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
Last Week in AI
Last Week in AI
罗磊的独立博客
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
J
Java Code Geeks
WordPress大学
WordPress大学
博客园 - 聂微东
月光博客
月光博客
博客园 - 司徒正美
T
The Blog of Author Tim Ferriss
大猫的无限游戏
大猫的无限游戏
Microsoft Security Blog
Microsoft Security Blog

博客园 - ymworkroom

命令行在银河麒麟的达梦数据库中创建新的数据库 Python学习——Ai+streamlit实现Ai伴侣(练习流式输出) Python中包的导入 Python中__name__与__all__两个特殊变量的作用 Python中模块导入方式 国内安装Claude方案 连接mysql8.0时报:KeyNotFoundException: 给定关键字不在字典中 vscode中运行的vue项目,vscode关闭后仍然运行,如何停止 [INS-30131] 执行安装程序验证所需的初始设置失败 Java中CornExpression说明 java项目开发常用配置文件模板 oracle数据库导入dmp文件,两种方法 Oracle dmp文件导入(还原)到不同的表空间和不同的用户下 pdf.js在IIS中配置使用笔记 禁止达梦数据库中not null字段插入空字符串 错误:java.lang.NoClassDefFoundError: org/jaxen/JaxenException 服务器级的urn筛选器无效:筛选器必须为空,或服务器属性必须等于实际的服务器名称处理方法 oracle下安装logminer笔记 .net导出图片到Excel Lotus Notes连接Domino服务器时出错 配置Mysql远程访问 mysql Access denied for user root@localhost错误处理备忘 MsSql判断表是否有自增标识 .net用NPOI生成Word表格 .net读取Lotus Domino文件数据库并写入DataTable中
.net获取网络图片
ymworkroom · 2019-11-13 · via 博客园 - ymworkroom
using System.IO;
using System.Net;

namespace TestUpfile
{
    public class NetHelper
    {
        public static Stream GetImgStream(string url,string protocol="http")
        {
            WebResponse response = null;
            Stream stream = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                response = request.GetResponse();
                stream = response.GetResponseStream();
                if (response.ContentType.ToLower().StartsWith("text/")) return null;
                ////如果要转成byte
                //using (MemoryStream mstream = new MemoryStream())
                //{
                //    int count = 0;
                //    byte[] buffer = new byte[1024];
                //    int readnum = 0;
                //    while ((readnum = stream.Read(buffer, 0, 1024)) > 0)
                //    {
                //        count = count + readnum;
                //        mstream.Write(buffer, 0, 1024);
                //    }
                //    mstream.Position = 0;
                //    using (BinaryReader br = new BinaryReader(mstream))
                //    {
                //        byte[] bytes = br.ReadBytes(count);
                //    }
                //}
                return stream;
            }
            catch
            {

            }
            return stream;
        }

        /// <summary>
        /// 通过webClient获取图片的字节
        /// </summary>
        /// <param name="url">要下载的url</param>
        /// <param name="protocol"></param>
        /// <returns></returns>
        public static byte[] GetImgBytes(string url)
        {
            byte[] bytes = null;
            try
            {
                if (url.Substring(0,5).ToLower() == "https")
                {
                    // 解决WebClient不能通过https下载内容问题
                    System.Net.ServicePointManager.ServerCertificateValidationCallback +=
                        delegate (object sender, System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                                 System.Security.Cryptography.X509Certificates.X509Chain chain,
                                 System.Net.Security.SslPolicyErrors sslPolicyErrors)
                        {
                            return true; // **** Always accept
                        };
                }
                WebClient client = new WebClient();
                bytes=client.DownloadData(url);
                return bytes;
            }
            catch
            {
                return null;
            }
        }
    }
}