Share Pastebin
Guest
Public paste!

juneof44

By: a guest | Apr 4th, 2009 | Syntax: VB.NET | Size: 2.10 KB | Hits: 632 | Expires: Never
Copy text to clipboard
  1. <%@ WebHandler Language="VB" Class="imgur" %>
  2.  
  3. Imports System
  4. Imports System.Web
  5. Imports System.Net
  6. Imports System.IO
  7.  
  8. Public Class imgur : Implements IHttpHandler
  9.    
  10.     Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
  11.         If context.Request.QueryString.Count = 0 Then Exit Sub
  12.        
  13.         Dim remoteWebSite As String = "http://imgur.com/"
  14.         Dim imageName = context.Request.QueryString(0)
  15.  
  16.                
  17.         If Not Regex.IsMatch(imageName, "(.*?)\.(jpg|jpeg|png|gif)$", RegexOptions.IgnoreCase) Then
  18.             context.Response.Write("<h2>doesn't look like an image to me :(</h2>")
  19.             context.Response.End()
  20.             Return
  21.         End If
  22.        
  23.                                    
  24.         Dim request = WebRequest.Create(remoteWebSite & imageName)
  25.         Dim response As HttpWebResponse
  26.            
  27.         Try
  28.             response = request.GetResponse
  29.         Catch ex As WebException
  30.             context.Response.Write("<h2>Image not found</h2>")
  31.             context.Response.End()
  32.             Return
  33.         End Try
  34.  
  35.         If response.ContentType = "text/html" Then
  36.             context.Response.Write("<h2>Image not found</h2>")
  37.             context.Response.End()
  38.             Return
  39.         End If
  40.        
  41.        
  42.        
  43.         Using recieveStream = response.GetResponseStream
  44.  
  45.             Dim length = 256
  46.             Dim buffer = New Byte(length) {}
  47.             Dim bytesRead = recieveStream.Read(buffer, 0, length)
  48.                    
  49.             While bytesRead > 0
  50.                 context.Response.OutputStream.Write(buffer, 0, bytesRead)
  51.                 bytesRead = recieveStream.Read(buffer, 0, length)
  52.             End While
  53.                
  54.         End Using
  55.  
  56.        
  57.         context.Response.ContentType = response.ContentType
  58.         response.Close()
  59.         context.Response.End()
  60.        
  61.  
  62.     End Sub
  63.  
  64.     Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
  65.         Get
  66.             Return False
  67.         End Get
  68.     End Property
  69.  
  70. End Class