您的位置:首页 > 理论基础 > 计算机网络

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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: