2012-11-24 83 views
0

我使用的是httpClient3.1。我認爲length buffer.length然後a.txt包含所有的內容。但它不是。在使用getMethod.getResponseBodyAsStream之前,使用getMethod.getResponseBodyAsString.everything是好的。例如使用apache-httpClient,getResponseBodyAsStream結果不一樣

public static void testGetResponseBodyAsStream() throws HttpException, IOException{ 
       /*test stackoverflow.com*/ 
     String uri="http://www.stackoverflow.com"; 
       /*uses httpClient 3.1*/ 
     HttpClient httpClient=new HttpClient(); 
     GetMethod getMethod=new GetMethod(uri); 
     httpClient.executeMethod(getMethod); 
       /*get result write file */ 
     FileOutputStream fileOutputStream=new FileOutputStream(new File("c:/a.txt")); 
       /*buffer is Large enough*/ 
     byte[] buffer=new byte[1024*1024]; 
     BufferedInputStream bufferedInputStream=new BufferedInputStream(getMethod.getResponseBodyAsStream()); 
       /*While length<buffer.length then a.txt contains all of 
       content.But it's no. 
       Before uses getMethod.getResponseBodyAsStream, uses       
       getMethod.getResponseBodyAsString.everything is good . */  
     int length=bufferedInputStream.read(buffer,0,buffer.length); 
     fileOutputStream.write(buffer, 0, buffer.length); 
     fileOutputStream.close(); 
    } 
+1

這不是一個很好的問題形成。請闡明一個很好形成的問題,並在標題和正文中使用它。 –

回答

0

試試這個,貌似你可能不會調用一些必要的方法:

String url = "https://login.yahoo.com"; 
String _strGetRspBody; 
String _strPostRspBody; 
int port = 443; 
HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager()); 
httpClient.getHostConfiguration().setHost(url, port, "https"); 
GetMethod authget = new GetMethod(url); 
try { 
    httpClient.executeMethod(authget); 
} catch (IOException i) { 
    logger.error(i.toString()); 
} 
InputStream d = authget.getResponseBodyAsStream(); 
+0

我的問題是 雖然長度

相關問題