httpclient post HTTPS 后获取不到完整网页前台代码
只能获取3分一的,
然后 读取流 就报了 以下的错
java.io.IOException: Attempted read from closed stream. at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:133)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:121)
DefaultHttpClient httpClient = new DefaultHttpClient();
try { TrustManager easyTrustManager = new MyX509TrustManager(); SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext
.init(null, new TrustManager[] { easyTrustManager }, null);
SSLSocketFactory sf = new SSLSocketFactory(sslcontext); Scheme sch = new Scheme("https", 443, sf); httpClient.getConnectionManager().getSchemeRegistry().register(sch);
// 设置连接超时
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 60 * 1000);
// 设置读取超时
httpClient.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 60 * 1000); HttpPost httpPost = new HttpPost(url);
List<NameValuePair> formParams = new ArrayList<NameValuePair>(); // 创建参数队列
// 封装请求参数
int paramSize = Constants.StepOnePageIDsList.size();
for (int i = 0; i < paramSize; i++) {
formParams.add(new BasicNameValuePair(Constants.StepOnePageIDsList.get(i),
Constants.StepOnePageIDsValList.get(i)));
}
httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
InputStream in = entity.getContent();
byte b[] = new byte[1024]; int len = 0;
int temp=0; //所有读取的内容都使用temp接收
while((temp=in.read())!=-1){ //当没有读取完时,继续读 取 》》》》报错
b[len]=(byte)temp;
len++;
}
in.close();
System.out.println(new String(b,0,len));
只能获取3分一的,
然后 读取流 就报了 以下的错
java.io.IOException: Attempted read from closed stream. at org.apache.http.impl.io.ContentLengthInputStream.read(ContentLengthInputStream.java:133)
at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:121)
DefaultHttpClient httpClient = new DefaultHttpClient();
try { TrustManager easyTrustManager = new MyX509TrustManager(); SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext
.init(null, new TrustManager[] { easyTrustManager }, null);
SSLSocketFactory sf = new SSLSocketFactory(sslcontext); Scheme sch = new Scheme("https", 443, sf); httpClient.getConnectionManager().getSchemeRegistry().register(sch);
// 设置连接超时
httpClient.getParams().setParameter(
CoreConnectionPNames.CONNECTION_TIMEOUT, 60 * 1000);
// 设置读取超时
httpClient.getParams().setParameter(
CoreConnectionPNames.SO_TIMEOUT, 60 * 1000); HttpPost httpPost = new HttpPost(url);
List<NameValuePair> formParams = new ArrayList<NameValuePair>(); // 创建参数队列
// 封装请求参数
int paramSize = Constants.StepOnePageIDsList.size();
for (int i = 0; i < paramSize; i++) {
formParams.add(new BasicNameValuePair(Constants.StepOnePageIDsList.get(i),
Constants.StepOnePageIDsValList.get(i)));
}
httpPost.setEntity(new UrlEncodedFormEntity(formParams, "UTF-8"));
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
String content = EntityUtils.toString(entity);
InputStream in = entity.getContent();
byte b[] = new byte[1024]; int len = 0;
int temp=0; //所有读取的内容都使用temp接收
while((temp=in.read())!=-1){ //当没有读取完时,继续读 取 》》》》报错
b[len]=(byte)temp;
len++;
}
in.close();
System.out.println(new String(b,0,len));
解决方案 »
- 新手学JQuery:关于一个JQuery验证框架formVlaidator.js的问题
- JDBC查询oracle数据库的问题,在线等 高手进`````
- 用axis将接口发布为服务的问题
- jsf java.lang.NullPointerException 问题
- spring事务的实现原理,不搞明白,很不爽。
- Struts2 已经有非常好的IoC了,再集成Spring的意义何在?
- JSF和Seam的不同之处.请举例说明.
- 100分!如何学习Ioc/AOP!请帮帮忙!
- 昨天编译好的程序,今天在编译就编译不了了,抛出奇怪的异常,郁闷!
- 高手请进:webLogic中部署EJB包问题
- struts1如何动态增加删除文本框,并在后台获取数据写如数据库呢?
- @Transactional,,抛异常后不会滚??求解
我用了 BufferedInputStream 之后 就没有那个错, 但是 还是 获取不到 完整的网页前台HTMl代码 郁闷, 代码如下:
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
BufferedInputStream bufferedInput = null;
byte[] buffer = new byte[1024]; try { // 创建BufferedInputStream 对象
bufferedInput = new BufferedInputStream(
entity.getContent()); int bytesRead = 0; // 从文件中按字节读取内容,到文件尾部时read方法将返回-1
while ((bytesRead = bufferedInput.read(buffer)) != -1) { // 将读取的字节转为字符串对象
String chunk = new String(buffer, 0, bytesRead);
System.out.print(chunk);
} } catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
// 关闭 BufferedInputStream
try {
if (bufferedInput != null)
bufferedInput.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}