gpt4 book ai didi

Java HttpURLConnection 头

转载 作者:可可西里 更新时间:2023-11-01 16:43:19 26 4
gpt4 key购买 nike

我正在尝试从服务器列表中获取 apache 版本。我正在考虑解析 header ,但是我无法发送 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)

最佳答案

您可能需要删除 connection.setDoOutput(true); 行。HttpUrlConnection 对象上的意外参数会产生错误。

HEAD 请求不提供OutputStream

关于Java HttpURLConnection 头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19670622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com