httpclient proxy 获取权限,然后下载文件
2013-01-09 14:01
351 查看
public class SimulateLogin { private HttpClient httpClient; public SimulateLogin(String loginURL, String userName, String password, String iplogin) { this.httpClient = new DefaultHttpClient(); // 构造一个POST请求 HttpPost httpPost = new HttpPost(loginURL); httpPost .setHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.3 (KHTML, like Gecko) Chrome/6.0.472.63 Safari/534.3"); //如果对方系统没做特殊限制,可不用 // 将要POST的数据封包 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username", userName)); params.add(new BasicNameValuePair("password", password)); // params.add(new BasicNameValuePair("iplogin", iplogin)); // 封包添加到Post请求 try { httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } HttpResponse response = postMethod(httpPost); } /** * 嗅探指定的GET页面 * @param url * @return String txt */ public String notifyGetPage(String url) { HttpGet get = new HttpGet(url); ResponseHandler<String> responseHandler = new BasicResponseHandler(); String txt = null; try { txt = httpClient.execute(get, responseHandler); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { get.abort(); } return txt; } /** * 嗅探指定的POST页面,,因为post方法要封装参数,因此在函数外部封装好传参 * @param post * @return String txt */ public String notifyPostPage(HttpPost post) { ResponseHandler<String> responseHandler = new BasicResponseHandler(); String txt = null; try { txt = httpClient.execute(post, responseHandler); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { post.abort(); } return txt; } // 用post方法向服务器请求 并获得响应,因为post方法要封装参数,因此在函数外部封装好传参 public HttpResponse postMethod(HttpPost post) { HttpResponse resp = null; try { resp = httpClient.execute(post); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { post.abort(); } return resp; } // 用get方法向服务器请求 并获得响应 public HttpResponse getMethod(String url) { HttpGet get = new HttpGet(url); HttpResponse resp = null; try { resp = httpClient.execute(get); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { get.abort(); } return resp; } /** *输出数据 */ public void outputData(HttpServletResponse resp,String url) throws IOException { HttpGet httpget = new HttpGet(url); HttpResponse response = this.httpClient.execute(httpget); HttpEntity entity = response.getEntity(); ServletOutputStream output = resp.getOutputStream(); if (entity != null) { InputStream instream = entity.getContent(); int len; while ((len = instream.read()) != -1) { output.write(len); } output.close(); instream.close(); } this.httpClient.getConnectionManager().shutdown(); } //第一个参数,网络连接;第二个参数,保存到本地文件的地址 public void getFile(HttpServletResponse resp,String url,boolean ispdf) { HttpGet get = new HttpGet(url); try { ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() { public byte[] handleResponse(HttpResponse response) throws ClientProtocolException, IOException { HttpEntity entity = response.getEntity(); if (entity != null) { return EntityUtils.toByteArray(entity); } else { return null; } } }; byte[] charts = this.httpClient.execute(get, handler); Header[] headers= getMethod(url).getAllHeaders(); for(Header h:headers){ if(h.getValue().indexOf("kdh")!=-1&&ispdf){ resp.setHeader(h.getName(),h.getValue().replaceAll("kdh","pdf")); }else{ resp.setHeader(h.getName(),h.getValue()); } } ServletOutputStream out = resp.getOutputStream(); out.write(charts); out.close(); } catch (Exception e) { e.printStackTrace(); } finally { this.httpClient.getConnectionManager().shutdown(); } } }
simulateLogin.getFile(response,link,ispdf);
相关文章推荐
- 使用Android内部的DownloadProvider下载文件,并获取cache权限
- AsyncHttpClient上传文件到服务器 注意6.0权限
- [导入]ASP.NET 2.0 HttpHandler实现对某种文件类型权限保护(示例代码下载)
- HttpClient4.2.2的几个常用方法,登录之后访问页面问题,下载文件
- CAtlHttpClient 下载文件保存到本地,拿去就能用简单易懂
- HttpClient 4 文件下载
- 使用WebClient HttpWebRequest 的高级应用,获取带验证的session下载权限
- HttpClient下载文件
- android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件
- Android HttpURLConnection 获取下载文件大小
- HttpClient使用post方式模拟表单提交数据到服务器并下载服务器文件
- 使用HttpHandler做文件过滤器,验证下载文件权限
- 关于inputStream.available()与HttpURLConnection.getContentLength()方法获取下载文件的大小
- android 使用AsyncHttpClient框架上传文件以及使用HttpURLConnection下载文件
- HttpClient 4.5版本实现文件上传下载以及常用post、get请求
- AsyncHttpClient+Servlet实现文件上传下载
- httpClient 上传/下载文件流
- HTTP获取批量文件打包下载-java(自)
- httpclient 文件下载
- android开发利用AsyncHttpClient下载服务器文件