zoukankan      html  css  js  c++  java
  • .net获取网络图片

    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;
                }
            }
        }
    }
    

      

  • 相关阅读:
    Spring事务
    org.apache.catalina.webresources.Cache.getResource Unable to add the resource
    CentOS7下zip解压和unzip压缩文件
    通过Maven插件发布JaveEE项目到tomcat下
    MYSQL5.7版本sql_mode=only_full_group_by问题
    CentOS下安装Tomcat
    MYSQL57密码策略修改
    CentOS下安装mysql5.7和mysql8.x
    Linux下使用systemctl命令
    076-PHP数组修改元素值
  • 原文地址:https://www.cnblogs.com/ymworkroom/p/11851446.html
Copyright © 2011-2022 走看看