我工作的一个Java桌面应用程序的Java 7,我的应用程序,我想(使用HTTP)与POST将数据发送到服务器。服务器在localhost上的本地机器上运行。 但是,如果我试图连接到服务器,则会返回一个连接重置(SocketTimeoutException)。我无法连接,我也试图连接到像http://www.google.de这样的网页,但它也失败了。 var body包含正确形式的POST数据。 (我也试图与禁用防火墙连接) 我的代码:URL连接HTTP POST拒绝连接
body=body.substring(0,body.length()-2);
HttpURLConnection connection = null;
try {
if (revision){ //Connect to the revision server
this.urlRevision = new URL(this.settingsRevision.getAddress());
connection = (HttpURLConnection) urlRevision.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(10000);
connection.setReadTimeout(10000);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length", String.valueOf(body.length()));
connection.connect();
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(body);
writer.flush();
this.returnedData = new BufferedReader(new InputStreamReader(connection.getInputStream()));
for(String line; (line = returnedData.readLine()) != null;){
System.out.println(line);
}
writer.close();
this.returnedData.close();
}
} catch (Exception e) {
this.exception=e;
}
请在浏览器中尝试http:// localhost。其他一些服务器可能正在运行。你的服务器是什么? – TechSpellBound
本地主机工作在我的浏览器,我收到我的本地XAMPP服务器的起始页(Apache服务器) – user1995621
与工作:System.setProperty(“java.net.preferIPv4Stack”,“真”); – user1995621