使用 httpconnection 我的 java 代码出现奇怪的错误
Strange error in my java code with httpconnection
我写这段代码:
import java.net.HttpURLConnection;
import java.net.URL;
public class Main{
private static HttpURLConnection connection;
public static void main(String[] args){
final URL url = new URL (spec: "https://google.com");
connection = (HttpURLConnection) url.openConnection();
connection =url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int status = connection.getResponseCode();
system.out.println(status);
}
}
并得到这个错误:
Main.java:8: error: ')' expected
我使用 OpenJdk:
openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing)
和UBUNTU18.04
感谢您的帮助
这里有些地方不对,不是处理异常,应该是这样的:
public class Main {
private static HttpURLConnection connection;
public static void main(String[] args) throws Exception {
final URL url = new URL("https://google.com");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int status = connection.getResponseCode();
System.out.println(status);
}
}
此代码应该有效
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main{
private static HttpURLConnection connection;
public static void main(String[] args) throws IOException {
final URL url = new URL("https://google.com");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int status = connection.getResponseCode();
System.out.println(status);
}
}
我不明白什么是 'spec:' IDE 提示或 smth?制度也应该来自资本。
我写这段代码:
import java.net.HttpURLConnection;
import java.net.URL;
public class Main{
private static HttpURLConnection connection;
public static void main(String[] args){
final URL url = new URL (spec: "https://google.com");
connection = (HttpURLConnection) url.openConnection();
connection =url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int status = connection.getResponseCode();
system.out.println(status);
}
}
并得到这个错误:
Main.java:8: error: ')' expected
我使用 OpenJdk:
openjdk version "11.0.6" 2020-01-14
OpenJDK Runtime Environment (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1)
OpenJDK 64-Bit Server VM (build 11.0.6+10-post-Ubuntu-1ubuntu118.04.1, mixed mode, sharing)
和UBUNTU18.04
感谢您的帮助
这里有些地方不对,不是处理异常,应该是这样的:
public class Main {
private static HttpURLConnection connection;
public static void main(String[] args) throws Exception {
final URL url = new URL("https://google.com");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int status = connection.getResponseCode();
System.out.println(status);
}
}
此代码应该有效
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
public class Main{
private static HttpURLConnection connection;
public static void main(String[] args) throws IOException {
final URL url = new URL("https://google.com");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
int status = connection.getResponseCode();
System.out.println(status);
}
}
我不明白什么是 'spec:' IDE 提示或 smth?制度也应该来自资本。