gpt4 book ai didi

java - URL JSON 字符串基本身份验证

转载 作者:行者123 更新时间:2023-12-01 08:55:17 24 4
gpt4 key购买 nike

我正在使用此(工作)代码通过代理和基本身份验证连接到 URL。 URL 后面是我想要使用的 JSON 字符串 - 如何访问此 JSON 字符串?

package package;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import org.json.JSONException;

import sun.misc.BASE64Encoder;

@SuppressWarnings("restriction")
public class javahttptojson {

public static void main(String[] args) throws IOException, JSONException {

System.setProperty("https.proxyHost", "someIP");
System.setProperty("https.proxyPort", "somePort");

BASE64Encoder encoder = new BASE64Encoder();
String authString = "admin" + ":" + "adminPW";
String authStringEncoded = encoder.encode(authString.getBytes());

URL myURL = new URL("https://URL");
HttpURLConnection is = (HttpURLConnection) myURL.openConnection();

is.setRequestProperty("Authorization", "Basic " + authStringEncoded);

}
}

编辑(工作解决方案)感谢@ freedev:

StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(is.getInputStream()));
String inputLine = "";
while ((inputLine = br.readLine()) != null) {
sb.append(inputLine);
}
String result = sb.toString();

System.out.println(result);

最佳答案

对象有一个方法getInputStream()

Returns an input stream that reads from this open connection.

BufferedReader br = new BufferedReader(new InputStreamReader(is.getInputStream()));
StringBuilder sb = new StringBuilder();
String inputLine = "";
while ((inputLine = br.readLine()) != null) {
sb.append(inputLine);
}
String result = sb.toString();

关于java - URL JSON 字符串基本身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42070663/

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