您的位置:首页 > 编程语言 > Java开发

记录一次Java中发起https请求数据接口

2020-08-20 20:32 197 查看
[code]
import javax.net.ssl.*;
import java.io.*;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.HashMap;
import java.util.Map;

public class HttpsUtil {

private static final class DefaultTrustManager implements X509TrustManager {
@Override
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
}

@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}
}

public static String getStringFromInputStream(InputStream inputStream) {
StringBuffer str = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
try {
String temp;
while ((temp = br.readLine()) != null) {
str.append(temp);
}
} catch (IOException e) {
} finally {
close(inputStream);
}
return str.toString();
}

private static final void close(Closeable closeable) {
if (closeable != null) {
try {
closeable.close();
} catch (IOException e) {
}
}
}

public static Map<String, Object> doGet(String uri) {
Map<String, Object> resultMap = new HashMap<>();
String msg = "";
int success = 0;

try {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
SSLSocketFactory ssf = ctx.getSocketFactory();

URL url = new URL(uri);
HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
httpsConn.setSSLSocketFactory(ssf);
httpsConn.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
httpsConn.setRequestMethod("GET");
httpsConn.setDoInput(true);
httpsConn.setDoOutput(true);

int respCode = httpsConn.getResponseCode();
if (respCode == 200) {
success = 1;
msg = getStringFromInputStream(httpsConn.getInputStream());
}else{
success = -1;
msg = "response_status: " + respCode;
}
} catch (Exception e) {
success = -2;
msg = "error msg : " + e.getMessage();
e.printStackTrace();
} finally {
}

resultMap.put("msg", msg);
resultMap.put("success", success);
return resultMap;
}

public static Map<String, Object> doPost(String uri, String data) {
Map<String, Object> resultMap = new HashMap<>();
String msg = "";
int success = 0;

try {
SSLContext ctx = SSLContext.getInstance("TLS");
ctx.init(new KeyManager[0], new TrustManager[] { new DefaultTrustManager() }, new SecureRandom());
SSLSocketFactory ssf = ctx.getSocketFactory();

URL url = new URL(uri);
HttpsURLConnection httpsConn = (HttpsURLConnection) url.openConnection();
httpsConn.setSSLSocketFactory(ssf);
httpsConn.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
});
httpsConn.setRequestMethod("POST");
httpsConn.setDoInput(true);
httpsConn.setDoOutput(true);

// 参数写入
DataOutputStream out = new DataOutputStream(httpsConn.getOutputStream());
out.writeBytes(data);
out.flush();
out.close();

int respCode = httpsConn.getResponseCode();
if (respCode == 200) {
success = 1;
msg = getStringFromInputStream(httpsConn.getInputStream());
}else{
success = -1;
msg = "response_status: " + respCode;
}
} catch (Exception e) {
success = -2;
msg = "error msg : " + e.getMessage();
e.printStackTrace();
} finally {
}

resultMap.put("msg", msg);
resultMap.put("success", success);
return resultMap;

}

}

 

 

String httpsurl = "";
String pars = "key=1&secret=1&time=2&sign=3&params=333";//
Map res = HttpsUtil.doPost(httpsurl,pars);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: