HttpClient设置代理的问题

ck504323276 2014-02-13 08:14:57
我要访问的网站需要代理才能访问(SecureCRT连接个远程主机,然后IE再配下代理)

下面是代码,目前这个代码访问不需要代理的网站是OK的,访问我们的系统必须要通过代理,所以HttpClient也得加个代理。我想在getHttpClient()方法里面把代理设置好并返回个client对象,但是代理不会用,这个方法也是网上扒的,求高人点拨,感激不尽。
现在代码在response=getHttpClient().execute(..)处报connection reset异常。
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRouteParams;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;



public class HttpClientUtil {


public HttpClientUtil(){

}
public HttpClientUtil(String curDate,String platformId,String queryType,String platName){
this.curDate=curDate;
this.platformId=platformId;
this.queryType=queryType;
this.platName=platName;
this.redirectURL="http://132.228.5.26:8080/sysmgr/sysmgr/fsmp/runningreport/balanceReport.do?method=qryRunningReport&curDate="+curDate+"&platformId="+platformId+"&queryType="+queryType+"&tableType=table";
}
//登录网站用户名
private static String userName = "zhangsan";
//登录网站密码
private static String password = "1234qwer";
//健康度分析查询参数-查询日期
private String curDate;
//健康度分析查询参数-平台ID
private String platformId;
//健康度分析查询参数-类型
private String queryType;
//平台名称
private String platName;
//登录成功后重定向的网址
private String redirectURL;
//登录表单提交地址
private static String loginURL = "http://132.228.5.26:8080/sysmgr/j_security_check";
// The HttpClient is used in one session

private HttpResponse response;

//模拟登录
private boolean login() {
HttpPost httpost = new HttpPost(loginURL);
// All the parameters post to the web site

List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("origURL", redirectURL));
nvps.add(new BasicNameValuePair("j_username", userName));
nvps.add(new BasicNameValuePair("j_password", password));
try {
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = getHttpClient().execute(httpost);
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
httpost.abort();
}
return true;
}


// private String getRedirectLocation() {
// Header locationHeader = response.getFirstHeader("Location");
// if (locationHeader == null) {
// return null;
// }
// return locationHeader.getValue();
// }

//抓取页面源码
private String getText(String redirectLocation) {
HttpGet httpget = new HttpGet(redirectLocation);
// Create a response handler

ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = "";
try {
responseBody = getHttpClient().execute(httpget, responseHandler);
} catch (Exception e) {
e.printStackTrace();
responseBody = null;
} finally {
httpget.abort();

getHttpClient().getConnectionManager().shutdown();
}
return responseBody;
}

//根据抓取到的页面源码生成新的html
public void printText() throws IOException {
if (login()) {
String redirectLocation = redirectURL;
if (redirectLocation != null) {
String text=getText(redirectLocation);
//写文件
// File f=new File("/toptea/linkage/apache-tomcat-7.0.50/webapps/sysmgr/balanceFiles/"+curDate+"-"+platName+".html");
File f=new File("d://"+curDate+"-"+platName+".html");
FileWriter fw=new FileWriter(f);
fw.write(text);
fw.close();
}
}
}

public static HttpClient getHttpClient() {

DefaultHttpClient httpClient = new DefaultHttpClient();
String proxyHost = "202.102.36.241";
int proxyPort = 22;
String userName = "";
String password = "";
httpClient.getCredentialsProvider().setCredentials(
new AuthScope(proxyHost, proxyPort),
new UsernamePasswordCredentials(userName, password));
HttpHost proxy = new HttpHost(proxyHost,proxyPort);
httpClient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);

return httpClient;
}
...全文
635 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
ck504323276 2014-02-13
  • 打赏
  • 举报
回复
ck504323276 2014-02-13
  • 打赏
  • 举报
回复

50,549

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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