MCPcopy Create free account
hub / github.com/0opslab/opslabJutil / sendPostSSL

Method sendPostSSL

src/main/java/com/opslab/helper/HttpHelper.java:261–353  ·  view source on GitHub ↗

发起POST请求 支持忽略SSL校验 @param httpUrl @param param @param isIgnoreSSL @return

(String httpUrl, String param, boolean isIgnoreSSL)

Source from the content-addressed store, hash-verified

259 * @return
260 */
261 public static String sendPostSSL(String httpUrl, String param, boolean isIgnoreSSL) {
262 HttpURLConnection connection = null;
263 InputStream is = null;
264 OutputStream os = null;
265 BufferedReader br = null;
266 String result = null;
267 try {
268 if (isIgnoreSSL) {
269 //该部分必须在获取connection前调用
270 trustAllHttpsCertificates();
271 HostnameVerifier hv = new HostnameVerifier() {
272 public boolean verify(String urlHostName, SSLSession session) {
273 return true;
274 }
275 };
276 HttpsURLConnection.setDefaultHostnameVerifier(hv);
277 connection = (HttpURLConnection) new URL(httpUrl).openConnection();
278 // 设置连接方式:get
279 connection.setRequestMethod("GET");
280 // 设置连接主机服务器的超时时间:15000毫秒
281 connection.setConnectTimeout(15000);
282 // 设置读取远程返回的数据时间:60000毫秒
283 connection.setReadTimeout(60000);
284 } else {
285 URL url = new URL(httpUrl);
286 // 通过远程url连接对象打开连接
287 connection = (HttpURLConnection) url.openConnection();
288 // 设置连接请求方式
289 connection.setRequestMethod("POST");
290 // 设置连接主机服务器超时时间:15000毫秒
291 connection.setConnectTimeout(15000);
292 // 设置读取主机服务器返回数据超时时间:60000毫秒
293 connection.setReadTimeout(60000);
294 }
295
296
297 // 默认值为:false,当向远程服务器传送数据/写数据时,需要设置为true
298 connection.setDoOutput(true);
299 // 默认值为:true,当前向远程服务读取数据时,设置为true,该参数可有可无
300 connection.setDoInput(true);
301 // 设置传入参数的格式:请求参数应该是 name1=value1&name2=value2 的形式。
302 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
303 // 设置鉴权信息:Authorization: Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0
304 //connection.setRequestProperty("Authorization", "");
305 // 通过连接对象获取一个输出流
306 os = connection.getOutputStream();
307 // 通过输出流对象将参数写出去/传输出去,它是通过字节数组写出的
308 os.write(param.getBytes());
309 // 通过连接对象获取一个输入流,向远程读取
310 if (connection.getResponseCode() == 200) {
311 is = connection.getInputStream();
312 // 对输入流对象进行包装:charset根据工作项目组的要求来设置
313 br = new BufferedReader(new InputStreamReader(is, Opslab.UTF_8));
314
315 StringBuilder sbf = new StringBuilder();
316 String temp;
317 // 循环遍历一行一行读取数据
318 while ((temp = br.readLine()) != null) {

Callers 1

testPostMethod · 0.95

Calls 3

writeMethod · 0.45
toStringMethod · 0.45

Tested by 1

testPostMethod · 0.76