httpclient proxy 获取权限,然后下载文件

  1 public class SimulateLogin {
  2         private HttpClient httpClient;
  3         
  4         public SimulateLogin(String loginURL, String userName, String password, String iplogin) {
  5             this.httpClient = new DefaultHttpClient();
  6             // 构造一个POST请求  
  7             HttpPost httpPost = new HttpPost(loginURL);
  8             httpPost
  9                     .setHeader("User-Agent",
 10                             "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"); //如果对方系统没做特殊限制,可不用  
 11             // 将要POST的数据封包    
 12             List<NameValuePair> params = new ArrayList<NameValuePair>();
 13             params.add(new BasicNameValuePair("username", userName));
 14             params.add(new BasicNameValuePair("password", password));
 15            // params.add(new BasicNameValuePair("iplogin", iplogin));
 16             // 封包添加到Post请求    
 17             try {
 18                 httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
 19             } catch (UnsupportedEncodingException e1) {
 20                 e1.printStackTrace();
 21             }
 22             HttpResponse response = postMethod(httpPost);
 23         }
 24         
 25         /** 
 26          * 嗅探指定的GET页面 
 27          * @param url 
 28          * @return String txt 
 29          */
 30         public String notifyGetPage(String url) {
 31             HttpGet get = new HttpGet(url);
 32             ResponseHandler<String> responseHandler = new BasicResponseHandler();
 33             String txt = null;
 34             try {
 35                 txt = httpClient.execute(get, responseHandler);
 36             } catch (ClientProtocolException e) {
 37                 e.printStackTrace();
 38             } catch (IOException e) {
 39                 e.printStackTrace();
 40             } finally {
 41                 get.abort();
 42             }
 43             return txt;
 44         }
 45         
 46         /** 
 47          * 嗅探指定的POST页面,,因为post方法要封装参数,因此在函数外部封装好传参   
 48          * @param post 
 49          * @return String txt 
 50          */
 51         public String notifyPostPage(HttpPost post) {
 52             ResponseHandler<String> responseHandler = new BasicResponseHandler();
 53             String txt = null;
 54             try {
 55                 txt = httpClient.execute(post, responseHandler);
 56             } catch (ClientProtocolException e) {
 57                 e.printStackTrace();
 58             } catch (IOException e) {
 59                 e.printStackTrace();
 60             } finally {
 61                 post.abort();
 62             }
 63             return txt;
 64         }
 65         
 66         // 用post方法向服务器请求 并获得响应,因为post方法要封装参数,因此在函数外部封装好传参    
 67         public HttpResponse postMethod(HttpPost post) {
 68             HttpResponse resp = null;
 69             try {
 70                 resp = httpClient.execute(post);
 71             } catch (ClientProtocolException e) {
 72                 e.printStackTrace();
 73             } catch (IOException e) {
 74                 e.printStackTrace();
 75             } finally {
 76                 post.abort();
 77             }
 78             return resp;
 79         }
 80         
 81         // 用get方法向服务器请求 并获得响应    
 82         public HttpResponse getMethod(String url) {
 83             HttpGet get = new HttpGet(url);
 84             HttpResponse resp = null;
 85             try {
 86                 resp = httpClient.execute(get);
 87             } catch (ClientProtocolException e) {
 88                 e.printStackTrace();
 89             } catch (IOException e) {
 90                 e.printStackTrace();
 91             } finally {
 92                 get.abort();
 93             }
 94             return resp;
 95         }
 96         
 97         /**
 98          *输出数据
 99          */
100         public void outputData(HttpServletResponse resp,String url) throws IOException {
101             HttpGet httpget = new HttpGet(url);
102             HttpResponse response = this.httpClient.execute(httpget);
103             HttpEntity entity = response.getEntity();
104             ServletOutputStream output = resp.getOutputStream();
105             if (entity != null) {
106                 InputStream instream = entity.getContent();
107                 int len;
108                 while ((len = instream.read()) != -1) {
109                     output.write(len);
110                 }
111                 output.close();
112                 instream.close();
113             }
114              this.httpClient.getConnectionManager().shutdown();
115             
116         }
117         
118         //第一个参数,网络连接;第二个参数,保存到本地文件的地址
119         public void getFile(HttpServletResponse resp,String url,boolean ispdf) {
120                 HttpGet get = new HttpGet(url);
121                 try {
122                     ResponseHandler<byte[]> handler = new ResponseHandler<byte[]>() {
123                         public byte[] handleResponse(HttpResponse response)
124                                 throws ClientProtocolException, IOException {
125                             HttpEntity entity = response.getEntity();
126                             if (entity != null) {
127                                 return EntityUtils.toByteArray(entity);
128                             } else {
129                                 return null;
130                             }
131                         }
132                     };
133 
134                     byte[] charts = this.httpClient.execute(get, handler);
135                     Header[] headers= getMethod(url).getAllHeaders();
136                     for(Header h:headers){
137                         if(h.getValue().indexOf("kdh")!=-1&&ispdf){
138                             resp.setHeader(h.getName(),h.getValue().replaceAll("kdh","pdf"));
139                         }else{
140                             resp.setHeader(h.getName(),h.getValue());
141                         }
142                     }
143                     ServletOutputStream out = resp.getOutputStream();
144                     out.write(charts);
145                     out.close();
146                 } catch (Exception e) {
147                     e.printStackTrace();
148                 } finally {
149                     this.httpClient.getConnectionManager().shutdown();
150                 }
151             }
152         
153     }
1 simulateLogin.getFile(response,link,ispdf);

 

你可能感兴趣的:(httpclient)