G
ghause
I'm trying to stream in image from another domain asynchronously.
My ashx looks like this:
<%@ WebHandler Language="VB" Class="Handlers.AsyncImageHandler" Debug="true"
%>
Here is the vb:
Imports System.net
imports system.io
Namespace .Handlers
Public Class AsyncImageHandler
Implements System.Web.IHttpAsyncHandler
Private _context As HttpContext
Private _webreq As HttpWebRequest
Sub ProcessRequest(ByVal context As HttpContext) _
Implements System.Web.IHttpHandler.ProcessRequest
Throw New InvalidOperationException()
End Sub
Function BeginProcessRequest(ByVal context As HttpContext, ByVal cb
As AsyncCallback, ByVal extraData As Object) As IAsyncResult _
Implements System.Web.IHttpAsyncHandler.BeginProcessRequest
' Save a reference to the HttpContext
_context = System.Web.HttpContext.Current
_webreq =
DirectCast(WebRequest.Create("http://www.nws.noaa.gov/weather/images/fcicons/shra20.jpg"), HttpWebRequest)
Return _webreq.BeginGetResponse(cb, extraData)
End Function
Sub EndProcessRequest(ByVal result As IAsyncResult) _
Implements System.Web.IHttpAsyncHandler.EndProcessRequest
Try
Dim response As HttpWebResponse =
CType(_webreq.EndGetResponse(result), HttpWebResponse)
Dim Image As Byte()
Image =
getImageByteArray(System.Drawing.Image.FromStream(response.GetResponseStream()))
_context.Response.ContentType = "image/jpeg"
_context.Response.AddHeader("Vary", "User-Agent")
_context.Response.OutputStream.Write(Image, 78, Image.Length
- 78)
Finally
End Try
End Sub
ReadOnly Property IsReusable() As Boolean Implements
IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Private Function getImageByteArray(ByVal image As
System.Drawing.Image) As Byte()
Dim ms As New MemoryStream()
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Return ms.ToArray()
End Function
End Class
End Namespace
The baffiling response content (in text format) looks like this:
<%@ WebHandler Language="VB" Class="Handlers.AsyncImageHandler" Debug="true"
%>
����
What could I be doing that is sending the handler directive in the response?
My ashx looks like this:
<%@ WebHandler Language="VB" Class="Handlers.AsyncImageHandler" Debug="true"
%>
Here is the vb:
Imports System.net
imports system.io
Namespace .Handlers
Public Class AsyncImageHandler
Implements System.Web.IHttpAsyncHandler
Private _context As HttpContext
Private _webreq As HttpWebRequest
Sub ProcessRequest(ByVal context As HttpContext) _
Implements System.Web.IHttpHandler.ProcessRequest
Throw New InvalidOperationException()
End Sub
Function BeginProcessRequest(ByVal context As HttpContext, ByVal cb
As AsyncCallback, ByVal extraData As Object) As IAsyncResult _
Implements System.Web.IHttpAsyncHandler.BeginProcessRequest
' Save a reference to the HttpContext
_context = System.Web.HttpContext.Current
_webreq =
DirectCast(WebRequest.Create("http://www.nws.noaa.gov/weather/images/fcicons/shra20.jpg"), HttpWebRequest)
Return _webreq.BeginGetResponse(cb, extraData)
End Function
Sub EndProcessRequest(ByVal result As IAsyncResult) _
Implements System.Web.IHttpAsyncHandler.EndProcessRequest
Try
Dim response As HttpWebResponse =
CType(_webreq.EndGetResponse(result), HttpWebResponse)
Dim Image As Byte()
Image =
getImageByteArray(System.Drawing.Image.FromStream(response.GetResponseStream()))
_context.Response.ContentType = "image/jpeg"
_context.Response.AddHeader("Vary", "User-Agent")
_context.Response.OutputStream.Write(Image, 78, Image.Length
- 78)
Finally
End Try
End Sub
ReadOnly Property IsReusable() As Boolean Implements
IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Private Function getImageByteArray(ByVal image As
System.Drawing.Image) As Byte()
Dim ms As New MemoryStream()
image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg)
Return ms.ToArray()
End Function
End Class
End Namespace
The baffiling response content (in text format) looks like this:
<%@ WebHandler Language="VB" Class="Handlers.AsyncImageHandler" Debug="true"
%>
����
What could I be doing that is sending the handler directive in the response?