Posted by juneof44 on Sat 4 Apr 08:31
report abuse | View followups from reddit | download | new post
- <%@ WebHandler Language="VB" Class="imgur" %>
- Imports System
- Imports System.Web
- Imports System.Net
- Imports System.IO
- Public Class imgur : Implements IHttpHandler
- Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
- If context.Request.QueryString.Count = 0 Then Exit Sub
- Dim remoteWebSite As String = "http://imgur.com/"
- Dim imageName = context.Request.QueryString(0)
- If Not Regex.IsMatch(imageName, "(.*?)\.(jpg|jpeg|png|gif)$", RegexOptions.IgnoreCase) Then
- context.Response.Write("<h2>doesn't look like an image to me :(</h2>")
- context.Response.End()
- Return
- End If
- Dim request = WebRequest.Create(remoteWebSite & imageName)
- Dim response As HttpWebResponse
- Try
- response = request.GetResponse
- Catch ex As WebException
- context.Response.Write("<h2>Image not found</h2>")
- context.Response.End()
- Return
- End Try
- If response.ContentType = "text/html" Then
- context.Response.Write("<h2>Image not found</h2>")
- context.Response.End()
- Return
- End If
- Using recieveStream = response.GetResponseStream
- Dim length = 256
- Dim buffer = New Byte(length) {}
- Dim bytesRead = recieveStream.Read(buffer, 0, length)
- While bytesRead > 0
- context.Response.OutputStream.Write(buffer, 0, bytesRead)
- bytesRead = recieveStream.Read(buffer, 0, length)
- End While
- End Using
- context.Response.ContentType = response.ContentType
- response.Close()
- context.Response.End()
- End Sub
- Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
- Get
- Return False
- End Get
- End Property
- End Class
Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.