Reading Image from Web Server in C# proxy

后端 未结 4 1218
粉色の甜心
粉色の甜心 2020-12-21 03:49

I am trying to write a proxy which reads an image from one server and returns it to the HttpContext supplied, but I am just getting character stream back.

I am tryin

相关标签:
4条回答
  • 2020-12-21 04:12

    I use this in an application currently. Content URL is passed in as a query string value (the URL to the image).

    try
    {
        if (ContentUrl != "")
        {
            string imgExtension = ContentUrl.Substring(ContentUrl.Length - 3, 3);
            switch (imgExtension)
            {
                case "":
                    //image/bmp
                    Response.ContentType = "image/bmp";
                    break;
    
                case "jpg":
                    //image/jpeg
                    Response.ContentType = "image/jpeg";
                    break;
    
                case "gif":
                    //image/gif
                    Response.ContentType = "image/gif";
                    break;
    
                default:
                    Response.ContentType = "image/jpeg";
                    break;
            }
    
            if (!ContentUrl.StartsWith("http"))
                Response.BinaryWrite(new byte[] { 0 });
    
            WebClient wc = new WebClient();
            wc.Credentials = System.Net.CredentialCache.DefaultCredentials;
            Byte[] result;
            result = wc.DownloadData(ContentUrl);
    
    
            Response.BinaryWrite(result);
    
        }
    }
    catch (Exception ex)
    {
        Utility.WriteEventError(Utility.EVENTLOG_SOURCE, string.Format("ImageProxy Error... Url:  {0}, Exception: {1}", ContentUrl, ex.ToString()));
    }
    finally
    {
        Response.End();
    }
    
    0 讨论(0)
  • 2020-12-21 04:16

    Since you are working with binary, you don't want to use StreamReader, which is a TextReader!

    Now, assuming that you've set the content-type correctly, you should just use the response stream:

    const int BUFFER_SIZE = 1024 * 1024;
    
    var req = WebRequest.Create(imageUrl);
    using (var resp = req.GetResponse())
    {
        using (var stream = resp.GetResponseStream())
        {
            var bytes = new byte[BUFFER_SIZE];
            while (true)
            {
                var n = stream.Read(bytes, 0, BUFFER_SIZE);
                if (n == 0)
                {
                    break;
                }
                context.Response.OutputStream.Write(bytes, 0, n);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-21 04:23

    I guess you would need to check the ContentType returned by your WebResponse request.

     if (resp.ContentType.StartsWith("image/"))
     {
       // Do your stuff
     }
    
    0 讨论(0)
  • 2020-12-21 04:23

    You're going to need to set the Content Type on your response. Here's a snippet of code that'll do it:

    // specify that the response is a JPEG
    // Also could use "image/GIF" or "image/PNG" depending on what you're
    // getting from the server
    Response.ContentType = "image/JPEG";
    
    0 讨论(0)
提交回复
热议问题