Processing IP addresses in WebService

Source: Internet
Author: User
Tags time in milliseconds

In the WebService, write:

Import Javax.annotation.Resource;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.xml.ws.WebServiceContext;
Import Javax.xml.ws.handler.MessageContext;

Import org.apache.cxf.transport.http.AbstractHTTPDestination;
Import org.springframework.beans.factory.annotation.Autowired;

@Resource
Private Webservicecontext context;

The container automatically injects the websericecontext into the attribute.

public void getipaddress () {

Messagecontext CTX = Context.getmessagecontext ();

HttpServletRequest request = (httpservletrequest) ctx.get (abstracthttpdestination.http_request);

String ip=request.getremoteaddr (); Get address

}

Attached: Get detailed city and county via IP:

Write this class first, this is the IP query location via an interface on the Internet


Import Java.io.BufferedReader;
Import Java.io.DataOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
Import java.net.HttpURLConnection;
Import Java.net.URL;

/**
* @author Dingzongbiao
* @version creation time: 2014-5-8 04:18:45
*/
public class Addressbyiputil {


public static String getipaddr (HttpServletRequest request) {
String IP = request.getheader ("X-forwarded-for");

if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {

ip = Request.getheader ("Proxy-client-ip");

}

if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {

ip = Request.getheader ("Wl-proxy-client-ip");

}

if (IP = = NULL | | ip.length () = = 0 | | "Unknown". Equalsignorecase (IP)) {

ip = request.getremoteaddr ();

}

return IP;

}

/**
*
* @param content
* The requested parameter format is: name=xxx&pwd=xxx
* @param encoding
* Server-side request encoding. such as Gbk,utf-8, etc.
* @return
*/
public static string GetAddresses (string content, String encodingstring) {
Content= "ip=" +content;
This calls the Pconline interface.
String urlstr = "http://whois.pconline.com.cn/ipJson.jsp";
From http://whois.pconline.com.cn to obtain the information of the provinces and cities where IP resides
String returnstr = Addressbyiputil.getresult (urlstr, Content, encodingstring);
if (returnstr! = null) {
Handling returned provincial and municipal information
int startIndex = Returnstr.indexof ("\" Pro ");
int endIndex = Returnstr.indexof (", \" addr ");
if (StartIndex > 0 && endIndex > 0 && endIndex > StartIndex) {
Returnstr = returnstr.substring (StartIndex, EndIndex);
Returnstr = Returnstr.replaceall ("\" "," "). Replace (" Pro: "," ")
. Replace ("City:", ""). Replace ("region:", "");
}
}
Return returnstr.substring (0,returnstr.length ()-1);
}

 /**
  * @param urlstr
  *             Requested address
  * @param content
  *           The parameter format for the   request is: name=xxx&pwd=xxx
  * @param encoding
  *             server-side request encoding. such as Gbk,utf-8
  * @return
  */
 private static string GetResult (string urlstr, string content, String encoding) {
  url URL = null;
  httpurlconnection connection = null;
  try {
   url = new URL (urlstr);
   connection = (httpurlconnection) url.openconnection ();//New Connection instance
    Connection.setconnecttimeout (2000);//Set the connection time-out, in milliseconds
   connection.setreadtimeout (2000);// Set the read data time-out time in milliseconds

Connection.setdooutput (TRUE);//whether to open the output stream True|false
Connection.setdoinput (TRUE);//whether the input stream is open True|false
Connection.setrequestmethod ("POST");//Submission Method post| GET
Connection.setusecaches (false);//whether to cache True|false
Connection.connect ();//Open the connection port

DataOutputStream out = new DataOutputStream (connection
. Getoutputstream ());//Open the output stream to write data to the peer server
Out.writebytes (content);//write data, that is, submit your form name=xxx&pwd=xxx
Out.flush ();//Refresh
Out.close ();//Close output stream
BufferedReader reader = new BufferedReader (New InputStreamReader (
Connection.getinputstream (), encoding));//write data to end server back to end
To read the BufferedReader stream.
StringBuffer buffer = new StringBuffer ();
String line = "";
while (line = Reader.readline ()) = null) {
Buffer.append (line);
}
Reader.close ();
return buffer.tostring ();
} catch (IOException e) {
E.printstacktrace ();
} finally {
if (connection! = null) {
Connection.disconnect ();//close connection
}
}
return null;
}

}

This is the test class

public static void Main (string[] args) throws Exception {
Addressbyiputil addressutils = new Addressbyiputil ();
Test IP
String IP = "111.204.22.226";
String address = addressutils.getaddresses (IP, "GBK");
SYSTEM.OUT.PRINTLN (address);

}

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.