gpt4 book ai didi

json - Http GET 方法到 Https?

转载 作者:太空宇宙 更新时间:2023-11-03 13:03:04 25 4
gpt4 key购买 nike

抱歉我的菜鸟问题,但我有这段代码可以从我的网络服务中获取关于我的数据库的 JSON 信息:

public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
InputStream is = new URL(url).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
return new JSONObject(jsonText);
} finally {
is.close();
}
}

private static String readAll(Reader rd) throws IOException {
StringBuilder sb = new StringBuilder();
int cp;
while ((cp = rd.read()) != -1) {
sb.append((char) cp);
}
return sb.toString();
}

我刚买了一个 SSL 证书,我必须将我的 GET 方法代码从 HTTP 传递到 HTTPS,我该怎么做?

这是我的 TLSSocketFactory,用于启用 TLS 1.1 和 1.2:

public class TLSSocketFactory extends SSLSocketFactory {

private SSLSocketFactory internalSSLSocketFactory;

public TLSSocketFactory() throws KeyManagementException, NoSuchAlgorithmException {
SSLContext context = SSLContext.getInstance("TLS");
context.init(null, null, null);
internalSSLSocketFactory = context.getSocketFactory();
}

@Override
public String[] getDefaultCipherSuites() {
return internalSSLSocketFactory.getDefaultCipherSuites();
}

@Override
public String[] getSupportedCipherSuites() {
return internalSSLSocketFactory.getSupportedCipherSuites();
}

@Override
public Socket createSocket(Socket s, String host, int port, boolean autoClose) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(s, host, port, autoClose));
}

@Override
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}

@Override
public Socket createSocket(String host, int port, InetAddress localHost, int localPort) throws IOException, UnknownHostException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port, localHost, localPort));
}

@Override
public Socket createSocket(InetAddress host, int port) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(host, port));
}

@Override
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort) throws IOException {
return enableTLSOnSocket(internalSSLSocketFactory.createSocket(address, port, localAddress, localPort));
}

private Socket enableTLSOnSocket(Socket socket) {
if(socket != null && (socket instanceof SSLSocket)) {
((SSLSocket)socket).setEnabledProtocols(new String[] {"TLSv1.1", "TLSv1.2"});
}
return socket;
}
}

使用我的 POST 方法,我创建了一个 TLSSocket 工厂的实例,并像这样应用于我的 POST 方法:

TLSSocketFactory tlsSocketFactory = new TLSSocketFactory();
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setSSLSocketFactory(tlsSocketFactory);

但是,我的问题是.. 我怎样才能对我的 GET 方法做同样的事情?

谢谢你帮助我!

最佳答案

要设置您的 TLSSocketFactory 以便在给定您的 url 的情况下使用,假设它是一个 HTTPS URL,您可以使用:

HttpsURLConnection conn = (HttpsURLConnection) new URL(url).openConnection();
conn.setSSLSocketFactory(tlsSocketFactory);

就像您对 POSTPUT 所做的一样。要明确声明您希望此 conn 使用 GET:

conn.setRequestMethod("GET");

“POST”“PUT” 根据需要。然后:

conn.connect();
InputStream is = conn.getInputStream();

并从 InputStream 中读取您的 JSON 数据。

希望这对您有所帮助!

关于json - Http GET 方法到 Https?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36659290/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com