我有下面的代碼,使使用獨立的Java程序中的HTTP調用,可有人請讓我知道哪條線/法實際上使HTTP調用URL。在HttpURLConnection的哪一種方法,使請求到指定的URL
按照下文代碼我明白我得到的連接對象,並且提供所述請求的方法類型爲「GET」,並且還給予授權細節給它。
HttpURLConnection connection = (HttpURLConnection) new URL("<Some URL String>").openConnection();
connection.setRequestMethod("GET");
BASE64Encoder enc = new BASE64Encoder();
StringBuffer userpassword = new StringBuffer().append(username).append(":").append(password);
String encodedAuthorization = enc.encode(userpassword.toString().getBytes());
connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
BufferedReader serverResponse = new BufferedReader(new InputStreamReader(connection.getInputStream()));
// Read the response
String line;
StringBuffer response = new StringBuffer();
while ((line = serverResponse.readLine()) != null) {
response.append(line);
}
謝謝!
NIVESH你好,非常感謝您的答覆。所以只需調用方法connection.getInputStream()在內部進行HTTP調用? – Chaitanya 2013-02-27 12:58:16
是的,你可以嘗試通過調用此方法,並在服務器上,你可以看到調試這個時候這行代碼執行的請求到達那裏(在服務器上) – 2013-02-27 13:00:15
感謝很多NIVESH – Chaitanya 2013-02-27 13:03:26