java httpclient 伪造ip_伪造Http请求IP地址

package com.sh.portal.framework.client.http;

import java.io.IOException;

import org.apache.commons.lang.StringUtils;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.config.RequestConfig;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClientBuilder;

import org.apache.http.message.BasicHeader;

import org.apache.http.protocol.HTTP;

import org.apache.http.util.EntityUtils;

import org.springframework.stereotype.Component;

import com.sh.portal.framework.client.RemoteServerArgs;

import com.sh.portal.framework.client.RemoteServerClient;

import com.sh.portal.framework.client.RemoteServerResponse;

import com.sh.portal.util.CommonUtils;

@Componentpublic classRemoteServerClientImpl implements RemoteServerClient {private static final String DEFAULT_ENCODE = "UTF-8";private static final String APPLICATION_JSON = "application/json";

@OverridepublicRemoteServerResponse post(RemoteServerArgs args) throws IOException {

String ip=CommonUtils.getRequestIpAddress();//创建HttpClientBuilder

HttpClientBuilder httpClientBuilder =HttpClientBuilder.create();//HttpClient

CloseableHttpClient closeableHttpClient =httpClientBuilder.build();//请求参数

StringEntity entity = newStringEntity(args.getRequestJson(), DEFAULT_ENCODE);

entity.setContentEncoding(newBasicHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON));

HttpPost httpPost= newHttpPost(args.getUrl());

httpPost.addHeader(HTTP.CONTENT_TYPE, APPLICATION_JSON);//此处区别PC终端类型

httpPost.addHeader("typeFlg", "9");//此处增加浏览器端访问IP

if(!ip.equals("")){

httpPost.addHeader("x-forwarded-for",ip);

}

httpPost.setEntity(entity);

httpPost.setConfig(RequestConfig.DEFAULT);

HttpResponse httpResponse;//post请求

httpResponse =closeableHttpClient.execute(httpPost);

HttpEntity httpEntity=httpResponse.getEntity();

RemoteServerResponse response;if (httpEntity != null) {

response= newRemoteServerResponse(httpResponse.getStatusLine().getStatusCode(),

EntityUtils.toString(httpEntity, DEFAULT_ENCODE));

}else{

response= newRemoteServerResponse(httpResponse.getStatusLine().getStatusCode(),

StringUtils.EMPTY);

}//释放资源

closeableHttpClient.close();returnresponse;

}

}

原作者:https://www.cnblogs.com/holdon521/p/4686849.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值