locked
Help with generic handlers RRS feed

  • Question

  • User108409295 posted

    I've made robot script of addition two numbers. My user should addition these two numbers for confirmation. i added generic handler ASHX file to my web address with folloving code:

    Public Class Robot : Implements IHttpHandler
       
        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            Dim rnd1 As New Random
            Dim rnd2 As New Random
            Dim i As Integer = rnd1.Next(1, 5)
            Dim j As Integer = rnd2.Next(5, 10)
            Dim prikaz As String = i & "+" & j & "= ?"
            Dim Rezultat As Integer = i + j
           
            Dim font As New Font("Tahoma", 25, FontStyle.Bold, GraphicsUnit.Pixel)
            Dim bitmapa As New Bitmap(100, 30)
            Dim grafika As Graphics = Graphics.FromImage(bitmapa)
            Dim podrucje As New Rectangle(0, 0, 100, 30)
            grafika.FillRectangle(Brushes.Red, podrucje)
            grafika.DrawRectangle(Pens.Black, podrucje)
            grafika.DrawString(prikaz, font, Brushes.Green, 0, 0)

            context.Response.ContentType = "Image/jpeg"
            bitmapa.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
           
            bitmapa.Dispose()
            grafika.Dispose()
        End Sub
     
        Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property

    End Class

    In my .aspx file i have next code on button click:

    imgSlika.ImageUrl = "~/Robot.ashx"

    I want pass variable with name Razultat from ashx with picture to asp.page. So that i could compare result with value od textBox.

    Please help.

    Tuesday, September 15, 2009 6:02 AM

Answers

All replies

  • User108409295 posted

    I reorganized my question.

    Tuesday, September 15, 2009 9:51 AM
  • Tuesday, September 15, 2009 10:32 AM
  • User108409295 posted

    Addition, but i want my own. I have a picture from handler, but i want addition two generated numbers in generic hanler and return result in aspx page so that i can compare result with user input.


    Wednesday, September 16, 2009 2:11 AM
  • User-1363023537 posted

    Hi,

    but i want my own.


    you can create your own
     
    simple CAPTCHA techniqe http://www.revenmerchantservices.com/page/C-Simple-Captcha.aspx 

    like this, I have explained.

    Satalaj

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, September 16, 2009 3:20 AM
  • User108409295 posted

    I've respolved everthing, this is my own captcha wit addition numbers and confirmation.

    Thx to all

    ASPX

    <form id="form1" runat="server">
        <div>
            <asp:TextBox ID="txtCap" runat="server"></asp:TextBox>
            <br />
            <br />
            <asp:Button ID="btnProvjeri" runat="server" Text="Provjeri" />
            <asp:Label ID="Label1" runat="server"></asp:Label>
            <br />
            <br />
            <asp:Image ID="Image1" runat="server" ImageUrl="~/Robot.ashx" />
        </div>
     </form>

    BUTTON CHECK

    Protected Sub btnProvjeri_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnProvjeri.Click
            Dim odgovor As Integer = Convert.ToInt32(Context.Session("Rezultat"))

            If odgovor = Convert.ToInt32(txtCap.Text) Then
                Label1.Text = "OK"
            Else
                Label1.Text = "NO"
            End If

    End Sub


    GENERIC HANDLER

    <%@ WebHandler Language="VB" %>

    Imports System
    Imports System.Web
    Imports System.Drawing
    Imports System.Web.SessionState

    Public Class Robot : Implements IHttpHandler, IRequiresSessionState
        
        
        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            Dim rnd1 As New Random
            Dim rnd2 As New Random
            Dim i As Integer = rnd1.Next(1, 5)
            Dim j As Integer = rnd2.Next(5, 10)
            Dim prikaz As String = i & "+" & j & "= ?"
            Dim Rezultat As Integer = i + j

            context.Session("Rezultat") = Rezultat

            Dim font As New Font("Tahoma", 25, FontStyle.Bold, GraphicsUnit.Pixel)
            Dim bitmapa As New Bitmap(100, 30)
            Dim grafika As Graphics = Graphics.FromImage(bitmapa)
            Dim podrucje As New Rectangle(0, 0, 100, 30)
            grafika.FillRectangle(Brushes.Red, podrucje)
            grafika.DrawRectangle(Pens.Black, podrucje)
            grafika.DrawString(prikaz, font, Brushes.Green, 0, 0)

            context.Response.ContentType = "Image/jpeg"
            bitmapa.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)

            bitmapa.Dispose()
            grafika.Dispose()
        End Sub
     
        Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property

    End Class

    Wednesday, September 16, 2009 6:03 AM
  • User-1363023537 posted

    Yes, when we are working with HttpHandler and Session variables
    we need that handler to inherit IRequiresSessionState.

    Wednesday, September 16, 2009 6:09 AM