public HttpResponseMessage GetCheckCode() { //返回验证码 HttpResponseMessage responsemessage = new HttpResponseMessage(); string sCode = string.Empty; var imgStream = CommonTool.GetCheckCode(out sCode, 4);// new MemoryStream(File.ReadAllBytes(imgPath)); if (string.IsNullOrEmpty(sCode)) { responsemessage = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent("验证码生成错误"), ReasonPhrase = "验证码生成错误" }; throw new HttpResponseException(responsemessage); } //将验证码放入header中,登录的时候获取头部内容与传入的值进行比较,也可以放到session里面 responsemessage = Request.CreateResponse(HttpStatusCode.OK);//new HttpResponseMessage(HttpStatusCode.OK); //responsemessage.Content = new StreamContent(imgStream); responsemessage.Content = new ByteArrayContent(imgStream.ToArray()); responsemessage.Headers.Add("X-CheckCode", sCode); responsemessage.Content.Headers.ContentType = new MediaTypeHeaderValue("image/png"); return(responsemessage); }