4 回答
TA贡献1900条经验 获得超5个赞
使用HTTP时,引用HttpURLConnection而不是基类几乎总是更有用URLConnection(因为URLConnection当你URLConnection.openConnection()在HTTP URL上请求时,无论如何都是一个抽象类)。
然后你可以而不是依赖于URLConnection#setDoOutput(true)隐式地将请求方法设置为POST而不是httpURLConnection.setRequestMethod("POST")某些人可能会发现更自然(并且还允许您指定其他请求方法,如PUT,DELETE,...)。
它还提供有用的HTTP常量,因此您可以执行以下操作:
int responseCode = httpURLConnection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
TA贡献1794条经验 获得超8个赞
使用HTTP URL Hits有两个选项:GET / POST
GET请求: -
HttpURLConnection.setFollowRedirects(true); // defaults to true
String url = "https://name_of_the_url";
URL request_url = new URL(url);
HttpURLConnection http_conn = (HttpURLConnection)request_url.openConnection();
http_conn.setConnectTimeout(100000);
http_conn.setReadTimeout(100000);
http_conn.setInstanceFollowRedirects(true);
System.out.println(String.valueOf(http_conn.getResponseCode()));
POST请求: -
HttpURLConnection.setFollowRedirects(true); // defaults to true
String url = "https://name_of_the_url"
URL request_url = new URL(url);
HttpURLConnection http_conn = (HttpURLConnection)request_url.openConnection();
http_conn.setConnectTimeout(100000);
http_conn.setReadTimeout(100000);
http_conn.setInstanceFollowRedirects(true);
http_conn.setDoOutput(true);
PrintWriter out = new PrintWriter(http_conn.getOutputStream());
if (urlparameter != null) {
out.println(urlparameter);
}
out.close();
out = null;
System.out.println(String.valueOf(http_conn.getResponseCode()));
添加回答
举报