Hello Friends,
I read on article which can access web service using Sys.Net.WebRequest. Below is a read article URL i.e
http://www.dotnetfunda.com/articles/article480-accessing-webservice-by-using-sysnetwebrequest-.aspx
So I need to access handler using Sys.Net.WebRequest.
So here is a sample code to access handler using Sys.Net.WebRequest.
WebRequest.Aspx Page
---------------------------------------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="WebRequest.aspx.cs" Inherits="WebRequest" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<input type="text" id="text1" /><br />
<input type="button" id="Button1" value="Button" onclick="return Button1_onclick()" />
</div>
<div id="result">
</div>
</div>
<script language="javascript" type="text/javascript">
function Button1_onclick() {
var myRequest = new Sys.Net.WebRequest;
myRequest.set_url("sampleHandler.ashx");
myRequest.set_httpVerb("POST");
var tbvalue = document.getElementById("text1").value;
var body = "Param1=" + tbvalue;
myRequest.set_body(body);
myRequest.get_headers()["Content-Length"] = body.length;
myRequest.add_completed(myResultHandler);
myRequest.invoke();
}
function myResultHandler(executor, eventArgs) {
if (executor.get_responseAvailable) {
var result = document.getElementById("result");
result.innerHTML = executor.get_responseData();
}
}
</script>
</form>
</body>
</html>
Now Here is a code for 'sampleHandler.ashx'
<%@ WebHandler Language="C#" Class="sampleHandler" %>
using System;
using System.Web;
public class sampleHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string paramValue = string.Empty;
if (HttpContext.Current.Request.Form["Param1"]!="")
{
paramValue = HttpContext.Current.Request.Form["Param1"];
}
context.Response.ContentType = "text/plain";
context.Response.Write(paramValue);
}
public bool IsReusable {
get {
return false;
}
}
}
Happy Coding and Enjoy it...
Send Push Notification using C#
5 years ago
No comments:
Post a Comment