获取IP
public static String getIp(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.getHeader("HTTP_CLIENT_IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getHeader("HTTP_X_FORWARDED_FOR");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
return ip;
}
根据IP解析地址
在网上一番寻找,希望能找到一个根据IP解析地址的API,终于不负有心人,让我找到了,可是这个API是一个webservice,于是又学会了webservice调用,这个API来自webxml.com.cn,有着丰富的webservice接口,通过soapui调用情况如下图
http://ws.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl
下面是实现根据IP获取地址的静态工具方法,说明:
userIp:不用多说,是上面方法获取到的IP
soapConnector: 简单说,就是soap接口的请求入口
再看方法里边,我们定义了url(实际请求地址)、request(顾名思义,定义请求报文的)、response(顾名思义,是咱们需要的响应啦)soapAction(可自行了解Soap接口为啥需要soapaction,如果不传,我们是无法调用成功的),下面我们一个个来。
public static String getAddressByIp(String userIp, SOAPConnector soapConnector){
String address = "";
String url = "http://ws.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx";
GetCountryCityByIp request = new GetCountryCityByIp();
request.setTheIpAddress(userIp);
SoapActionCallback soapAction = new SoapActionCallback("http://WebXml.com.cn/getCountryCityByIp");
GetCountryCityByIpResponse response = (GetCountryCityByIpResponse) soapConnector.callWebService(url, request, soapAction);
address = response.getGetCountryCityByIpResult().getString().get(1);
return address;
}
GetCountryCityByIp 和 GetCountryCityByIpResponse
实际上,这两个类是通过maven的插件生成的
需要修改的节点为generatePackage,更换成自己的包路径
然后执行maven的compile,会在对应的包路径下生成一些类,这些类对应着webservice每个方法里的请求和响应
...略
<!-- 解析webservice的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
...略
<plugin>
<groupId>org.jvnet.jaxb2.maven2</groupId>
<artifactId>maven-jaxb2-plugin</artifactId>
<version>0.14.0</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemaLanguage>WSDL</schemaLanguage>
<generateDirectory>src/main/java</generateDirectory>
<generatePackage>cn.xyp.utils.soap.consumer.generated</generatePackage>
<schemas>
<schema>
<url>http://ws.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl</url>
</schema>
</schemas>
</configuration>
</plugin>
soapAction,可以通过soapui获取
soapConnector
除了SoapConnector,还需要一个配置类
package cn.xyp.utils.soap.consumer.client;
import org.springframework.ws.client.core.support.WebServiceGatewaySupport;
import org.springframework.ws.soap.client.core.SoapActionCallback;
public class SOAPConnector extends WebServiceGatewaySupport {
public Object callWebService(String url, Object request, SoapActionCallback soapAction) {
return getWebServiceTemplate().marshalSendAndReceive(url, request, soapAction);
}
}
package cn.xyp.utils.soap.consumer.config;
import cn.xyp.utils.soap.consumer.client.SOAPConnector;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.jaxb.Jaxb2Marshaller;
@Configuration
public class SoapClientConfig {
@Bean
public Jaxb2Marshaller marshaller() {
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
marshaller.setContextPath("cn.xyp.utils.soap.consumer.generated");
return marshaller;
}
@Bean
public SOAPConnector soapConnector(Jaxb2Marshaller marshaller) {
SOAPConnector client = new SOAPConnector();
client.setDefaultUri("http://ws.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx");
client.setMarshaller(marshaller);
client.setUnmarshaller(marshaller);
return client;
}
}
如何调用getAddressByIp
@Autowired
private SOAPConnector soapConnector;
IpUtil.getAddressByIp(userIp, soapConnector)
测试
@Test
public void getAddressByIp(){
String url = "http://ws.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx";
GetCountryCityByIp request = new GetCountryCityByIp();
request.setTheIpAddress("101.227.1.197");
SoapActionCallback soapAction = new SoapActionCallback("http://WebXml.com.cn/getCountryCityByIp");
GetCountryCityByIpResponse response = (GetCountryCityByIpResponse) soapConnector.callWebService(url, request, soapAction);
System.out.println(response.getGetCountryCityByIpResult().getString().get(1));
}