Obtain the Client IP Address by using the WebService developed by Java

Source: Internet
Author: User

1. method for obtaining client IP address based on WebService developed by Jax-ws [Add the following code in proxy class xxxdelegate ]:

Import javax. annotation. resource;

Import javax. servlet. http. httpservletrequest;

Import javax. xml. ws. webservicecontext;

Import javax. xml. ws. handler. messagecontext;

@ Resource

Private webservicecontext wscontext;

Private string getclientinfo (){

Messagecontext MC = wscontext. getmessagecontext ();

Httpservletrequest request = (httpservletrequest) (MC. Get (messagecontext. servlet_request ));

String remortaddress = request. getremoteaddr ();

Return (remortaddress );

}

2. method for obtaining client IP address based on WebService released by Axis

/**

* Obtain the Client IP address, such as the caller's IP address, for permission check.

* Applicable to WebService released by Axis

* @ Return

*/

Public String getclientipaxis (){

Messagecontext MC = NULL;

Httpservletrequest request = NULL;

Try {

MC = messagecontext. getcurrentmessagecontext ();

If (MC = NULL)

Throw new exception ("messagecontext cannot be obtained ");

Request = (httpservletrequest) MC

. Getproperty (httpconstants. mc_http_servletrequest );

System. Out. println ("remote IP:" + request. getremoteaddr ());

} Catch (exception e ){

System. Out. println (E. getmessage ());

E. printstacktrace ();

}

Return request. getremoteaddr ();

}

3. method for obtaining client IP address based on WebService released by xfire

/**

* Obtain the Client IP Address

* Applicable to WebService released by xfire

* @ Return

*/

Public String getclientipxfire (){

String IP = "";

Try {

Httpservletrequest request = xfireservletcontroller. getrequest ();

IP = request. getremoteaddr ();

} Catch (exception e ){

System. Out. println ("httpservletrequest. cannot be obtained .");

E. printstacktrace ();

}

Return IP;

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.