解决!
这给了我正确的提示: Request to j_security_check return 408 error only with right paramters
查询url / j_security_check是发送请求的位置,以便检索cookie和网站状态代码。
我正在尝试向我工作的公司的网站发送HTTP“获取”请求,以验证它已启动并正在运行。它托管在“Apache Tomcat”服务器(7.0.34)上。但是,响应代码/消息总是超时。
使用浏览器时,网站显然有效(但速度很慢)。我需要能够以编程方式获取响应代码200。
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HTTPRequest
{
URL url;
HttpURLConnection connection;
public HTTPRequest() throws IOException
{
url = new URL("http://www.google.com");
connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(15000);
connection.setReadTimeout(15000);
System.out.println("Response code: " + connection.getResponseCode());
System.out
.println("Response message: " + connection.getResponseMessage());
}
public static void main(String[] args) throws IOException
{
new HTTPRequest();
}
}
输出Google:
Response code: 200
Response message: OK
输出公司网站:
Response code: 408
Response message: Request Timeout
我使用方法setConnectTimeout()和setReadTimeout()但它们都没有效果,结果打印速度和以前一样快: HttpURLConnection setConnectTimeout() has no effect