常用的java 发送http请求的方式有两种
1、通过HttpsURLConnection 自己去发送请求
2、 用spring框架提供的 RestTemplate (import org.springframework.web.client.RestTemplate;) 对象发送请求
第一种方式配置代理服务器关键代码:
int PROXY_PORT = 80;// 改成你自己的代理服务器端口
String PROXY_HOST = "127.0.0.1";// 改成你自己的代理服务器ip地址
System.setProperty("https.proxyHost", PROXY_HOST);
System.setProperty("https.proxyPort", PROXY_PORT);
改代码块放置到访问请求之前即可。
第二种方式配置代理服务器:
RestTemplate restTemplate1 = new RestTemplate();
String url = "www.baidu.com";
String xmlParam = "xxx";
int PROXY_PORT = 80;// 代理服务器端口
String PROXY_HOST = "127.0.0.1";
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT));
SimpleClientHttpRequestFactory httpRequestFactory = new SimpleClientHttpRequestFactory();
httpRequestFactory.setProxy(proxy);
restTemplate1.setRequestFactory(httpRequestFactory);
// 发起请求
String strXML = restTemplate1.postForObject(url, xmlParam, String.class);
下面例子用来发起https请求 配置代理服务
/**
* 发送https请求
* @param requestUrl 请求地址
* @param requestMethod 请求方式(GET、POST)
* @param outputStr 提交的数据
* @return 返回服务器响应的信息
*/
public String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
try {
String PROXY_PORT = "80";// 代理服务器端口
String PROXY_HOST = "127.0.0.1";// 改成你自己的代理服务器ip地址
System.setProperty("https.proxyHost", PROXY_HOST);
System.setProperty("https.proxyPort", PROXY_PORT);
// 创建SSLContext对象,并使用我们指定的信任管理器初始化
TrustManager[] tm = { new MyX509TrustManager() };
SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE");
sslContext.init(null, tm, new java.security.SecureRandom());
// 从上述SSLContext对象中得到SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(ssf);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
// 设置请求方式(GET/POST)
conn.setRequestMethod(requestMethod);
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
// 当outputStr不为null时向输出流写数据
if (null != outputStr) {
OutputStream outputStream = conn.getOutputStream();
// 注意编码格式
outputStream.write(outputStr.getBytes("UTF-8"));
outputStream.close();
}
// 从输入流读取返回内容
InputStream inputStream = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "utf-8");
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String str = null;
StringBuffer buffer = new StringBuffer();
while ((str = bufferedReader.readLine()) != null) {
buffer.append(str);
}
// 释放资源
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
inputStream = null;
conn.disconnect();
return buffer.toString();
} catch (ConnectException ce) {
log.error("连接超时:{}", ce);
} catch (Exception e) {
log.error("https请求异常:{}", e);
}
return null;
}
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.X509TrustManager;
/**
* 信任管理器
*
* Title: MyX509TrustManager
*
* Description:
*
* @author
*
* @date 2020年11月18日
*
*/
public class MyX509TrustManager implements X509TrustManager {
// 检查客户端证书
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
// 检查服务器端证书
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}
// 返回受信任的X509证书数组
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}