常用的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;
}
}