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

      

  • 相关阅读:
    C# 防止同一个账号多次登录(cache方法)
    前端--关于Canvas
    前端--关于客户端javascript
    前端--关于javascript函数
    前端--关于javascript对象
    前端--关于javascript基础
    前端--关于背景、浮动和定位
    前端--关于CSS盒模型
    前端--关于CSS文本
    前端--关于css选择器
  • 原文地址:https://www.cnblogs.com/ymworkroom/p/11851446.html
Copyright © 2011-2022 走看看