Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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]
C#
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 2: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

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.
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900