Java HttpsUrlConnection在获取请求时获取EOF异常
我正在使用https从服务器获取一些数据。问题是,当我尝试运行此代码时,它只写入空字符串(Java HttpsUrlConnection在获取请求时获取EOF异常,java,https,stream,Java,Https,Stream,我正在使用https从服务器获取一些数据。问题是,当我尝试运行此代码时,它只写入空字符串(response.toString()返回空字符串)。但当我在BufferedReader行之前添加Thread.sleep方法时,它工作得非常完美(从服务器返回完整数据)。这里的主要问题是什么 private static void httpGet(String urlStr, String[] paramName, String[] paramVal) throws Exception {
response.toString()
返回空字符串)。但当我在BufferedReader
行之前添加Thread.sleep
方法时,它工作得非常完美(从服务器返回完整数据)。这里的主要问题是什么
private static void httpGet(String urlStr, String[] paramName, String[] paramVal) throws Exception {
URL url = new URL(urlStr);
HttpsURLConnection conn
= (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setRequestProperty(COOKIE_HEADER, param);
if (conn != null) {
//Thread.sleep(1000);
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
System.out.println(response.toString());
}
}
嗯,你的代码对我有用;-)即使没有Thread.sleep(见下文),我也从“Oracle”中提取数据。另外,请不要忘记关闭输入流
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL url = new URL("https://www.oracle.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
//conn.setRequestProperty("COOKIE_HEADER", param);
if (conn != null) {
// Thread.sleep(1000);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
System.out.println(response.toString());
}
}
}好吧,你的代码对我有用;-)即使没有Thread.sleep(见下文),我也从“Oracle”中提取数据。另外,请不要忘记关闭输入流
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class URLConnectionReader {
public static void main(String[] args) throws Exception {
URL url = new URL("https://www.oracle.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
//conn.setRequestProperty("COOKIE_HEADER", param);
if (conn != null) {
// Thread.sleep(1000);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
System.out.println(response.toString());
}
}
}
HttpsUrlConnection
在获取请求时获取EOF异常
您没有执行GET请求。您正在写一篇文章,但没有提供任何文章数据
这里还有许多其他问题:
conn.setRequestMethod("GET");
这是默认设置。不要写无意义的代码
conn.setDoOutput(true);
if (conn != null) {
//Thread.sleep(1000);
这将请求方法更改为POST。如果您不想发布帖子,请删除此行
conn.setDoInput(true);
这是默认设置。不要写无意义的代码
conn.setDoOutput(true);
if (conn != null) {
//Thread.sleep(1000);
conn
此时不可能为空。不要写无意义的代码
conn.setDoOutput(true);
if (conn != null) {
//Thread.sleep(1000);
在网络代码中睡觉简直是浪费时间。不要使用它们,也不要认为它们解决了网络问题。他们没有。它们可能会掩盖一点,但潜在的问题仍然存在
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
在这里,您试图从一篇文章中读取响应,但从未编写过任何文章数据。服务器可能会对此表示反对,可能会提前终止连接,这可能会导致EOFEException
,但不会出现在上面的readLine()
代码中。由于您没有提供堆栈跟踪,因此无法进一步进行注释。但是,除非存在服务器端问题,否则在修复上述错误后,代码应该可以正常工作
HttpsUrlConnection
在获取请求时获取EOF异常
您没有执行GET请求。您正在写一篇文章,但没有提供任何文章数据
这里还有许多其他问题:
conn.setRequestMethod("GET");
这是默认设置。不要写无意义的代码
conn.setDoOutput(true);
if (conn != null) {
//Thread.sleep(1000);
这将请求方法更改为POST。如果您不想发布帖子,请删除此行
conn.setDoInput(true);
这是默认设置。不要写无意义的代码
conn.setDoOutput(true);
if (conn != null) {
//Thread.sleep(1000);
conn
此时不可能为空。不要写无意义的代码
conn.setDoOutput(true);
if (conn != null) {
//Thread.sleep(1000);
在网络代码中睡觉简直是浪费时间。不要使用它们,也不要认为它们解决了网络问题。他们没有。它们可能会掩盖一点,但潜在的问题仍然存在
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
在这里,您试图从一篇文章中读取响应,但从未编写过任何文章数据。服务器可能会对此表示反对,可能会提前终止连接,这可能会导致
EOFEException
,但不会出现在上面的readLine()
代码中。由于您没有提供堆栈跟踪,因此无法进一步进行注释。但是,除非存在服务器端问题,否则在修复上述错误后,您的代码应该可以正常工作。您的服务器代码是什么?这个客户端代码看起来非常好,所以请发布你的服务器代码,也许问题就在那里。还请将此代码更新为完整的方法,因为这只是一个片段,代码的信封中可能有东西。@krzysztofcichochiki服务器代码不是我写的。我正在从Nessus应用程序中获取一些xml数据。(). 当我发布登录参数时,Nessus返回一些会话令牌。这部分工作没有任何问题,但当我试图从Nessus获取一些xml信息时,如果没有Thread.sleep,它就无法工作。请尝试删除行“conn.setDoOutput(true)”;如果这样做没有效果,请尝试使用“conn.getOutputStream().flush();”而不是“Thread.sleep()”,这可能会对您有所帮助。您的服务器代码是什么?这个客户端代码看起来非常好,所以请发布你的服务器代码,也许问题就在那里。还请将此代码更新为完整的方法,因为这只是一个片段,代码的信封中可能有东西。@krzysztofcichochiki服务器代码不是我写的。我正在从Nessus应用程序中获取一些xml数据。(). 当我发布登录参数时,Nessus返回一些会话令牌。这部分工作没有任何问题,但当我尝试从Nessus获取一些xml信息时,如果没有Thread.sleep,它就无法工作。请尝试删除行“conn.setDoOutput(true)”;如果这不起作用,请尝试使用“conn.getOutputStream().flush();”而不是“Thread.sleep()”,这可能会对您有所帮助。此代码不会引发任何异常,但response.toString方法返回空字符串。(当我尝试使用conn.getInputstream()时,我遇到文件结尾错误)当我添加线程时。sleep方法response.toString方法从服务器返回数据。我不明白为什么我需要添加线程。睡眠?Thread.sleep在这里做什么?您不需要添加Thread.sleep()
。我已经说过了。您需要修复我上面列举的几个bug。您仍然没有提供请求的堆栈跟踪。如果代码没有引发任何异常,为什么您的问题会提到“EOF异常”?此代码没有引发任何异常,但response.toString方法返回空字符串。(当我尝试使用conn.getInputstream()时,我遇到文件结尾错误)当我添加thread.sleep方法response.toString方法时,它会从服务器返回数据。我不明白为什么我需要添加线程。睡眠?Thread.sleep在这里做什么?您不需要添加Thread.sleep()
。我已经说过了。您需要修复我上面列举的几个bug。您仍然没有提供请求的堆栈跟踪。如果代码没有引发任何异常,为什么您的问题会提到“EOF异常”?