当前位置:编程学习 > JAVA >>

httpclient post HTTPS 后获取不到完整网页前台代码

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));  
--------------------编程问答-------------------- 换BufferedInputStream试试 --------------------编程问答--------------------
引用 1 楼 cowfishy 的回复:
换BufferedInputStream试试


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

--------------------编程问答-------------------- getResponseBodyAsString
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,