【问题标题】:Reading Image from Web Server in C# proxy在 C# 代理中从 Web 服务器读取图像
【发布时间】:2010-11-19 07:01:23
【问题描述】:

我正在尝试编写一个代理,它从一台服务器读取图像并将其返回给提供的 HttpContext,但我只是获取字符流。

我正在尝试以下方法:

WebRequest req = WebRequest.Create(image);
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader sr = new StreamReader(stream);
StreamWriter sw = new StreamWriter (context.Response.OutputStream);

sw.Write (sr.ReadToEnd());

但正如我之前提到的,这只是用文本响应。

如何判断它是图像?

编辑:我从网页中的 img 标签的 source 属性中访问它。将内容类型设置为 application/octet-stream 提示保存文件并将其设置为 image/jpeg 只会以文件名响应。我想要的是调用页面返回并显示的图像。

【问题讨论】:

    标签: c# http image stream


    【解决方案1】:

    由于您使用的是二进制文件,因此您不想使用StreamReader,它是一个文本阅读器!

    现在,假设您已正确设置内容类型,您应该只使用响应流:

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

    【讨论】:

    • 感激不尽。完全把我逼到了墙角。
    • 感谢这对我也很有帮助。这种方法是否适用于任何类型的响应 - 任何形式的文本或二进制文件?
    • 就目前而言,是的。当然对于某些类型的响应,你可能需要设置headers等。
    【解决方案2】:

    我想您需要检查您的 WebResponse 请求返回的 ContentType。

     if (resp.ContentType.StartsWith("image/"))
     {
       // Do your stuff
     }
    

    【讨论】:

      【解决方案3】:

      您需要在响应中设置内容类型。这是一个可以做到这一点的代码:

      // 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";
      

      【讨论】:

      • 这会提示保存文件。我在 img 标签中使用它,所以希望它显示图像。
      【解决方案4】:

      我目前在一个应用程序中使用它。内容 URL 作为查询字符串值(图像的 URL)传入。

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

      【讨论】:

        猜你喜欢
        • 2013-10-17
        • 1970-01-01
        • 1970-01-01
        • 2012-03-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-22
        相关资源
        最近更新 更多