Click here to Skip to main content
14,422,026 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want to save Ip from handler class and i want to use it another class file.

How can i achieve this.
Plz help.

[Edit]
public class sdp : IHttpHandler
{
    private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    string returnMsg;

    public void ProcessRequest(HttpContext context)
    {

        var ip = context.Request.Headers["Host"];
        // var ip = context.Request["HTTP_X_FORWARDED_FOR"] ?? context.Request.UserHostAddress;
        log.DebugFormat(
            "Received request from IP: {0}, Msisdn: {1}, Message: {2}",
            ip, context.Request["msisdn"], context.Request["msg"]
        );
        var status = Processor.Instance.Process(context.Request.Params["msisdn"], context.Request.Params["msg"]);
        log.Info(status.Description);
        context.Response.ContentType = "text/plain";
        returnMsg = string.Format("3{0}", status.Description);
        log.Info(returnMsg);
        context.Response.Write(returnMsg);
    }
}


[/Edit]
Posted
Updated 16-May-13 3:40am
v2
Comments
debkumar@codeproject 16-May-13 8:20am
   
Which IP?
Ranjith Kumar 16-May-13 8:24am
   
any incoming ip
Ranjith Kumar 16-May-13 8:24am
   
how can i use that ip in another class file
Ranjith Kumar 16-May-13 8:25am
   
///
public class sdp : IHttpHandler
{
private static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
string returnMsg;

public void ProcessRequest(HttpContext context)
{

var ip = context.Request.Headers["Host"];
// var ip = context.Request["HTTP_X_FORWARDED_FOR"] ?? context.Request.UserHostAddress;
log.DebugFormat("Received request from IP: {0}, Msisdn: {1}, Message: {2}",
ip, context.Request["msisdn"], context.Request["msg"]);
var status = Processor.Instance.Process(context.Request.Params["msisdn"], context.Request.Params["msg"]);
log.Info(status.Description);
context.Response.ContentType = "text/plain";
returnMsg = string.Format("<ussd><type>3<msg>{0}", status.Description);
log.Info(returnMsg);
context.Response.Write(returnMsg);
}
lukeer 16-May-13 8:41am
   
Please insert code blocks into your question using the "Improve question" link (FTFY).

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You may store the IP address in HTTP session/cache and use it in some other class. Otherwise, pass the IP information in the class constructor.
   
Comments
Ranjith Kumar 16-May-13 8:31am
   
can u give sample code for using session/cache plzz
debkumar@codeproject 16-May-13 8:35am
   
If your class is in the same application domain, then you would get the request object there. In that, you may capture IP there itself. If it is completely different application domain, then you may have to pass as parameter. In handler class, storing IP in database/file is not a good approach.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100