0
我使用此(工作)代码连接到URL,使用代理和基本身份验证。 URL后面是一个JSON字符串,我想与之合作 - 如何访问此JSON字符串?URL 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);
}
}
EDIT(工作液)感谢@ 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);
您不应该用答案更新问题。 – freedev