Java 调用getInputStream()时HttpsURLConnection暂停

Java 调用getInputStream()时HttpsURLConnection暂停,java,https,gmail,httpurlconnection,Java,Https,Gmail,Httpurlconnection,[Java 1.5;Eclipse Galileo] 调用getInputStream()方法时,HttpsURLConnection似乎会暂停。我尝试过使用不同的网站,但都没有用(目前)。我应该指出我使用的是httpS 下面的代码已经根据我从其他StackOverflow答案中学到的内容进行了修改。然而,到目前为止,我尝试过的任何解决方案都没有奏效 我将非常感谢你朝着正确的方向轻推:) 这意味着您必须在尝试读取连接的输入流之前打开、写入和关闭连接的输出流。请参阅。也不要设置内容长度标题。Jav

[Java 1.5;Eclipse Galileo]

调用getInputStream()方法时,HttpsURLConnection似乎会暂停。我尝试过使用不同的网站,但都没有用(目前)。我应该指出我使用的是httpS

下面的代码已经根据我从其他StackOverflow答案中学到的内容进行了修改。然而,到目前为止,我尝试过的任何解决方案都没有奏效

我将非常感谢你朝着正确的方向轻推:)


这意味着您必须在尝试读取连接的输入流之前打开、写入和关闭连接的输出流。请参阅。

也不要设置内容长度标题。Java将为您做到这一点。

我在Android 2.2中重现了这个问题:当通过无线和HTTPS URL从web服务器下载时,错误是URLConnection.getInputStream()上的套接字“读取超时”

要修复此问题,请对InputStream使用url.openStream()而不是connection.getInputStream()

奖励:您可以获得下载文件的长度,这样您就可以显示完成百分比指示器

代码示例:

private final int TIMEOUT_CONNECTION = 5000;//5sec
private final int TIMEOUT_SOCKET = 30000;//30sec

file = new File(strFullPath);
URL url = new URL(strURL);
URLConnection ucon = url.openConnection();

//this timeout affects how long it takes for the app to realize there's a connection problem
ucon.setReadTimeout(TIMEOUT_CONNECTION);
ucon.setConnectTimeout(TIMEOUT_SOCKET);


//IMPORTANT UPDATE:
// ucon.getInputStream() often times-out over wireless
// so, replace it with ucon.connect() and url.openStream()
ucon.connect();
iFileLength = ucon.getContentLength();//returns -1 if not set in response header

if (iFileLength != -1)
{
    Log.i(TAG, "Expected Filelength = "+String.valueOf(iFileLength)+" bytes");
}

//Define InputStreams to read from the URLConnection.
// uses 5KB download buffer
InputStream is = url.openStream();//ucon.getInputStream();
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
outStream = new FileOutputStream(file);
bFileOpen = true;
byte[] buff = new byte[5 * 1024];

//Read bytes (and store them) until there is nothing more to read(-1)
int total=0;
int len;
int percentdone;
int percentdonelast=0;
while ((len = inStream.read(buff)) != -1)
{
    //write to file
    outStream.write(buff,0,len);

    //calculate percent done
    if (iFileLength != -1)
    {
        total+=len;
        percentdone=(int)(total*100/iFileLength);

        //limit the number of messages to no more than one message every 10%
        if ( (percentdone - percentdonelast) > 10)
        {
            percentdonelast = percentdone;
            Log.i(TAG,String.valueOf(percentdone)+"%");
        }
    }
}

//clean up
outStream.flush();//THIS IS VERY IMPORTANT !
outStream.close();
bFileOpen = false;
inStream.close();

只是暗中试探一下,您正在发送POST请求,因此我假设另一端(谷歌)正在等待您发送一些参数。您发送的只是一些HTTP头。当你把帖子改成GET时会发生什么?你/真的/想做什么?你的gmail标签让我怀疑你最好使用POP、IMAP或SMTP(或谷歌的一个定制API),这也会在URL中使用HTTP协议时隐式地将请求方法设置为
POST
,换句话说,就是
连接.setRequestMethod(“POST”)
完全是超级链接(向下转换为
HttpUrlConnection
)。如果您打算在没有任何请求参数的情况下(这没有任何意义,但可以)触发POST,请执行
连接.getOutputStream().close()
)。
java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at com.sun.net.ssl.internal.ssl.InputRecord.readFully(InputRecord.java:293)
at com.sun.net.ssl.internal.ssl.InputRecord.read(InputRecord.java:331)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:782)
at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readDataRecord(SSLSocketImpl.java:739)
at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:75)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
at java.io.BufferedInputStream.read(BufferedInputStream.java:313)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:681)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:626)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:983)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234)
at https_understanding.HTTPSRequest.request(HTTPSRequest.java:60)
at https_understanding.Main.main(Main.java:17)
connection.setDoOutput(true);
private final int TIMEOUT_CONNECTION = 5000;//5sec
private final int TIMEOUT_SOCKET = 30000;//30sec

file = new File(strFullPath);
URL url = new URL(strURL);
URLConnection ucon = url.openConnection();

//this timeout affects how long it takes for the app to realize there's a connection problem
ucon.setReadTimeout(TIMEOUT_CONNECTION);
ucon.setConnectTimeout(TIMEOUT_SOCKET);


//IMPORTANT UPDATE:
// ucon.getInputStream() often times-out over wireless
// so, replace it with ucon.connect() and url.openStream()
ucon.connect();
iFileLength = ucon.getContentLength();//returns -1 if not set in response header

if (iFileLength != -1)
{
    Log.i(TAG, "Expected Filelength = "+String.valueOf(iFileLength)+" bytes");
}

//Define InputStreams to read from the URLConnection.
// uses 5KB download buffer
InputStream is = url.openStream();//ucon.getInputStream();
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
outStream = new FileOutputStream(file);
bFileOpen = true;
byte[] buff = new byte[5 * 1024];

//Read bytes (and store them) until there is nothing more to read(-1)
int total=0;
int len;
int percentdone;
int percentdonelast=0;
while ((len = inStream.read(buff)) != -1)
{
    //write to file
    outStream.write(buff,0,len);

    //calculate percent done
    if (iFileLength != -1)
    {
        total+=len;
        percentdone=(int)(total*100/iFileLength);

        //limit the number of messages to no more than one message every 10%
        if ( (percentdone - percentdonelast) > 10)
        {
            percentdonelast = percentdone;
            Log.i(TAG,String.valueOf(percentdone)+"%");
        }
    }
}

//clean up
outStream.flush();//THIS IS VERY IMPORTANT !
outStream.close();
bFileOpen = false;
inStream.close();