Tuesday, January 6, 2015

How to post the data using HTTP Listener in asp.net

How to post the data using HTTP Listener in asp.net

Step1:- Create once sample Empty project name it as “HTTPListener

Step2:- And add the new Generic Handler template and name it as “HandlerListener

This will create one default method is

public void ProcessRequest(HttpContext context)

This method will process the request and resend the request

Step3:- Write below code
public string GetPostDataFromRequest(HttpRequest httpRequest)
        {
            string inputString = string.Empty;
            StreamReader reader = null;
            if (httpRequest.InputStream != null)
            {
                reader = new StreamReader(httpRequest.InputStream);
                inputString = reader.ReadToEnd().Trim();
                reader.Close();
            }
            return inputString;
        }

Step4:- Add the below code in the processrequest method

string strPostData = this.GetPostDataFromRequest(context.Request);
context.Response.ContentType = "text/plain";
context.Response.Write(strPostData);

The complete code is look like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace HTTPListener
{
    /// <summary>
    /// Summary description for HandlerListner
    /// </summary>
    public class HandlerListener : IHttpHandler
    {

        public string GetPostDataFromRequest(HttpRequest httpRequest)
        {
            string inputString = string.Empty;
            StreamReader reader = null;
            if (httpRequest.InputStream != null)
            {
                reader = new StreamReader(httpRequest.InputStream);
                inputString = reader.ReadToEnd().Trim();
                reader.Close();
            }
            return inputString;
        }
        public void ProcessRequest(HttpContext context)
        {
            string strPostData = this.GetPostDataFromRequest(context.Request);
            context.Response.ContentType = "text/plain";
            context.Response.Write(strPostData);
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

Step5:- Finally run the code and check the URL

Steps to checking the URL working process

Step1:-For testing the URL we use the Mozilla Firefox Browser
In that browser add POSTER Add on

Step2:- To run the poster add on Toolsà Posterà
The poster will look like


Step3:- Type the URL and write some data in the content part and click the POST button
This will replay the message what you typed

E.g.:- I entered “Hi lokesh how r u”
The Listener will the replay the same
Using this code you can customize the and meets you requirements

 JJJJJJJJJ
Happy Coding …………………………………………….

JJJJJJJJJ

No comments:

Post a Comment