2010-02-17 20 views
8

[Java 1.5; Eclipse Galileo]当调用getInputStream()时,HttpsURLConnection停止运作

当调用getInputStream()方法时,HttpsURLConnection似乎失速。我试过使用不同的网站无济于事(目前https://www.google.com)。我应该指出我正在使用http S

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

我是一个微调的方向是正确:)

public static void request(URL url, String query) 
{ 
try{ 

    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); 

    //connection.setReadTimeout(5000); //<-- uncommenting this line at least allows a timeout error to be thrown 

    connection.setDoInput(true); 
    connection.setDoOutput(true); 
    connection.setUseCaches(false); 
    System.setProperty("http.keepAlive", "false"); 


    connection.setRequestMethod("POST"); 


    // setting headers 
    connection.setRequestProperty("Content-length",String.valueOf (query.length())); 
    connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); //WAS application/x-www- form-urlencoded 
    connection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows 98; DigExt)"); 

    //////////////////////////////////////////////////////////////////////////////////// 
    //////////////////////////////////////////////////////////////////////////////////// 
    System.out.println("THIS line stalls" + connection.getInputStream()); 
    //////////////////////////////////////////////////////////////////////////////////// 

}catch(Exception e) { 
    System.out.println(e); 
    e.printStackTrace(); 
} 

典型错误看起来非常感谢:

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) 
+0

在黑暗中只是一个尝试,您要发送POST请求,所以我会假设,另一端(谷歌)正等着你发送一些参数。你发送的只是一些HTTP头文件。当您将POST更改为GET时会发生什么? – beny23 2010-02-17 03:39:34

+0

你/你真的/想要做什么?你的gmail标签会让我怀疑你用POP,IMAP或SMTP(或Google的其中一个自定义API)可能会更好。 – 2010-02-17 03:44:26

回答

5
connection.setDoOutput(true); 

这意味着,你必须打开,写在尝试从输入流读取数据之前关闭连接的输出流。见the docs

+0

当HTTP协议用于URL时,这也隐含地将请求方法设置为'POST',换句话说,'connection.setRequestMethod(“POST”);'是完全超级的(就像向下是HttpUrlConnection一样)。如果你打算在没有任何请求参数的情况下触发POST(这没有任何意义,但是OK;)),执行'connection.getOutputStream()。close()'。 – BalusC 2010-02-17 23:22:31

+3

这没有解决问题 – 2011-12-16 00:14:39

2

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

+0

感谢您的提示:) – geraldalewis 2010-02-18 03:02:15

3

我在Android 2.2的复制问题:从网络服务器通过无线和HTTPS URL下载时,错误是套接字“读超时”在URLConnection.getInputStream()

要修复它,使用url.openStream()为InputStream的,而不是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(); 
相关问题