Hi suhaas121,
Check this example. Now please take its reference and correct your code.
Namespaces
C#
using System.Net;
using System.Web.Script.Serialization;
VB.Net
Imports System.Net
Imports System.Web.Script.Serialization
Code
C#
protected void Button1_Click(object sender, EventArgs e)
{
Product product = new Product();
product.status = "SUCCESSFUL";
JavaScriptSerializer js = new JavaScriptSerializer();
// Get the Json response from the api.
string response = js.Serialize(product);
string responseData = (new WebClient()).DownloadString("http://localhost:54496/Handler.ashx?Json=" + response);
if (responseData != "")
{
ClientScript.RegisterStartupScript(this.GetType(), "message", "alert('" + responseData + "');", true);
}
}
public class Product
{
public string status { get; set; }
}
VB.Net
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim product As Product = New Product()
product.status = "SUCCESSFUL"
Dim js As JavaScriptSerializer = New JavaScriptSerializer()
Dim response As String = js.Serialize(product)
Dim responseData As String = (New WebClient()).DownloadString("http://localhost:54496/Handler.ashx?Json=" & response)
If responseData <> "" Then
ClientScript.RegisterStartupScript(Me.GetType(), "message", "alert('" & responseData & "');", True)
End If
End Sub
Public Class Product
Public Property status As String
End Class
Generic Handler
C#
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Web.SessionState;
using System.Web.Script.Serialization;
public class Handler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
if (context.Request.QueryString["Json"] != null)
{
string json = context.Request.QueryString["Json"];
JavaScriptSerializer js = new JavaScriptSerializer();
Product product = js.Deserialize<Product>(json);
string y = product.status;
string output = "";
if (y == "PENDING")
{
output = "THANK YOU";
}
context.Response.ContentType = "text/plain";
context.Response.Write(output);
}
}
public bool IsReusable
{
get
{
return false;
}
}
public class Product
{
public string status { get; set; }
}
}
VB.Net
<%@ WebHandler Language="VB" Class="Handler" %>
Imports System
Imports System.Web
Imports System.Web.Script.Serialization
Public Class Handler : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
If context.Request.QueryString("Json") IsNot Nothing Then
Dim json As String = context.Request.QueryString("Json")
Dim js As JavaScriptSerializer = New JavaScriptSerializer()
Dim product As Product = js.Deserialize(Of Product)(json)
Dim y As String = product.status
Dim output As String = ""
If y = "PENDING" Then
output = "THANK YOU"
End If
context.Response.ContentType = "text/plain"
context.Response.Write(output)
End If
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Public Class Product
Public Property status As String
End Class
End Class