我正在尝试使用从服务器请求数据HttpsURLConnection;我目前有服务器要求用户通过提示输入用户名和密码。在 Web 浏览器中输入正确的用户名和密码后,浏览器会将用户名和密码保存为浏览器中的会话 cookie,以便您可以访问站点内的其他页面,而不会提示您输入凭据。但是对于Java客户端,不保存用户名和密码。我试图用来.disconnect()关闭连接,但我不断收到以下错误:
java.lang.IllegalStateException: Already connected
at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3053)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestProperty(HttpsURLConnectionImpl.java:316)
我的Java代码:
private static void sendPost(String _url) throws Exception {
String url = _url;
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
int responseCode = con.getResponseCode();
Auth(con);
if (responseCode == 200) {
label.setText("Sucssesfully Scanned: " + StudID.getText());
} else {
label.setText("Error, please scan again");
}
con.disconnect();
}
private static ArrayList<String> Get(String _url) throws Exception {
ArrayList<String> list = new ArrayList<>();
JsonParser parser = new JsonParser();
String url = _url;
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
Auth(con);
con.setRequestMethod("GET");
con.setRequestProperty("Accept", "application/json");
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
用户名和密码提示示例:https : //chapel-logs.herokuapp.com/chapel
largeQ
杨__羊羊
相关分类