我有下面的代码,使使用独立的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