Делаешь хэндлер
| Код | /// <summary> /// Summary description for $codebehindclassname$ /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class GismeteoHandler : IHttpHandler { [DllImport("advapi32.dll")] public static extern bool LogonUser(string lpszUserName, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out int phToken);
private byte[] ImageContent { get { return HttpContext.Current.Cache["GisMeteoWeatherImage"] as byte[]; } set { HttpContext.Current.Cache.Add("GisMeteoWeatherImage", value, null, DateTime.Now.AddMinutes(15.0), Cache.NoSlidingExpiration, CacheItemPriority.Low, null); } }
public void ProcessRequest(HttpContext context) { if (ImageContent == null) { ReadWeatherImageContent(); }
context.Response.AddHeader("Content-Length", ImageContent.Length.ToString()); context.Response.ContentType = "image/gif"; context.Response.BinaryWrite(ImageContent); }
private void ReadWeatherImageContent() { // данные аккаунта с доступом в инет лучше хранить зашифрованными в web.config string weatherUserId = ""; string weatherUserDomain = ""; string weatherUserPassword = "";
int returnedToken;
if (LogonUser(weatherUserId, weatherUserDomain, weatherUserPassword, 3, 0, out returnedToken)) { IntPtr token = new IntPtr(returnedToken); WindowsImpersonationContext impersonationContext = WindowsIdentity.Impersonate(token); try { var request = (HttpWebRequest)WebRequest.Create("http://informer.gismeteo.ru/new/4944-30.GIF"); var response = (HttpWebResponse)request.GetResponse(); using (StreamReader stream = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(1251))) { ImageContent = Encoding.GetEncoding(1251).GetBytes(stream.ReadToEnd()); } } finally { impersonationContext.Undo(); } } }
public bool IsReusable { get { return false; } } }
|
на странице:
| Код | <asp:Image runat="server" ImageUrl="~/AppCode/GismeteoHandler.ashx" />
|
|