请求地址工具

Posted gjths

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了请求地址工具相关的知识,希望对你有一定的参考价值。

import javax.net.ssl.*;
import java.io.*;
import java.net.URL;


public static String httpsRequest(String requestUrl, String requestMethod, String outputStr) {
StringBuffer buffer = null;
try {
//创建SSLContext
SSLContext sslContext = SSLContext.getInstance("SSL");
TrustManager[] tm = {new MyX509TrustManager()};
//初始化
sslContext.init(null, tm, new java.security.SecureRandom());
;
//获取SSLSocketFactory对象
SSLSocketFactory ssf = sslContext.getSocketFactory();
URL url = new URL(requestUrl);
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setRequestMethod(requestMethod);
//设置当前实例使用的SSLSoctetFactory
conn.setSSLSocketFactory(ssf);
conn.connect();
//往服务器端写内容
if (null != outputStr) {
OutputStream os = conn.getOutputStream();
os.write(outputStr.getBytes("utf-8"));
os.close();
}

//读取服务器端返回的内容
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "utf-8");
BufferedReader br = new BufferedReader(isr);
buffer = new StringBuffer();
String line = null;
while ((line = br.readLine()) != null) {
buffer.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return buffer.toString();
}

以上是关于请求地址工具的主要内容,如果未能解决你的问题,请参考以下文章

请求地址工具

请求地址工具

根据请求的HttpServletRequest获取ip地址工具类

基于C#的Http参数化请求工具

欺骗 HTTP 请求的源 IP 地址

快速上手使用本地测试工具postman