我正在使用 Java 7 开发 Java 桌面应用程序。对于我的应用程序,我想通过 POST 将数据发送到服务器(使用 HTTP)。该服务器正在本地主机上的本地计算机上运行。但是,如果我尝试连接到服务器,则会返回连接重置 (SocketTimeoutException)。我无法连接,我也尝试连接到http://www.google.de之类的网页,但它也失败了。var 正文包含正确格式的 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;
}