java 让程序经过代理上网

什么都不说了.附上代码

public class HttpUtil {
	static private RequestConfig requestConfig = null;
	static private CredentialsProvider credentialsProvider = null;
	static private boolean isProxyUser = false;
	
	static public void setRequestConfig(String addr, String port, String scheme, String user, String password) {
		if (addr.isEmpty() || port.isEmpty()) return;
		
		int nPort = Integer.parseInt(port);
		HttpHost proxy = new HttpHost(addr, nPort, scheme);
		requestConfig = RequestConfig.custom().setProxy(proxy).build();
		
		if (password.isEmpty()) return;
		
		isProxyUser = true;
		//设置代理IP端口和验证密码
		credentialsProvider = new BasicCredentialsProvider();
		credentialsProvider.setCredentials(new AuthScope(proxy.getHostName(), proxy.getPort()), 
										   new UsernamePasswordCredentials(user, password));
	}
	
	static public String doGet(String url, List param) {
		CloseableHttpClient client = null;
		try {  
			String str = EntityUtils.toString(new UrlEncodedFormEntity(param, Consts.UTF_8));
			if (isProxyUser) {
				client = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
			} else {
				client = HttpClientBuilder.create().build();
			} 
			
            //发送GET 请求
            HttpGet request = new HttpGet(url+"?"+str);  
            //设置代理服务器
            request.setConfig(requestConfig);
            
            HttpResponse response = client.execute(request);  
      
            /**请求发送成功,并得到响应**/  
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
                /**读取服务器返回过来的json字符串数据**/  
                String strResult = EntityUtils.toString(response.getEntity());  
                  
                return strResult;  
            }
            Shell shell = new Shell();
        	shell.setLocation(Display.getCurrent().getClientArea().width/2 - shell.getShell().getSize().x/2,
    				Display.getCurrent().getClientArea().height/2 - shell.getSize().y/2);
        	MessageDialog.openWarning(shell, "提示", response.getStatusLine().getReasonPhrase());
        }   
        catch (Exception e) {
        	Shell shell = new Shell();
        	shell.setLocation(Display.getCurrent().getClientArea().width/2 - shell.getShell().getSize().x/2,
    				Display.getCurrent().getClientArea().height/2 - shell.getSize().y/2);
        	MessageDialog.openWarning(shell, "提示", e.getMessage());
            e.printStackTrace();  
        }
		
		return null;
	}
	
	static public String doPost(String url, List param) {
		CloseableHttpClient client = null;
		try {
			if (isProxyUser) {
				client = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build();
			} else {
				client = HttpClientBuilder.create().build();
			} 
			//发送POST 请求
			HttpPost request = new HttpPost(url);
			//设置代理服务器
			request.setConfig(requestConfig);
			request.setEntity(new UrlEncodedFormEntity(param, "UTF-8"));
			HttpResponse response = client.execute(request);
			
			/**请求发送成功,并得到响应**/  
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {  
                /**读取服务器返回过来的json字符串数据**/  
                String strResult = EntityUtils.toString(response.getEntity());  
                  
                return strResult;  
            }
            Shell shell = new Shell();
        	shell.setLocation(Display.getCurrent().getClientArea().width/2 - shell.getShell().getSize().x/2,
    				Display.getCurrent().getClientArea().height/2 - shell.getSize().y/2);
        	MessageDialog.openWarning(shell, "提示", response.getStatusLine().getReasonPhrase());
		} catch (Exception e) {
			// TODO: handle exception
			Shell shell = new Shell();
        	shell.setLocation(Display.getCurrent().getClientArea().width/2 - shell.getShell().getSize().x/2,
    				Display.getCurrent().getClientArea().height/2 - shell.getSize().y/2);
        	MessageDialog.openWarning(shell, "提示", e.getMessage());
		}
		return null;
	}
}

你可能感兴趣的:(JAVA)