我正在尝试从服务器列表中获取 apache 版本。我正在考虑解析标头,但是我无法发送 HEAD 但它可以与 GET POST 等一起使用。
这是我的代码:
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;
import javax.*;
public class SiteCycler {
public static void main(String args[]) throws IOException {
Vars vars = new Vars();
SendEmail sendemail = new SendEmail();
FileInputStream fis = new FileInputStream("D:/WRKSPC/Sender/src/sites.txt");
Scanner scanner = new Scanner(fis);
System.out.println("Reading file line by line in Java using Scanner");
int i = 0;
File outFile = new File ("C:/output.txt");
FileWriter fWriter = new FileWriter (outFile);
PrintWriter pWriter = new PrintWriter (fWriter);
while(scanner.hasNextLine()){
//System.out.println(scanner.nextLine());
String str = scanner.nextLine();
Vars.site = str;
System.out.println(Vars.site);
URL url;
HttpURLConnection connection = null;
try {
//Create connection
String urlParameters =
"server=" + URLEncoder.encode("???", "UTF-8");
url = new URL("http://www."+str+"/server-status/");
connection = (HttpURLConnection)url.openConnection();
// connection.setDoOutput(true);
connection.setRequestMethod("HEAD");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", "" +
Integer.toString(urlParameters.getBytes().length));
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches (false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream (
connection.getOutputStream ());
wr.writeBytes (urlParameters);
wr.flush ();
wr.close ();
//Get Response
InputStream is = (InputStream) connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer response = new StringBuffer();
while((line = rd.readLine()) != null) {
response.append(line);
response.append('\r');
System.out.println(line);
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(connection != null) {
connection.disconnect();
}
}
// if(true){ //this will be the check
pWriter.println (str);
// SendEmail.main(args);
// }else{
// }
// new String("SiteID"+i);
}
pWriter.close();
scanner.close();
}
}
结果在这个
java.net.ProtocolException: HTTP method HEAD doesn't support output
at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(Unknown Source)
at SiteCycler.main(SiteCycler.java:70)