gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 04:59:51 24 4
gpt4 key购买 nike

我使用httpClient3.1。我认为虽然 length buffer.length 那么 a.t​​xt 包含所有内容。但事实并非如此。在使用getMethod.getResponseBodyAsStream之前,使用getMethod.getResponseBodyAsString.everything就好了。例如

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

最佳答案

试试这个,看起来你可能没有调用一些需要的方法:

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

关于java - 使用apache-httpClient,getResponseBodyAsStream结果不一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13537702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com