我工作的一個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