从服务器A 访问服务器B, 服务器B读服务器A的ip地址,这时候如何将服务器A的ip模拟一个手机ip地址,而不是实际的服务器A地址。
public static String sendWithIP(String url,Map<String,String> map,String encoding,String ip) {
CloseableHttpResponse response = null;
String body = "";
CloseableHttpClient client = HttpClients.custom().setRetryHandler(new DefaultHttpRequestRetryHandler(0,false)).build();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
if(map!=null) {
for(Map.Entry<String,String> entry:map.entrySet()) {
nvps.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
}
}
try {
httpPost.setEntity(new UrlEncodedFormEntity(nvps,encoding));
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(10000).setSocketTimeout(10000).build();
httpPost.setConfig(requestConfig);
httpPost.addHeader("x-forwarded-for",ip);
httpPost.addHeader("X-Real-IP",ip);
response = client.execute(httpPost);
HttpEntity entity = response.getEntity();
if (entity != null) {
body = EntityUtils.toString(entity,encoding);
}
EntityUtils.consume(entity);
response.close();
System.out.println("响应:"+body);
System.out.println("response:"+response.getStatusLine());
} catch (Exception e) {
e.printStackTrace();
}
return body;
}
这个方法没有起作用,有没有其他方法,欢迎高手指教。
--
FROM 111.30.225.*