如以下url中的附件链接
http://202.38.193.153:8000/publishHtml/announcement_7852/announcement_7852.html
http://m.smtshopping.cn/news_detail.asp?id=956
但是直接点击可以下载,为什么用httpclient访问不到?
public boolean downloadAttachment(int num, String source, String title, String path, String visitingUrl) { RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(5000)//设置socket超时时间 .setConnectTimeout(5000)//设置connect超时时间 .setRedirectsEnabled(false)//关闭自动重定向 .build(); CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(requestConfig) .build(); HttpGet httpGet = null; try { source=encodeUrl(source); } catch (MalformedURLException e1) { e1.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } httpGet = new HttpGet(source); httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/octet-stream;q=0.9,*/*;q=0.8"); httpGet.setHeader("Accept-Charset", "GB2312,utf-8;q=0.7,*;q=0.7"); httpGet.setHeader("Accept-Encoding", "gzip, deflate"); httpGet.setHeader("Accept-Language", "zh-cn,zh;q=0.5"); httpGet.setHeader("Connection", "keep-alive"); httpGet.setHeader("referer", visitingUrl); CloseableHttpResponse httpResponse = null; try { httpResponse = httpClient.execute(httpGet); } catch (ClientProtocolException e) { System.out.println("非法url"); } catch (IOException e) { System.out.println("下载附件失败"); System.out.println(e.getMessage()); return false; } StatusLine statusLine = httpResponse.getStatusLine(); System.out.println(statusLine); if (statusLine.getStatusCode() == 200) { HttpEntity entity=httpResponse.getEntity(); String filePath; File attachFile = new File(filePath); try{ FileOutputStream outputStream = new FileOutputStream(attachFile); InputStream inputStream = entity.getContent(); byte buff[] = new byte[4096]; int counts = 0; while ((counts = m.smtshopping.cn(buff)) != -1) { System.out.println("正在下载附件..."); outputStream.write(buff, 0, counts); } System.out.println("附件下载完成"); outputStream.flush(); outputStream.close(); }catch (IOException e){ System.out.println("下载附件失败"); return false; }finally{ try { if(!(httpResponse==null)) httpResponse.close(); httpClient.close(); } catch (IOException e) { System.out.println(e.getMessage()); } } }else return false; return true; }