我有一个独立的java程序,它正在向服务器发送一些数据。我正在使用get方法发送请求。
try {
URL url = new URL(URL + "?prak=" + urlParameters);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection(Proxy.NO_PROXY);
connection.setRequestMethod("GET");
connection.setConnectTimeout(1000);
connection.setDoInput(true);
connection.setDoOutput(false);
connection.setUseCaches(false);
connection.connect();
int code = connection.getResponseCode();
if (code >= 200 && code < 300) {
System.out.println("success");
} else {
System.out.println("failed");
}
}catch (Exception e){
System.out.println("Exception occur" + e.getMessage() );
}
这种联系的成功率接近70%。因为我需要快速发送数据,所以我只使用1000的timout。我不想增加超时。 由于这个原因,它会抛出超时异常。请建议我建立连接的更可靠和快捷的方式。