【问题标题】:how to display image directly from httpwebresponse stream in asp.net?如何直接从asp.net中的httpwebresponse流显示图像?
【发布时间】:2013-07-22 05:43:36
【问题描述】:

我想直接显示从 webresponse 收到的图像到浏览器而不保存到文件。我使用下面的代码将图像保存到文件,但我想直接在浏览器中显示。网站在 webresponse 中提供验证码图像,我想展示。请帮帮我。

public void captcha(string id, string pass)
{
    HttpWebRequest req;
    HttpWebResponse response;
    string strNewValue,ckuser,ckpass;
    System.Drawing.Image returnval;
    ckuser = id;
    ckpass = pass;
    this.req = (HttpWebRequest)WebRequest.Create("http://site2sms.com/security/captcha.asp");
    ServicePointManager.Expect100Continue = false;
    this.req.CookieContainer = new CookieContainer();
    this.req.AllowAutoRedirect = false;
    this.req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0";
    this.req.Accept = "*/*";
    this.req.Method = "POST";
    this.req.CookieContainer = cookieCntr;
    this.req.ContentType = "application/x-www-form-urlencoded";
    this.req.Referer = "http://site2sms.com/verification.asp?source=login";
    this.strNewValue = "user=" + ckuser + "&password=" + ckpass + "&Submit=Sign+in";
    this.req.ContentLength = this.strNewValue.Length;
    StreamWriter writer = new StreamWriter(this.req.GetRequestStream(), Encoding.ASCII);
    writer.Write(this.strNewValue);
    writer.Close();
    this.response = (HttpWebResponse)this.req.GetResponse();
    returnval = Image.FromStream(response.GetResponseStream());
   // returnval.Save( Server.MapPath("captcha.bmp"));
    Response.AppendHeader("Content-Disposition:", "inline;filename=captcha.bmp");
    Response.ContentType = "image/bmp";
    Response.Write(returnval);
    Response.End();
    this.response.Close();
 }

【问题讨论】:

  • 请详细说明代码有什么问题或抛出了什么错误?
  • 请检查您的图片保存路径权限。

标签: c# asp.net httpwebresponse


【解决方案1】:
  • 您可以使用 HttpResponse.WriteFile 方法将流直接发送到客户端。 将指定文件直接写入 HTTP 响应输出流。

  • 您可以创建IHttpHandler来发送流,从而避免一些页面生命周期,提高性能。 Here is the link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-24
    • 1970-01-01
    相关资源
    最近更新 更多