简体   繁体   English

使用apache-httpClient,getResponseBodyAsStream结果不一样

[英]use apache-httpClient, getResponseBodyAsStream result is not the same

I uses httpClient3.1. 我使用httpClient3.1。 I think while length buffer.length then a.txt contains all of content. 我认为虽然length buffer.length然后a.txt包含所有内容。 But it's no. 但这不是。 Before uses getMethod.getResponseBodyAsStream , uses getMethod.getResponseBodyAsString.everything is good. 使用getMethod.getResponseBodyAsStream之前,使用getMethod.getResponseBodyAsString.everythinggetMethod.getResponseBodyAsString.everything都很好。 For example 例如

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();
    }

Try this, looks like you might not be calling some needed method: 尝试此操作,看来您可能未调用某些所需的方法:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM