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

topbasemaster 2013-07-18 05:36:39
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));
...全文
186 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
topbasemaster 2013-07-19
  • 打赏
  • 举报
回复
引用 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();
    			}
    		}
			
S117 2013-07-19
  • 打赏
  • 举报
回复
getResponseBodyAsString
balabala_sean 2013-07-18
  • 打赏
  • 举报
回复
换BufferedInputStream试试

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧