.net MVC 跨域获取验证码

来源:互联网 发布:软件开发编程招聘 编辑:程序博客网 时间:2024/06/08 03:48
public partial class Illegal : Controller{    public FileContentResult CreateVcode(string url = "http://www.gdhzcgs.com/cgs/captcha.jpg", string sessionidURL = null)    {        if (sessionidURL != null) try { Session["sessionId"] = ((HttpWebRequest)WebRequest.Create(sessionidURL)).GetResponse().Headers["Set-Cookie"].ToString(); } catch { return null; }        HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);        if (!string.IsNullOrEmpty(sessionidURL)) wr.Headers.Add("Cookie", Session["sessionId"].ToString());                try        {            using (WebResponse ws = wr.GetResponse())            {                using (Stream rd = ws.GetResponseStream())                {                    Bitmap image = new Bitmap(rd);                    MemoryStream stream = new MemoryStream();                    image.Save(stream, ImageFormat.Jpeg);                    if (!string.IsNullOrEmpty(sessionidURL)) Session["sessionId"] = ws.Headers["Set-Cookie"].ToString();                    return File(stream.ToArray(), @"image/jpeg");                }            }        }        catch {  return null; }    }}

0 0