Java获取请求者IP和地址

该博客介绍了如何利用Spring Boot调用Web Service接口来获取IP地址的详细地理位置信息。首先,展示了获取客户端IP的Java代码,然后讲解了如何解析Web Service的WSDL文件并生成相应的Java类。接着,通过配置SOAP客户端,实现了调用远程服务并获取IP地址解析结果的过程。最后,给出了测试用例和实际调用的示例。
摘要由CSDN通过智能技术生成

获取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));
}

在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值