本篇文章给大家谈谈带有SFTP的Paramiko的SSHClient,以及sftpssh的知识点,同时本文还将给你拓展org.apache.commons.httpclient.params.Http
本篇文章给大家谈谈带有SFTP的Paramiko的SSHClient,以及sftp ssh的知识点,同时本文还将给你拓展org.apache.commons.httpclient.params.HttpClientParams的实例源码、org.apache.http.client.params.AllClientPNames的实例源码、org.apache.http.client.params.ClientParamBean的实例源码、org.apache.http.client.params.ClientPNames的实例源码等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- 带有SFTP的Paramiko的SSHClient(sftp ssh)
- org.apache.commons.httpclient.params.HttpClientParams的实例源码
- org.apache.http.client.params.AllClientPNames的实例源码
- org.apache.http.client.params.ClientParamBean的实例源码
- org.apache.http.client.params.ClientPNames的实例源码
带有SFTP的Paramiko的SSHClient(sftp ssh)
如何SSHClient
在远程服务器上进行SFTP传输?我有一个本地主机和两个远程主机。远程主机是备份服务器和Web服务器。我需要在备份服务器上找到必要的备份文件,然后通过SFTP将其放在Web服务器上。如何使Paramiko的SFTP传输与Paramiko的SFTP一起工作SSHClient
?
答案1
小编典典paramiko.SFTPClient
用法示例:
import paramikoparamiko.util.log_to_file("paramiko.log")# Open a transporthost,port = "example.com",22transport = paramiko.Transport((host,port))# Auth username,password = "bar","foo"transport.connect(None,username,password)# Go! sftp = paramiko.SFTPClient.from_transport(transport)# Downloadfilepath = "/etc/passwd"localpath = "/home/remotepasswd"sftp.get(filepath,localpath)# Uploadfilepath = "/home/foo.jpg"localpath = "/home/pony.jpg"sftp.put(localpath,filepath)# Closeif sftp: sftp.close()if transport: transport.close()
org.apache.commons.httpclient.params.HttpClientParams的实例源码
protected HttpClient constructHttpClient() { MultiThreadedhttpconnectionManager connectionManager = new MultiThreadedhttpconnectionManager(); HttpClient httpClient = new HttpClient(connectionManager); HttpClientParams params = httpClient.getParams(); params.setBooleanParameter(httpconnectionParams.TCP_NODELAY,true); params.setBooleanParameter(httpconnectionParams.STALE_CONNECTION_CHECK,true); if (socketTimeout != null) { params.setSoTimeout(socketTimeout); } httpconnectionManagerParams connectionManagerParams = httpClient.gethttpconnectionManager().getParams(); connectionManagerParams.setMaxTotalConnections(maxTotalConnections); connectionManagerParams.setDefaultMaxConnectionsPerHost(maxHostConnections); connectionManagerParams.setConnectionTimeout(connectionTimeout); return httpClient; }
/** * Constructor. * * @param MetadataURL the URL to fetch the Metadata * @param requestTimeout the time,in milliseconds,to wait for the Metadata server to respond * * @throws MetadataProviderException thrown if the URL is not a valid URL or the Metadata can not be retrieved from * the URL */ @Deprecated public HTTPMetadataProvider(String MetadataURL,int requestTimeout) throws MetadataProviderException { super(); try { MetadataURI = new URI(MetadataURL); } catch (URISyntaxException e) { throw new MetadataProviderException("Illegal URL Syntax",e); } HttpClientParams clientParams = new HttpClientParams(); clientParams.setSoTimeout(requestTimeout); httpClient = new HttpClient(clientParams); httpClient.gethttpconnectionManager().getParams().setConnectionTimeout(requestTimeout); authScope = new AuthScope(MetadataURI.getHost(),MetadataURI.getPort()); }
/** * Creates an instance of HttpClient using the given * {@link HttpClientParams parameter set}. * * @param params The {@link HttpClientParams parameters} to use. * * @see HttpClientParams * * @since 3.0 */ public HttpClient(HttpClientParams params) { super(); if (params == null) { throw new IllegalArgumentException("Params may not be null"); } this.params = params; this.httpconnectionManager = null; Class clazz = params.getConnectionManagerClass(); if (clazz != null) { try { this.httpconnectionManager = (httpconnectionManager) clazz.newInstance(); } catch (Exception e) { LOG.warn("Error instantiating connection manager class,defaulting to" + " SimplehttpconnectionManager",e); } } if (this.httpconnectionManager == null) { this.httpconnectionManager = new SimplehttpconnectionManager(); } if (this.httpconnectionManager != null) { this.httpconnectionManager.getParams().setDefaults(this.params); } }
public void testRelativeRedirect() throws IOException { String host = this.server.getLocalAddress(); int port = this.server.getLocalPort(); this.server.setHttpService(new RelativeRedirectService()); this.client.getParams().setBooleanParameter( HttpClientParams.REJECT_RELATIVE_REDIRECT,false); getmethod httpget = new getmethod("/oldlocation/"); httpget.setFollowRedirects(true); try { this.client.executeMethod(httpget); assertEquals("/relativelocation/",httpget.getPath()); assertEquals(host,httpget.getURI().getHost()); assertEquals(port,httpget.getURI().getPort()); assertEquals(new URI("http://" + host + ":" + port + "/relativelocation/",false),httpget.getURI()); } finally { httpget.releaseConnection(); } }
public void testRejectRelativeRedirect() throws IOException { String host = this.server.getLocalAddress(); int port = this.server.getLocalPort(); this.server.setHttpService(new RelativeRedirectService()); this.client.getParams().setBooleanParameter( HttpClientParams.REJECT_RELATIVE_REDIRECT,true); getmethod httpget = new getmethod("/oldlocation/"); httpget.setFollowRedirects(true); try { this.client.executeMethod(httpget); assertEquals(HttpStatus.SC_MOVED_TEMPORARILY,httpget.getStatusCode()); assertEquals("/oldlocation/",httpget.getPath()); assertEquals(new URI("/oldlocation/",httpget.getURI()); } finally { httpget.releaseConnection(); } }
private HttpClient getHttpClient() { if (s_client == null) { final MultiThreadedhttpconnectionManager mgr = new MultiThreadedhttpconnectionManager(); mgr.getParams().setDefaultMaxConnectionsPerHost(4); // Todo make it configurable mgr.getParams().setMaxTotalConnections(1000); s_client = new HttpClient(mgr); final HttpClientParams clientParams = new HttpClientParams(); clientParams.setSoTimeout(ClusterServiceAdapter.ClusterMessageTimeOut.value() * 1000); s_client.setParams(clientParams); } return s_client; }
private synchronized void init() { client = new HttpClient(new MultiThreadedhttpconnectionManager()); HttpClientParams params = client.getParams(); if (encode != null && !encode.trim().equals("")) { params.setParameter("http.protocol.content-charset",encode); params.setContentCharset(encode); } if (timeout > 0) { params.setSoTimeout(timeout); } if (null != proxy) { HostConfiguration hc = new HostConfiguration(); hc.setProxy(proxy.getHost(),proxy.getPort()); client.setHostConfiguration(hc); client.getState().setProxyCredentials(AuthScope.ANY,new UsernamePasswordCredentials(proxy.getUser(),proxy.getpassword())); } initialized = true; }
/** * * @throws Exception . */ private void init() throws Exception { httpClientManager = new MultiThreadedhttpconnectionManager(); httpconnectionManagerParams params = httpClientManager.getParams(); params.setStaleCheckingEnabled(true); params.setMaxTotalConnections(1000); params.setDefaultMaxConnectionsPerHost(500); params.setConnectionTimeout(2000); params.setSoTimeout(3000); /** 设置从连接池中获取连接超时。*/ HttpClientParams clientParams = new HttpClientParams(); clientParams.setConnectionManagerTimeout(1000); httpClient = new HttpClient(clientParams,httpClientManager); }
public HttpClient(int maxConPerHost,int conTimeOutMs,int soTimeOutMs,int maxSize) { connectionManager = new MultiThreadedhttpconnectionManager(); httpconnectionManagerParams params = connectionManager.getParams(); params.setDefaultMaxConnectionsPerHost(maxConPerHost); params.setConnectionTimeout(conTimeOutMs); params.setSoTimeout(soTimeOutMs); HttpClientParams clientParams = new HttpClientParams(); // 忽略cookie 避免 Cookie rejected 警告 clientParams.setCookiePolicy(CookiePolicy.IGnorE_COOKIES); client = new org.apache.commons.httpclient.HttpClient(clientParams,connectionManager); Protocol myhttps = new Protocol("https",new MySSLSocketFactory(),443); Protocol.registerProtocol("https",myhttps); this.maxSize = maxSize; // 支持proxy if (proxyHost != null && !proxyHost.equals("")) { client.getHostConfiguration().setProxy(proxyHost,proxyPort); client.getParams().setAuthenticationPreemptive(true); if (proxyAuthUser != null && !proxyAuthUser.equals("")) { client.getState().setProxyCredentials(AuthScope.ANY,new UsernamePasswordCredentials(proxyAuthUser,proxyAuthPassword)); log("Proxy AuthUser: " + proxyAuthUser); log("Proxy AuthPassword: " + proxyAuthPassword); } } }
protected void configureHttpClient() throws IOException,GeneralSecurityException { httpClient.getParams().setAuthenticationPreemptive(isAuthenticationPreemptive()); initCredentials(); initSocketFactory(); initProtocolIfNeeded(); if (httpconnectionManager != null) { httpClient.sethttpconnectionManager(httpconnectionManager); } List<Header> headers = getDefaultHeaders(); httpClient.getHostConfiguration().getParams().setParameter(HostParams.DEFAULT_HEADERS,headers); httpClient.getParams().setParameter(HttpClientParams.USER_AGENT,"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.19 (KHTML,like Gecko) Ubuntu/11.04 Chromium/18.0.1025.151 Chrome/18.0.1025.151 Safari/535.19"); httpClient.getParams().setParameter(HttpClientParams.HTTP_CONTENT_CHARSET,"UTF-8"); httpClient.getParams().setCookiePolicy(CookiePolicy.broWSER_COMPATIBILITY); httpClient.getParams().setConnectionManagerTimeout(connectionManagerTimeout); httpClient.getParams().setSoTimeout(soTimeout); if (connectionTimeout >= 0) { httpClient.gethttpconnectionManager().getParams().setConnectionTimeout(connectionTimeout); } }
private void init() { initList(m_ipList,ipFilePath); initList(m_uaList,uaFilePath); initList(m_itemList,itmFilePath); initList(m_tsList,tsFilePath); initList(m_siteList,siteFilePath); initList(m_dsList,dsFilePath); initGUIDList(); String finalURL = ""; if (batchMode) { finalURL = BATCH_URL; } else { finalURL = URL; } m_payload = readFromresource(); HttpClientParams clientParams = new HttpClientParams(); clientParams.setSoTimeout(60000); m_client = new HttpClient(clientParams); m_method = new PostMethod(NODE + finalURL + EVENTTYPE); m_method.setRequestHeader("Connection","Keep-Alive"); m_method.setRequestHeader("Accept-Charset","UTF-8"); }
public HttpClient(int maxConPerHost,int maxSize) { // MultiThreadedhttpconnectionManager connectionManager = new MultiThreadedhttpconnectionManager(); SimplehttpconnectionManager connectionManager = new SimplehttpconnectionManager(true); httpconnectionManagerParams params = connectionManager.getParams(); params.setDefaultMaxConnectionsPerHost(maxConPerHost); params.setConnectionTimeout(conTimeOutMs); params.setSoTimeout(soTimeOutMs); HttpClientParams clientParams = new HttpClientParams(); clientParams.setCookiePolicy(CookiePolicy.IGnorE_COOKIES); client = new org.apache.commons.httpclient.HttpClient(clientParams,connectionManager); Protocol myhttps = new Protocol("https",443); Protocol.registerProtocol("https",myhttps); }
/** * Constructor. * * @param MetadataURL the URL to fetch the Metadata * @param requestTimeout the time,to wait for the Metadata server to respond * * @throws MetadataProviderException thrown if the URL is not a valid URL or the Metadata can not be retrieved from * the URL */ public HTTPMetadataProvider(String MetadataURL,int requestTimeout) throws MetadataProviderException { super(); try { MetadataURI = new URI(MetadataURL); maintainExpiredMetadata = true; HttpClientParams clientParams = new HttpClientParams(); clientParams.setSoTimeout(requestTimeout); httpClient = new HttpClient(clientParams); authScope = new AuthScope(MetadataURI.getHost(),MetadataURI.getPort()); // 24 hours maxCacheDuration = 60 * 60 * 24; } catch (URISyntaxException e) { throw new MetadataProviderException("Illegal URL Syntax",e); } }
@Override public void setUp() throws Exception { KeyStoreParameters keyStoreParameters = new KeyStoreParameters("SSL Key Store","JCEKS",null,"ssl-keystore-passwords.properties","ssl.keystore"); KeyStoreParameters trustStoreParameters = new KeyStoreParameters("SSL Trust Store","ssl-truststore-passwords.properties","ssl.truststore"); SSLEncryptionParameters sslEncryptionParameters = new SSLEncryptionParameters(keyStoreParameters,trustStoreParameters); ClasspathKeyResourceLoader keyResourceLoader = new ClasspathKeyResourceLoader(); HttpClientFactory httpClientFactory = new HttpClientFactory(SecureCommsType.getType("https"),sslEncryptionParameters,keyResourceLoader,"localhost",8080,8443,40,0); StringBuilder sb = new StringBuilder(); sb.append("/solr/admin/cores"); this.baseUrl = sb.toString(); httpClient = httpClientFactory.getHttpClient(); HttpClientParams params = httpClient.getParams(); params.setBooleanParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION,true); httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST,AuthScope.ANY_PORT),new UsernamePasswordCredentials("admin","admin")); }
public HttpClient getInstanceHttpClient() { HttpClient client = new HttpClient(); HttpClientParams params = client.getParams(); params.setConnectionManagerTimeout(DEFAULT_TIMEOUT); params.setSoTimeout(DEFAULT_TIMEOUT); if (Jenkins.getInstance() == null) return client; ProxyConfiguration proxy = getInstance().proxy; if (proxy == null) return client; logger.log(Level.FINE,"Jenkins proxy: {0}:{1}",new Object[]{ proxy.name,proxy.port }); client.getHostConfiguration().setProxy(proxy.name,proxy.port); String username = proxy.getUserName(); String password = proxy.getpassword(); // Consider it to be passed if username specified. Sufficient? if (username != null && !"".equals(username.trim())) { logger.log(Level.FINE,"Using proxy authentication (user={0})",username); client.getState().setProxyCredentials(AuthScope.ANY,new UsernamePasswordCredentials(username,password)); } return client; }
/** * Define client configuration * @param httpClient instance to configure */ private static void setConfiguration(final HttpClient httpClient) { final httpconnectionManagerParams httpParams = httpClient.gethttpconnectionManager().getParams(); // define connect timeout: httpParams.setConnectionTimeout(NetworkSettings.DEFAULT_CONNECT_TIMEOUT); // define read timeout: httpParams.setSoTimeout(NetworkSettings.DEFAULT_SOCKET_READ_TIMEOUT); // define connection parameters: httpParams.setMaxTotalConnections(NetworkSettings.DEFAULT_MAX_TOTAL_CONNECTIONS); httpParams.setDefaultMaxConnectionsPerHost(NetworkSettings.DEFAULT_MAX_HOST_CONNECTIONS); // set content-encoding to UTF-8 instead of default ISO-8859 final HttpClientParams httpClientParams = httpClient.getParams(); // define timeout value for allocation of connections from the pool httpClientParams.setConnectionManagerTimeout(NetworkSettings.DEFAULT_CONNECT_TIMEOUT); // encoding to UTF-8 httpClientParams.setParameter(HttpClientParams.HTTP_CONTENT_CHARSET,"UTF-8"); // avoid retries (3 by default): httpClientParams.setParameter(HttpMethodParams.RETRY_HANDLER,_httpnoretryHandler); // Customize the user agent: httpClientParams.setParameter(HttpMethodParams.USER_AGENT,System.getProperty(NetworkSettings.PROPERTY_USER_AGENT)); }
private HttpClient getHttpClient() { if (s_client == null) { final MultiThreadedhttpconnectionManager mgr = new MultiThreadedhttpconnectionManager(); mgr.getParams().setDefaultMaxConnectionsPerHost(4); // Todo make it configurable mgr.getParams().setMaxTotalConnections(1000); s_client = new HttpClient(mgr); final HttpClientParams clientParams = new HttpClientParams(); clientParams.setSoTimeout(ClusterServiceAdapter.ClusterMessageTimeOut.value() * 1000); s_client.setParams(clientParams); } return s_client; }
@Before public void setUp() { HttpClientParams hmp = mock(HttpClientParams.class); when(_client.getParams()).thenReturn(hmp); _api = new BigSwitchBcfApi(){ @Override protected HttpClient createHttpClient() { return _client; } @Override protected HttpMethod createMethod(String type,String uri,int port) { return _method; } }; _api.setControllerAddress("10.10.0.10"); _api.setControllerUsername("myname"); _api.setControllerPassword("mypassword"); }
/** * Creates an instance of HttpClient using the given * {@link HttpClientParams parameter set}. * * @param params The {@link HttpClientParams parameters} to use. * * @see HttpClientParams * * @since 3.0 */ public HttpClient(HttpClientParams params) { super(); if (params == null) { throw new IllegalArgumentException("Params may not be null"); } this.params = params; this.httpconnectionManager = null; Class clazz = params.getConnectionManagerClass(); if (clazz != null) { try { this.httpconnectionManager = (httpconnectionManager) clazz.newInstance(); } catch (Exception e) { LOG.warn("Error instantiating connection manager class,e); } } if (this.httpconnectionManager == null) { this.httpconnectionManager = new SimplehttpconnectionManager(); } if (this.httpconnectionManager != null) { this.httpconnectionManager.getParams().setDefaults(this.params); } }
public void testRelativeRedirect() throws IOException { String host = this.server.getLocalAddress(); int port = this.server.getLocalPort(); this.server.setHttpService(new RelativeRedirectService()); this.client.getParams().setBooleanParameter( HttpClientParams.REJECT_RELATIVE_REDIRECT,httpget.getURI()); } finally { httpget.releaseConnection(); } }
public void testRejectRelativeRedirect() throws IOException { String host = this.server.getLocalAddress(); int port = this.server.getLocalPort(); this.server.setHttpService(new RelativeRedirectService()); this.client.getParams().setBooleanParameter( HttpClientParams.REJECT_RELATIVE_REDIRECT,httpget.getURI()); } finally { httpget.releaseConnection(); } }
protected void initializeHttpClientParams() { synchronized (httpinvokerConnector.class) { if (! this.httpClientinitialized) { this.httpClientParams = new HttpClientParams(); configureDefaultHttpClientParams(this.httpClientParams); Properties configProps = ConfigContext.getCurrentContextConfig().getProperties(); for (Iterator<Object> iterator = configProps.keySet().iterator(); iterator.hasNext();) { String paramName = (String) iterator.next(); if (paramName.startsWith("http.")) { HttpClientHelper.setParameter(this.httpClientParams,paramName,(String) configProps.get(paramName)); } } runIdleConnectionTimeout(); this.httpClientinitialized = true; } } }
protected void configureDefaultHttpClientParams(HttpParams params) { params.setParameter(HttpClientParams.CONNECTION_MANAGER_CLASS,MultiThreadedhttpconnectionManager.class); params.setParameter(HttpMethodParams.COOKIE_POLICY,CookiePolicy.RFC_2109); params.setLongParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT,10000); Map<HostConfiguration,Integer> maxHostConnectionsMap = new HashMap<HostConfiguration,Integer>(); maxHostConnectionsMap.put(HostConfiguration.ANY_HOST_CONfigURATION,new Integer(20)); params.setParameter(httpconnectionManagerParams.MAX_HOST_CONNECTIONS,maxHostConnectionsMap); params.setIntParameter(httpconnectionManagerParams.MAX_TOTAL_CONNECTIONS,20); params.setIntParameter(httpconnectionParams.CONNECTION_TIMEOUT,10000); params.setIntParameter(httpconnectionParams.so_TIMEOUT,2*60*1000); boolean retrySocketException = new Boolean(ConfigContext.getCurrentContextConfig().getProperty(RETRY_SOCKET_EXCEPTION_PROPERTY)); if (retrySocketException) { LOG.info("Installing custom HTTP retry handler to retry requests in face of SocketExceptions"); params.setParameter(HttpMethodParams.RETRY_HANDLER,new CustomHttpMethodRetryHandler()); } }
/** * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) * * Called upon initialization,Will create the ConfigParser and get the * RuleChain back. Will also configure the httpclient. */ public void init(FilterConfig filterConfig) throws servletexception { log = LogFactory.getLog(ProxyFilter.class); AllowedMethodHandler.setAllowedMethods("OPTIONS,GET,HEAD,POST,PUT,DELETE,TRACE"); httpClient = new HttpClient(new MultiThreadedhttpconnectionManager()); httpClient.getParams().setBooleanParameter(HttpClientParams.USE_EXPECT_CONTINUE,false); httpClient.getParams().setCookiePolicy(CookiePolicy.IGnorE_COOKIES); String data = filterConfig.getinitParameter("dataUrl"); if (data == null) { serverChain = null; } else { try { File dataFile = new File(filterConfig.getServletContext().getRealPath(data)); ConfigParser parser = new ConfigParser(dataFile); serverChain = parser.getServerChain(); } catch (Exception e) { throw new servletexception(e); } } }
/** * Called upon initialization,Will create the ConfigParser and get the * RuleChain back. Will also configure the httpclient. * * @see javax.servlet.Filter#init(javax.servlet.FilterConfig) */ public void init(FilterConfig filterConfig) throws servletexception { log = LogFactory.getLog(ProxyFilter.class); AllowedMethodHandler.setAllowedMethods("OPTIONS,false); httpClient.getParams().setCookiePolicy(CookiePolicy.IGnorE_COOKIES); String data = filterConfig.getinitParameter("dataUrl"); if (data == null) { serverChain = null; } else { try { File dataFile = new File(filterConfig.getServletContext().getRealPath(data)); ConfigParser parser = new ConfigParser(dataFile); serverChain = parser.getServerChain(); } catch (Exception e) { throw new servletexception(e); } } }
public void init() { ParameterCheck.mandatory("baseUrl",baseUrl); StringBuilder sb = new StringBuilder(); sb.append(baseUrl + "/admin/cores"); this.adminUrl = sb.toString(); httpClient = httpClientFactory.getHttpClient(); HttpClientParams params = httpClient.getParams(); params.setBooleanParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION,"admin")); }
@Override protected void setUp() throws Exception { super.setUp(); if (remoteServer != null) { httpClient = new HttpClient(); httpClient.getParams().setBooleanParameter(HttpClientParams.PREEMPTIVE_AUTHENTICATION,true); if (remoteServer.username != null) { httpClient.getState().setCredentials(new AuthScope(AuthScope.ANY_HOST,new UsernamePasswordCredentials(remoteServer.username,remoteServer.password)); } } }
/** * Basic constructor sets the listener to notify when * servers goes down/up. Also sets the polling time * which decides how long we wait between doing checks. * * @param listener The listener * @param pollingTime The time we wait between checks,in milliseconds */ public ServerStatusChecker(ServerStatusListener listener,long pollingTime) { this.listener = listener; this.pollingTime = Math.max(30*1000,pollingTime); setPriority(Thread.norM_PRIORITY-1); setDaemon(true); online = new LinkedList(); offline = new LinkedList(); httpClient = new HttpClient(); httpClient.getParams().setBooleanParameter(HttpClientParams.USE_EXPECT_CONTINUE,false); httpClient.getParams().setCookiePolicy(CookiePolicy.IGnorE_COOKIES); }
@Override public void initClient(final ConnectionSettings settings) { if (settings == null) throw new NullPointerException("Internet connection settings cannot be null"); this.settings = settings; final HttpClientParams clientParams = client.getParams(); clientParams.setCookiePolicy(CookiePolicy.broWSER_COMPATIBILITY); clientParams.setParameter(HttpMethodParams.SINGLE_COOKIE_HEADER,true); clientParams.setSoTimeout(timeout); clientParams.setConnectionManagerTimeout(timeout); clientParams.setHttpElementCharset("UTF-8"); this.client.sethttpconnectionManager(new SimplehttpconnectionManager(/*true*/)); this.client.gethttpconnectionManager().getParams().setConnectionTimeout(timeout); HttpState initialState = new HttpState(); HostConfiguration configuration = new HostConfiguration(); if (settings.getProxyType() == Proxy.Type.soCKS) { // Proxy stuff happens here configuration = new HostConfigurationWithStickyProtocol(); Proxy proxy = new Proxy(settings.getProxyType(),// create custom Socket factory new InetSocketAddress(settings.getProxyURL(),settings.getProxyPort()) ); protocol = new Protocol("http",new ProxySocketFactory(proxy),80); } else if (settings.getProxyType() == Proxy.Type.HTTP) { // we use build in HTTP Proxy support configuration.setProxy(settings.getProxyURL(),settings.getProxyPort()); if (settings.getUserName() != null) initialState.setProxyCredentials(AuthScope.ANY,new NTCredentials(settings.getUserName(),settings.getpassword(),"","")); } client.setHostConfiguration(configuration); clientParams.setBooleanParameter(HttpClientParams.ALLOW_CIRculaR_REDIRECTS,true); client.setState(initialState); }
public Stubs(ConfigurationContext ctx,String bbUrl) throws AxisFault { this.ctx = ctx; this.bbUrl = bbUrl; /* * Must use deprecated class of setting up security because the SOAP * response doesn't include a security header. Using the deprecated * OutflowConfiguration class we can specify that the security * header is only for the outgoing SOAP message. */ ofc = new OutflowConfiguration(); ofc.setActionItems("Usernametoken Timestamp"); ofc.setUser("session"); ofc.setPasswordType("PasswordText"); final MultiThreadedhttpconnectionManager conMan = new MultiThreadedhttpconnectionManager(); final httpconnectionManagerParams params = new httpconnectionManagerParams(); params.setMaxTotalConnections(1000); params.setDefaultMaxConnectionsPerHost(100); params.setSoTimeout(60000); params.setConnectionTimeout(30000); conMan.setParams(params); httpClient = new HttpClient(conMan); final HttpClientParams clientParams = httpClient.getParams(); clientParams.setAuthenticationPreemptive(false); clientParams.setCookiePolicy(CookiePolicy.broWSER_COMPATIBILITY); ctx.setProperty(HTTPConstants.CACHED_HTTP_CLIENT,httpClient); contextWebservice = new ContextWsstub(ctx,PathUtils.filePath(bbUrl,"webapps/ws/services/Context.WS")); initStub(contextWebservice); }
/** * Builds an HTTP client with the given settings. Settings are NOT reset to their default values after a client has * been created. * * @return the created client. */ public HttpClient buildClient() { if (httpsProtocolSocketFactory != null) { Protocol.registerProtocol("https",new Protocol("https",httpsProtocolSocketFactory,443)); } HttpClientParams clientParams = new HttpClientParams(); clientParams.setAuthenticationPreemptive(isPreemptiveAuthentication()); clientParams.setContentCharset(getContentCharSet()); clientParams.setParameter(HttpClientParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler( connectionRetryAttempts,false)); httpconnectionManagerParams connMgrParams = new httpconnectionManagerParams(); connMgrParams.setConnectionTimeout(getConnectionTimeout()); connMgrParams.setDefaultMaxConnectionsPerHost(getMaxConnectionsPerHost()); connMgrParams.setMaxTotalConnections(getMaxTotalConnections()); connMgrParams.setReceiveBufferSize(getReceiveBufferSize()); connMgrParams.setSendBufferSize(getSendBufferSize()); connMgrParams.setTcpNoDelay(isTcpNoDelay()); MultiThreadedhttpconnectionManager connMgr = new MultiThreadedhttpconnectionManager(); connMgr.setParams(connMgrParams); HttpClient httpClient = new HttpClient(clientParams,connMgr); if (proxyHost != null) { HostConfiguration hostConfig = new HostConfiguration(); hostConfig.setProxy(proxyHost,proxyPort); httpClient.setHostConfiguration(hostConfig); if (proxyUsername != null) { AuthScope proxyAuthScope = new AuthScope(proxyHost,proxyPort); UsernamePasswordCredentials proxyCredentials = new UsernamePasswordCredentials(proxyUsername,proxyPassword); httpClient.getState().setProxyCredentials(proxyAuthScope,proxyCredentials); } } return httpClient; }
/** * Assigns {@link HttpClientParams HTTP protocol parameters} for this ProxyClient. * * @see HttpClientParams */ public synchronized void setParams(final HttpClientParams params) { if (params == null) { throw new IllegalArgumentException("Parameters may not be null"); } this.params = params; }
/** * Creates an instance of HttpClient with a user specified * {@link HttpClientParams parameter set} and * {@link httpconnectionManager HTTP connection manager}. * * @param params The {@link HttpClientParams parameters} to use. * @param httpconnectionManager The {@link httpconnectionManager connection manager} * to use. * * @since 3.0 */ public HttpClient(HttpClientParams params,httpconnectionManager httpconnectionManager) { super(); if (httpconnectionManager == null) { throw new IllegalArgumentException("httpconnectionManager cannot be null"); } if (params == null) { throw new IllegalArgumentException("Params may not be null"); } this.params = params; this.httpconnectionManager = httpconnectionManager; this.httpconnectionManager.getParams().setDefaults(this.params); }
public HttpMethodDirector( final httpconnectionManager connectionManager,final HostConfiguration hostConfiguration,final HttpClientParams params,final HttpState state ) { super(); this.connectionManager = connectionManager; this.hostConfiguration = hostConfiguration; this.params = params; this.state = state; this.authProcessor = new AuthChallengeProcessor(this.params); }
public void testMaxRedirectCheck() throws IOException { this.server.setHttpService(new CircularRedirectService()); getmethod httpget = new getmethod("/circular-oldlocation/"); try { this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRculaR_REDIRECTS,true); this.client.getParams().setIntParameter(HttpClientParams.MAX_REDIRECTS,5); this.client.executeMethod(httpget); fail("RedirectException exception should have been thrown"); } catch (RedirectException e) { // expected } finally { httpget.releaseConnection(); } }
public void testCircularRedirect() throws IOException { this.server.setHttpService(new CircularRedirectService()); getmethod httpget = new getmethod("/circular-oldlocation/"); try { this.client.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRculaR_REDIRECTS,false); this.client.executeMethod(httpget); fail("CircularRedirectException exception should have been thrown"); } catch (CircularRedirectException expected) { } finally { httpget.releaseConnection(); } }
private void initialize(Cluster cluster,boolean sslEnabled) { this.cluster = cluster; this.sslEnabled = sslEnabled; MultiThreadedhttpconnectionManager manager = new MultiThreadedhttpconnectionManager(); httpconnectionManagerParams managerParams = manager.getParams(); managerParams.setConnectionTimeout(2000); // 2 s managerParams.setDefaultMaxConnectionsPerHost(10); managerParams.setMaxTotalConnections(100); extraHeaders = new ConcurrentHashMap<String,String>(); this.httpClient = new HttpClient(manager); HttpClientParams clientParams = httpClient.getParams(); clientParams.setVersion(HttpVersion.HTTP_1_1); }
protected void initHttpClient() { if (httpClient == null) { httpClient = new HttpClient(); } params = new HttpClientParams(); myCookie = new Cookie(".secq.me","mycookie","stuff","/",false); initialState = new HttpState(); initialState.addCookie(myCookie); httpClient.setParams(params); httpClient.setState(initialState); httpClient.getParams().setCookiePolicy(CookiePolicy.RFC_2109); }
@VisibleForTesting protected HTTPArtifactBinding createDefaultArtifactBinding(ServiceProviderBuilder builder) { HttpClientParams params = new HttpClientParams(); params.setIntParameter(httpconnectionParams.CONNECTION_TIMEOUT,60000); HttpClient httpClient = new HttpClient(params,new MultiThreadedhttpconnectionManager()); ArtifactResolutionProfileImpl artifactResolutionProfile = new ArtifactResolutionProfileImpl(httpClient); builder.setSharedobject(ArtifactResolutionProfile.class,artifactResolutionProfile); HTTPSOAP11Binding soapBinding = new HTTPSOAP11Binding(parserPool); artifactResolutionProfile.setProcessor(new SAMLProcessorImpl(soapBinding)); return new HTTPArtifactBinding(parserPool,getVeLocityEngine(),artifactResolutionProfile); }
private static void setupHttpClientParams(HttpClient client,String userAgent) { client.getParams().setBooleanParameter( HttpClientParams.ALLOW_CIRculaR_REDIRECTS,true); client.gethttpconnectionManager().getParams() .setSoTimeout(SOCKET_TIMEOUT); client.gethttpconnectionManager().getParams() .setConnectionTimeout(CONNNECT_TIMEOUT); }
org.apache.http.client.params.AllClientPNames的实例源码
public static void setRequestOptions(HttpRequestBase request,HttpRequestOptions requestOptions) { request.getParams().setParameter(AllClientPNames.MAX_REDIRECTS,new Integer(requestOptions.getMaxRedirects())); request.getParams().setParameter(AllClientPNames.so_TIMEOUT,new Integer(requestOptions.getSocketTimeout())); request.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT,new Integer(requestOptions.getConnTimeout())); request.getParams().setParameter(AllClientPNames.ALLOW_CIRculaR_REDIRECTS,Boolean.valueOf(requestOptions.getAllowCircularRedirects())); Map requestHeaders = requestOptions.getRequestHeaders(); if (requestHeaders != null) { Iterator iter = requestHeaders.keySet().iterator(); String headerName; while (iter.hasNext()) { headerName = (String) iter.next(); request.addHeader(headerName,(String) requestHeaders.get(headerName)); } } }
@Override public WebResourceResponse loadUrl(String url) { NetflixUrl netflixUrl = new NetflixUrl(url); // note due to shouldOverrideUrlLoading,no /watch url ever gets here,so no need to cater for it if (netflixUrl.isNetflixUrl()) { client.getParams().setParameter(AllClientPNames.USER_AGENT,UserAgents.Mobile); if (netflixUrl.isTitle()) { client.getParams().setParameter(AllClientPNames.USER_AGENT,UserAgents.Desktop); return doLoadUrl(url); } else if (netflixUrl.isbrowse() || netflixUrl.isDefault()) { return doLoadUrl(url); } } // else return null; }
/** * Send get to URL. * * @param url * @return */ public static String sendGet(String url) { HttpClient httpClient = new DefaultHttpClient(); String content = null; try { if(url.indexOf("https") != -1){ httpClient = wrapClient(httpClient); } HttpGet httpGet = new HttpGet(url); // 请求超时 httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,40000); // 读取超时 httpClient.getParams().setParameter(CoreConnectionPNames.so_TIMEOUT,40000); httpClient.getParams().setParameter(AllClientPNames.STRICT_TRANSFER_ENCODING,"GBK"); content = httpClient.execute(httpGet,new BasicResponseHandler()); } catch (Exception e) { log.error("Get url faild,url: " + url,e); content = null; } finally { httpClient.getConnectionManager().shutdown(); } return content; }
private HttpClient getHttpClient() { if (this.httpClient == null) { LOG.log(Level.FINER,"Creating an HttpClient"); BasicHttpParams params = new BasicHttpParams(); params.setParameter(AllClientPNames.CONNECTION_TIMEOUT,this.connectionTimeout) .setParameter(AllClientPNames.COOKIE_POLICY,CookiePolicy.BEST_MATCH) .setParameter(AllClientPNames.HTTP_CONTENT_CHARSET,HTTP.UTF_8) .setParameter(AllClientPNames.so_TIMEOUT,this.socketTimeout); this.httpClient = new DefaultHttpClient(params); } return this.httpClient; }
public static String sendGet(String url,String encoding,String ip) { HttpClient httpClient = new DefaultHttpClient(); String content = null; try { if(url.indexOf("https") != -1){ httpClient = wrapClient(httpClient); } HttpGet httpGet = new HttpGet(url); if(ip != null){ httpGet.setHeader("X-Forwarded-For",ip); } // 请求超时 httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,40000); httpClient.getParams().setParameter(AllClientPNames.STRICT_TRANSFER_ENCODING,encoding); HttpResponse response = httpClient.execute(httpGet); httpentity entity = response.getEntity(); if (entity != null) { // 使用EntityUtils的toString方法,传递默认编码,在EntityUtils中的默认编码是ISO-8859-1 content = EntityUtils.toString(entity,encoding); } } catch (Exception e) { e.printstacktrace(); log.error("Get url faild,e); content = null; } finally { httpClient.getConnectionManager().shutdown(); } return content; }
/** * Send a payload message. * * @param payload * @throws IOException */ public void send(Payload payload) throws IOException { String room = payload.getRoom(); String token; if (StringUtils.isEmpty(room)) { // default room room = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_ROOM,null); token = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_TOKEN,null); } else { // specified room,validate token token = runtimeManager.getSettings().getString(String.format(Plugin.SETTING_ROOM_TOKEN,room),null); if (StringUtils.isEmpty(token)) { room = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_ROOM,null); token = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_TOKEN,null); log.warn("No HipChat API token specified for '{}',defaulting to '{}'",payload.getRoom(),room); log.warn("Please set '{} = TOKEN' in gitblit.properties",String.format(Plugin.SETTING_ROOM_TOKEN,room)); } } String hipchatUrl = String.format("https://api.hipchat.com/v2/room/%s/notification?auth_token=%s",room,token); Gson gson = new GsonBuilder().create(); HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(hipchatUrl); post.getParams().setParameter(CoreProtocolPNames.USER_AGENT,Constants.NAME + "/" + Constants.getVersion()); post.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET,"UTF-8"); client.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT,5000); client.getParams().setParameter(AllClientPNames.so_TIMEOUT,5000); String body = gson.toJson(payload); StringEntity entity = new StringEntity(body,"UTF-8"); entity.setContentType("application/json"); post.setEntity(entity); HttpResponse response = client.execute(post); int rc = response.getStatusLine().getStatusCode(); if (HttpStatus.SC_NO_CONTENT == rc) { // This is the expected result code // https://www.hipchat.com/docs/apiv2/method/send_room_notification // replace this with post.closeConnection() after JGit updates to HttpClient 4.2 post.abort(); } else { String result = null; InputStream is = response.getEntity().getContent(); try { byte [] buffer = new byte[8192]; ByteArrayOutputStream os = new ByteArrayOutputStream(); int len = 0; while ((len = is.read(buffer)) > -1) { os.write(buffer,len); } result = os.toString("UTF-8"); } finally { if (is != null) { is.close(); } } log.error("HipChat plugin sent:"); log.error(body); log.error("HipChat returned:"); log.error(result); throw new IOException(String.format("HipChat Error (%s): %s",rc,result)); } }
/** * Send a payload message. * * @param payload * @throws IOException */ public void send(Payload payload) throws IOException { String conversation = payload.getConversation(); String token; if (StringUtils.isEmpty(conversation)) { // default conversation token = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_TOKEN,null); } else { // specified conversation,validate token token = runtimeManager.getSettings().getString(String.format(Plugin.SETTING_CONVERSATION_TOKEN,conversation),null); if (StringUtils.isEmpty(token)) { token = runtimeManager.getSettings().getString(Plugin.SETTING_DEFAULT_TOKEN,null); log.warn("No Glip API token specified for '{}',defaulting to default conversation'",payload.getConversation()); log.warn("Please set '{} = TOKEN' in gitblit.properties",String.format(Plugin.SETTING_CONVERSATION_TOKEN,conversation)); } } Gson gson = new GsonBuilder().registerTypeAdapter(Date.class,new GmtDateTypeAdapter()).create(); String json = gson.toJson(payload); log.debug(json); HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(AllClientPNames.CONNECTION_TIMEOUT,5000); String conversationUrl = payload.getEndPoint(token); HttpPost post = new HttpPost(conversationUrl); post.getParams().setParameter(CoreProtocolPNames.USER_AGENT,"UTF-8"); // post as JSON StringEntity entity = new StringEntity(json,"UTF-8"); entity.setContentType("application/json"); post.setEntity(entity); HttpResponse response = client.execute(post); int rc = response.getStatusLine().getStatusCode(); if (HttpStatus.SC_OK == rc) { // This is the expected result code // replace this with post.closeConnection() after JGit updates to HttpClient 4.2 post.abort(); } else { String result = null; InputStream is = response.getEntity().getContent(); try { byte [] buffer = new byte[8192]; ByteArrayOutputStream os = new ByteArrayOutputStream(); int len = 0; while ((len = is.read(buffer)) > -1) { os.write(buffer,len); } result = os.toString("UTF-8"); } finally { if (is != null) { is.close(); } } log.error("Glip plugin sent:"); log.error(json); log.error("Glip returned:"); log.error(result); throw new IOException(String.format("Glip Error (%s): %s",result)); } }
public void setUserAgent(String ua) { httpParams.setParameter(AllClientPNames.USER_AGENT,ua); }
public void setConnectionTimeout(int milliseconds) { httpParams.setIntParameter(AllClientPNames.CONNECTION_TIMEOUT,milliseconds); }
public void setSocketTimeout(int milliseconds) { httpParams.setIntParameter(AllClientPNames.so_TIMEOUT,milliseconds); }
org.apache.http.client.params.ClientParamBean的实例源码
/** * Set follow redirects. * * @param followRedirects When true the client will follow redirects. */ public static void setFollowRedirects(HttpClient httpClient,boolean followRedirects) { new ClientParamBean(httpClient.getParams()).setHandleRedirects(followRedirects); }
/** * Set follow redirects. * * @param followRedirects When true the client will follow redirects. */ public static void setFollowRedirects(HttpClient httpClient,boolean followRedirects) { new ClientParamBean(httpClient.getParams()).setHandleRedirects(followRedirects); }
/** * Set follow redirects. * * @param followRedirects When true the client will follow redirects. */ public static void setFollowRedirects(HttpClient httpClient,boolean followRedirects) { new ClientParamBean(httpClient.getParams()).setHandleRedirects(followRedirects); }
org.apache.http.client.params.ClientPNames的实例源码
private synchronized void setHeaders(Collection<BasicHeader> headers) { if (headers != null && headers.size() != 0){ @SuppressWarnings("unchecked") Collection<BasicHeader> preHeaders = (Collection<BasicHeader>) mHttpClient.getParams().getParameter(ClientPNames.DEFAULT_HEADERS); if (preHeaders == null){ preHeaders = new ArrayList<BasicHeader>(); } for(BasicHeader bh:headers){ for(BasicHeader bh1:preHeaders){ if(bh.getName().equals(bh1.getName())){ preHeaders.remove(bh1); break; } } if (bh.getValue() != null){ preHeaders.add(bh); } } } }
public void process(final HttpRequest request,final HttpContext context) throws HttpException,IOException { if (request == null) { throw new IllegalArgumentException("HTTP request may not be null"); } String method = request.getRequestLine().getmethod(); if (method.equalsIgnoreCase("CONNECT")) { return; } // Add default headers @SuppressWarnings("unchecked") Collection<Header> defheaders = (Collection<Header>) request.getParams().getParameter( ClientPNames.DEFAULT_HEADERS); if (defheaders != null) { for (Header defheader : defheaders) { request.addHeader(defheader); } } }
private byte[] downloadHTTPfile_post(String formTodownloadLocation,List<NameValuePair> params) throws IOException,NullPointerException,URISyntaxException { BasicHttpContext localContext = new BasicHttpContext(); LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState); if (this.mimicWebDriverCookieState) { localContext.setAttribute(ClientContext.COOKIE_STORE,mimicCookieState(this.driver.manage().getCookies())); } HttpPost httppost = new HttpPost(formTodownloadLocation); HttpParams httpRequestParameters = httppost.getParams(); httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS,this.followRedirects); httppost.setParams(httpRequestParameters); httppost.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); LOG.info("Sending POST request for: " + httppost.getURI()); @SuppressWarnings("resource") HttpResponse response = new DefaultHttpClient().execute(httppost,localContext); this.httpStatusOfLastDownloadAttempt = response.getStatusLine().getStatusCode(); LOG.info("HTTP GET request status: " + this.httpStatusOfLastDownloadAttempt); byte[] file = IoUtils.toByteArray(response.getEntity().getContent()); response.getEntity().getContent().close(); return file; }
/** * Perform an HTTP Status check and return the response code * * @return * @throws IOException */ @SuppressWarnings("resource") public int getHTTPStatusCode() throws IOException { HttpClient client = new DefaultHttpClient(); BasicHttpContext localContext = new BasicHttpContext(); LOG.info("Mimic WebDriver cookie state: " + this.mimicWebDriverCookieState); if (this.mimicWebDriverCookieState) { localContext.setAttribute(ClientContext.COOKIE_STORE,mimicCookieState(this.driver.manage().getCookies())); } HttpRequestBase requestMethod = this.httpRequestMethod.getRequestMethod(); requestMethod.setURI(this.linktocheck); HttpParams httpRequestParameters = requestMethod.getParams(); httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS,this.followRedirects); requestMethod.setParams(httpRequestParameters); LOG.info("Sending " + requestMethod.getmethod() + " request for: " + requestMethod.getURI()); HttpResponse response = client.execute(requestMethod,localContext); LOG.info("HTTP " + requestMethod.getmethod() + " request status: " + response.getStatusLine().getStatusCode()); return response.getStatusLine().getStatusCode(); }
/** * 此处解释下MaxtTotal和DefaultMaxPerRoute的区别: * 1、MaxtTotal是整个池子的大小; * 2、DefaultMaxPerRoute是根据连接到的主机对MaxTotal的一个细分;比如: * MaxtTotal=400 DefaultMaxPerRoute=200 * 而我只连接到http://sishuok.com时,到这个主机的并发最多只有200;而不是400; * 而我连接到http://sishuok.com 和 http://qq.com时,到每个主机的并发最多只有200;即加起来是400(但不能超过400);所以起作用的设置是DefaultMaxPerRoute。 */ public HttpParams getHttpParams() { HttpParams params = new BasicHttpParams(); // 设置连接超时时间 Integer CONNECTION_TIMEOUT = 2 * 1000; // 设置请求超时2秒钟 根据业务调整 Integer SO_TIMEOUT = 2 * 1000; // 设置等待数据超时时间2秒钟 根据业务调整 // 定义了当从ClientConnectionManager中检索ManagedClientConnection实例时使用的毫秒级的超时时间 // 这个参数期望得到一个java.lang.Long类型的值。如果这个参数没有被设置,默认等于CONNECTION_TIMEOUT,因此一定要设置 Long CONN_MANAGER_TIMEOUT = 500L; // 该值就是连接不够用的时候等待超时时间,一定要设置,而且不能太大 () params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,CONNECTION_TIMEOUT); params.setIntParameter(CoreConnectionPNames.so_TIMEOUT,SO_TIMEOUT); params.setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT,CONN_MANAGER_TIMEOUT); // 在提交请求之前 测试连接是否可用 params.setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK,true); return params; }
public void process(final HttpRequest request,IOException { Args.notNull(request,"HTTP request"); final String method = request.getRequestLine().getmethod(); if (method.equalsIgnoreCase("CONNECT")) { return; } // Add default headers @SuppressWarnings("unchecked") Collection<? extends Header> defheaders = (Collection<? extends Header>) request.getParams().getParameter(ClientPNames.DEFAULT_HEADERS); if (defheaders == null) { defheaders = this.defaultHeaders; } if (defheaders != null) { for (final Header defheader : defheaders) { request.addHeader(defheader); } } }
private void createHttpClient(NaviPoolConfig poolConfig,ServerUrlUtil.ServerUrl server) { if (params == null) { // httpClient.setParams(params)HttpParams httpClient = new DefaultHttpClient(cm); httpClient.getParams().setParameter(CoreConnectionPNames.so_TIMEOUT,poolConfig.getSocketTimeout()); httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,poolConfig.getConnectTimeout()); } else { httpClient = new DefaultHttpClient(cm,params); if (poolConfig instanceof NaviHttpPoolConfig) { httpClient.setHttpRequestRetryHandler( new NaviHttpRequestRetryHandler(((NaviHttpPoolConfig) poolConfig).getRetryTimes(),false) ); } } // 配置数据源 httpClient.getParams().setParameter(ClientPNames.DEFAULT_HOST,new HttpHost(server.getHost(),server.getPort())); }
@Override public void process(final HttpRequest request,"HTTP request"); final String method = request.getRequestLine().getmethod(); if (method.equalsIgnoreCase("CONNECT")) { return; } // Add default headers @SuppressWarnings("unchecked") Collection<? extends Header> defheaders = (Collection<? extends Header>) request.getParams().getParameter(ClientPNames.DEFAULT_HEADERS); if (defheaders == null) { defheaders = this.defaultHeaders; } if (defheaders != null) { for (final Header defheader : defheaders) { request.addHeader(defheader); } } }
public void process(final HttpRequest request,"HTTP request"); final String method = request.getRequestLine().getmethod(); if (method.equalsIgnoreCase("CONNECT")) { return; } // Add default headers @SuppressWarnings("unchecked") Collection<? extends Header> defheaders = (Collection<? extends Header>) request.getParams().getParameter(ClientPNames.DEFAULT_HEADERS); if (defheaders == null) { defheaders = this.defaultHeaders; } if (defheaders != null) { for (final Header defheader : defheaders) { request.addHeader(defheader); } } }
@Test public void testSetParams() { ModifiableSolrParams params = new ModifiableSolrParams(); params.set(HttpClientUtil.PROP_ALLOW_COMPRESSION,true); params.set(HttpClientUtil.PROP_BASIC_AUTH_PASS,"pass"); params.set(HttpClientUtil.PROP_BASIC_AUTH_USER,"user"); params.set(HttpClientUtil.PROP_CONNECTION_TIMEOUT,12345); params.set(HttpClientUtil.PROP_FOLLOW_REDIRECTS,true); params.set(HttpClientUtil.PROP_MAX_CONNECTIONS,22345); params.set(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST,32345); params.set(HttpClientUtil.PROP_SO_TIMEOUT,42345); params.set(HttpClientUtil.PROP_USE_RETRY,false); DefaultHttpClient client = (DefaultHttpClient) HttpClientUtil.createClient(params); assertEquals(12345,httpconnectionParams.getConnectionTimeout(client.getParams())); assertEquals(PoolingClientConnectionManager.class,client.getConnectionManager().getClass()); assertEquals(22345,((PoolingClientConnectionManager)client.getConnectionManager()).getMaxTotal()); assertEquals(32345,((PoolingClientConnectionManager)client.getConnectionManager()).getDefaultMaxPerRoute()); assertEquals(42345,httpconnectionParams.getSoTimeout(client.getParams())); assertEquals(HttpClientUtil.NO_RETRY,client.getHttpRequestRetryHandler()); assertEquals("pass",client.getCredentialsProvider().getCredentials(new AuthScope("127.0.0.1",1234)).getpassword()); assertEquals("user",1234)).getUserPrincipal().getName()); assertEquals(true,client.getParams().getParameter(ClientPNames.HANDLE_REDIRECTS)); client.getConnectionManager().shutdown(); }
@Override public void init() throws servletexception { String doLogStr = getConfigParam(P_LOG); if (doLogStr != null) { this.doLog = Boolean.parseBoolean(doLogStr); } String doForwardIPString = getConfigParam(P_FORWARDEDFOR); if (doForwardIPString != null) { this.doForwardIP = Boolean.parseBoolean(doForwardIPString); } initTarget();//sets target* HttpParams hcParams = new BasicHttpParams(); hcParams.setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.IGnorE_COOKIES); readConfigParam(hcParams,ClientPNames.HANDLE_REDIRECTS,Boolean.class); proxyClient = createHttpClient(hcParams); }
public void prepareForbrowser() { // Clear cookies,let the browser handle them httpClient.setCookieStore(new BlankCookieStore()); httpClient.getCookieSpecs().register("easy",new CookieSpecFactory() { @Override public CookieSpec newInstance(final HttpParams params) { return new browserCompatSpec() { @Override public void validate(final Cookie cookie,final CookieOrigin origin) throws MalformedCookieException { // easy! } }; } }); httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY,"easy"); decompress = false; setFollowRedirects(false); }
@Override public void init() throws servletexception { String doLogStr = getConfigParam(P_LOG); if (doLogStr != null) { this.doLog = Boolean.parseBoolean(doLogStr); } String doForwardIPString = getConfigParam(P_FORWARDEDFOR); if (doForwardIPString != null) { this.doForwardIP = Boolean.parseBoolean(doForwardIPString); } initTarget();// sets target* HttpParams hcParams = new BasicHttpParams(); hcParams.setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.IGnorE_COOKIES); readConfigParam(hcParams,Boolean.class); proxyClient = createHttpClient(hcParams); }
/** * 获取DefaultHttpClient对象 * * @param charset * 字符编码 * @return DefaultHttpClient对象 */ private static DefaultHttpClient getDefaultHttpClient(final String charset) { DefaultHttpClient httpclient = new DefaultHttpClient(); // 模拟浏览器,解决一些服务器程序只允许浏览器访问的问题 httpclient.getParams().setParameter(CoreProtocolPNames.USER_AGENT,USER_AGENT); httpclient.getParams().setParameter( CoreProtocolPNames.USE_EXPECT_CONTINUE,Boolean.FALSE); httpclient.getParams().setParameter( CoreProtocolPNames.HTTP_CONTENT_CHARSET,charset == null ? CHARSET_ENCODING : charset); // 浏览器兼容性 httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.broWSER_COMPATIBILITY); // 定义重试策略 httpclient.setHttpRequestRetryHandler(requestRetryHandler); return httpclient; }
public void init() { SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http",PlainSocketFactory .getSocketFactory(),80)); HttpParams params = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(params,10); HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1); ClientConnectionManager cm = new ThreadSafeClientConnManager(params,schemeRegistry); httpClient = new DefaultHttpClient(cm,params); // 重试 // httpClient.setHttpRequestRetryHandler(new // DefaultHttpRequestRetryHandler(3,false)); // 超时设置 // httpClient.getParams().setIntParameter(httpconnectionParams.CONNECTION_TIMEOUT,// 10000); // httpClient.getParams().setIntParameter(httpconnectionParams.so_TIMEOUT,// 10000); httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,true); threadNum = 10; execService = Executors.newFixedThreadPool(threadNum); destroy = false; }
public String getRedirectUrl(String url) { httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,false); HttpGet httpget = new HttpGet(url); httpget.setHeader("User-Agent",userAgent); httpget.addHeader("Accept-Encoding","gzip,deflate"); String newUrl; try { HttpResponse response = httpClient.execute(httpget); Header locationHeader = response.getLastHeader("Location"); newUrl = locationHeader.getValue(); } catch (IOException e) { Log.d(TAG,"get url Failed,",e); newUrl = null; } httpClient.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,true); return newUrl; }
/** * <h1>wordpress constructor. After initializing this constructor,you can * call several core methods: login(Bundle userData),register(Bundle * userData)</h1> * * @param context * Pass the application's context to get this initialized. */ public wordpress(Context context,OnConnectionFailureListener listener) { this.context = context; API = context.getString(R.string.api); BASE_URL = context.getString(R.string.url) + "/" + API + "/"; httpClient.getHttpClient().getParams() .setParameter(ClientPNames.ALLOW_CIRculaR_REDIRECTS,true); postHandler = new wordpressResponseHandler<WPPost>(); commentHandler = new wordpressResponseHandler<WPComment>(); onConnectionFailureListener = listener; postHandler.setonConnectionFailureListener(onConnectionFailureListener); commentHandler .setonConnectionFailureListener(onConnectionFailureListener); }
public void process(final HttpRequest request,final HttpContext context) throws HttpException,IOException { if (request == null) { throw new IllegalArgumentException("HTTP request may not be null"); } String method = request.getRequestLine().getmethod(); if (method.equalsIgnoreCase("CONNECT")) { return; } // Add default headers @SuppressWarnings("unchecked") Collection<Header> defheaders = (Collection<Header>) request.getParams().getParameter( ClientPNames.DEFAULT_HEADERS); if (defheaders != null) { for (Header defheader : defheaders) { request.addHeader(defheader); } } }
@SuppressWarnings("deprecation") @Test public void testCookiePolicy() { SpringClientFactory factory = new SpringClientFactory(); AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); addEnvironment(parent,"ribbon.restclient.enabled=true"); parent.register(RibbonAutoConfiguration.class,ArchaiusAutoConfiguration.class); parent.refresh(); factory.setApplicationContext(parent); RestClient client = factory.getClient("foo",RestClient.class); ApacheHttpClient4 jerseyClient = (ApacheHttpClient4) client.getJerseyClient(); assertEquals(CookiePolicy.IGnorE_COOKIES,jerseyClient.getClientHandler() .getHttpClient().getParams().getParameter(ClientPNames.COOKIE_POLICY)); parent.close(); factory.destroy(); }
@Test public void testSetParams() { ModifiableSolrParams params = new ModifiableSolrParams(); params.set(HttpClientUtil.PROP_ALLOW_COMPRESSION,client.getParams().getParameter(ClientPNames.HANDLE_REDIRECTS)); client.getConnectionManager().shutdown(); }
private void prepairCookieStore(DefaultHttpClient client) { client.setCookieStore(cookieStore); CookieSpecFactory csf = new CookieSpecFactory() { public CookieSpec newInstance(HttpParams params) { return new browserCompatSpec() { @Override public void validate(Cookie cookie,CookieOrigin origin) throws MalformedCookieException { // not filtering; accept all cookies Log.d(this.toString(),"cookies"); } }; } }; client.getCookieSpecs().register("all",csf); client.getParams().setParameter( ClientPNames.COOKIE_POLICY,"all"); }
private HttpResponse getHTTPResponse() throws IOException,NullPointerException { if (fileURI == null) throw new NullPointerException("No file URI specified"); HttpClient client = new DefaultHttpClient(); BasicHttpContext localContext = new BasicHttpContext(); //Clear down the local cookie store every time to make sure we don't have any left over cookies influencing the test localContext.setAttribute(ClientContext.COOKIE_STORE,null); System.out.println("Mimic WebDriver cookie state: " + mimicWebDriverCookieState); if (mimicWebDriverCookieState) { localContext.setAttribute(ClientContext.COOKIE_STORE,mimicCookieState(driver.manage().getCookies())); } HttpRequestBase requestMethod = httpRequestMethod.getRequestMethod(); requestMethod.setURI(fileURI); HttpParams httpRequestParameters = requestMethod.getParams(); httpRequestParameters.setParameter(ClientPNames.HANDLE_REDIRECTS,followRedirects); requestMethod.setParams(httpRequestParameters); //Todo if post send map of variables,also need to add a post map setter System.out.println("Sending " + httpRequestMethod.toString() + " request for: " + fileURI); return client.execute(requestMethod,localContext); }
public void process(final HttpRequest request,"HTTP request"); final String method = request.getRequestLine().getmethod(); if (method.equalsIgnoreCase("CONNECT")) { return; } // Add default headers @SuppressWarnings("unchecked") Collection<? extends Header> defheaders = (Collection<? extends Header>) request.getParams().getParameter(ClientPNames.DEFAULT_HEADERS); if (defheaders == null) { defheaders = this.defaultHeaders; } if (defheaders != null) { for (final Header defheader : defheaders) { request.addHeader(defheader); } } }
public synchronized HttpClient getClient() { if (m_client == null) { m_client = new DefaultHttpClient(); HttpParams clientParams = m_client.getParams(); clientParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,m_timeout); clientParams.setIntParameter(CoreConnectionPNames.so_TIMEOUT,m_timeout); clientParams.setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.broWSER_COMPATIBILITY); m_client.setParams(clientParams); m_client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(m_retries,false)); } return m_client; }
private static HttpParams buildParams(final HttpCollectionSet collectionSet) { HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,computeVersion(collectionSet.getUriDef())); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,Integer.parseInt(ParameterMap.getKeyedString(collectionSet.getParameters(),"timeout",DEFAULT_SO_TIMEOUT))); params.setIntParameter(CoreConnectionPNames.so_TIMEOUT,DEFAULT_SO_TIMEOUT))); params.setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.broWSER_COMPATIBILITY); //review the httpclient code,looks like virtual host is checked for null //and if true,sets Host to the connection's host property String virtualHost = collectionSet.getUriDef().getUrl().getVirtualHost(); if (virtualHost != null) { params.setParameter( ClientPNames.VIRTUAL_HOST,new HttpHost(virtualHost,collectionSet.getPort()) ); } return params; }
private void loadCookie(String authToken) throws AuthenticationException,IOException { String url = AUTH_COOKIE_URL.buildUpon().appendQueryParameter("auth",authToken).build().toString(); HttpGet method = new HttpGet(url); httpClient.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS,false); HttpResponse res = httpClient.execute(method,httpContext); Header[] headers = res.getHeaders("Set-Cookie"); if (res.getEntity() != null) { res.getEntity().consumeContent(); } if (res.getStatusLine().getStatusCode() != HttpStatus.SC_MOVED_TEMPORARILY || headers.length == 0) { throw new AuthenticationException("Failed to get cookie"); } if (!hasValidAuthCookie()) throw new AuthenticationException("Failed to get cookie"); }
private synchronized InputStream doRequest(String url) throws IOException { if (verbose) { System.out.println("Making HTTP request: " + url); } final HttpParams httpParams = new BasicHttpParams(); HttpClient client = new DefaultHttpClient(httpParams); if (proxyIP != null) { HttpHost proxy = new HttpHost(proxyIP,proxyPort); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); } httpconnectionParams.setConnectionTimeout(client.getParams(),(int) timeout); httpconnectionParams.setSoTimeout(client.getParams(),(int) timeout); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT,this.userAgent); client.getParams().setParameter(ClientPNames.ALLOW_CIRculaR_REDIRECTS,true); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); if (verbose) { System.out.println("Status code for request: " + response.getStatusLine().getStatusCode()); } if (response.getStatusLine().getStatusCode() == 429) { hardrateLimit = System.currentTimeMillis() + 900000L; //15 min throw new RateLimitReachedException(); } return response.getEntity().getContent(); }
private DefaultHttpClient getHttpClient() { DefaultHttpClient client = new DefaultHttpClient(); // I believe DefaultHttpClient handles redirects by default anyway client.getParams().setParameter(ClientPNames.HANDLE_REDIRECTS,Boolean.TRUE); client.addRequestInterceptor(new HttpRequestInterceptor() { public void process( final HttpRequest request,final HttpContext context) throws HttpException,IOException { request.addHeader("Accept","application/xml"); } }); SSLSocketFactory.getSocketFactory().setHostnameVerifier( SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER ); return client; }
@Test @DatabaseSetup("/database_seed.xml") public void the_approval_will_be_remembered() throws IOException { givenValidAuthCode("marissa","koala","internal"); givenAuthCode(); givenAccesstokenUsingAuthCode(); { HttpGet httpGet = new HttpGet(loginUri); httpGet.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.netscape); httpGet.getParams().setBooleanParameter("http.protocol.handle-redirects",false); authCodeResponse = httpClient.execute(httpGet); httpGet.releaseConnection(); } givenAuthCode(); givenAccesstokenUsingAuthCode(); assertTrue(accesstoken != null); assertNotNull(accesstoken.getRefreshToken()); }
@Test @DatabaseSetup("/database_seeds/LoginoAuth2IT/database_seed_zero_validity.xml") public void the_approval_will_not_be_remembered_if_validity_is_zero() throws IOException { givenValidAuthCode("marissa","internal"); givenAuthCode(); givenAccesstokenUsingAuthCode(); HttpGet httpGet = new HttpGet(loginUri); httpGet.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.netscape); httpGet.getParams().setBooleanParameter("http.protocol.handle-redirects",false); authCodeResponse = httpClient.execute(httpGet); String response = IoUtils.toString(authCodeResponse.getEntity().getContent()); httpGet.releaseConnection(); assertthat(response,containsstring("<title>Access confirmation</title>")); }
public synchronized HttpClient getClient() { if (m_client == null) { m_client = new DefaultHttpClient(); HttpParams clientParams = m_client.getParams(); clientParams.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,false)); } return m_client; }
private static HttpParams buildParams(final HttpCollectionSet collectionSet) { HttpParams params = new BasicHttpParams(); params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,collectionSet.getPort()) ); } return params; }
public static void reqArrayNoSSL(String url,Context parent,ModelCallback<JSONArray> callback) { RequestAsyncTask<JSONArray> task = new RequestAsyncTask<JSONArray>( "get",url,null,false,parent,callback) { @Override protected JSONArray toJSON(String responseStr) throws JSONException { return new JSONArray(responseStr); } }; task.client = new DefaultHttpClient(); task.client.getParams().setParameter( ClientPNames.ALLOW_CIRculaR_REDIRECTS,true); task.headers = new HashMap<String,String>(); task.headers.put("User-Agent",broWSER_LIKE_USER_AGENT); executeOnExecutor(task,HIGH_PRIORITY_EXECUTOR); }
/** * Returns a client with all our selected properties / params. * * @return client */ public static final DefaultHttpClient getClient() { // create a singular HttpClient object DefaultHttpClient client = new DefaultHttpClient(connectionManager); // dont retry client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(0,false)); // get the params for the client HttpParams params = client.getParams(); // establish a connection within x seconds params.setParameter(CoreConnectionPNames.so_TIMEOUT,connectionTimeout); // no redirects params.setParameter(ClientPNames.HANDLE_REDIRECTS,false); // set custom ua params.setParameter(CoreProtocolPNames.USER_AGENT,userAgent); // set the proxy if the user has one set if ((System.getProperty("http.proxyHost") != null) && (System.getProperty("http.proxyPort") != null)) { HttpHost proxy = new HttpHost(System.getProperty("http.proxyHost").toString(),Integer.valueOf(System.getProperty("http.proxyPort"))); client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); } return client; }
/** * Start a HTTP Session with authorisation * * @param username * @param password */ public void startSession(String username,String password) { // Create your httpclient client = new DefaultHttpClient(); client.getParams().setBooleanParameter(ClientPNames.ALLOW_CIRculaR_REDIRECTS,true); // Then provide the right credentials client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST,AuthScope.ANY_PORT),new UsernamePasswordCredentials(username,password)); // Generate BASIC scheme object and stick it to the execution context basicAuth = new BasicScheme(); context = new BasicHttpContext(); context.setAttribute("preemptive-auth",basicAuth); // Add as the first (because of the zero) request interceptor // It will first intercept the request and preemptively initialize the authentication scheme if there is not client.addRequestInterceptor(new PreemptiveAuth(),0); }
private DefaultHttpClient createClient(boolean https) { final DefaultHttpClient client = (https ? new DefaultHttpClient(conMan) : new DefaultHttpClient()); // Allows a slightly lenient cookie acceptance client.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.broWSER_COMPATIBILITY); // Allows follow of redirects on POST client.setRedirectStrategy(new LaxRedirectStrategy()); return client; }
private static int httpNotification(String uri,int timeout) throws IOException,URISyntaxException { DefaultHttpClient client = new DefaultHttpClient(); client.getParams() .setIntParameter(CoreConnectionPNames.so_TIMEOUT,timeout) .setLongParameter(ClientPNames.CONN_MANAGER_TIMEOUT,(long) timeout); HttpGet httpGet = new HttpGet(new URI(uri)); httpGet.setHeader("Accept","*/*"); return client.execute(httpGet).getStatusLine().getStatusCode(); }
/** * Setup some variables */ public void setup() { log.info("Setup..."); if (StringUtils.isEmpty(this.username)) { throw new IllegalArgumentException("Username is mandatory."); } if (StringUtils.isEmpty(this.password)) { throw new IllegalArgumentException("Password is mandatory."); } this.deviceid = InstagramHashUtil.generatedeviceid(this.username,this.password); if (StringUtils.isEmpty(this.uuid)) { this.uuid = InstagramGenericUtil.generateUuid(true); } if (this.cookieStore == null) { this.cookieStore = new BasicCookieStore(); } log.info("Device ID is: " + this.deviceid + ",random id: " + this.uuid); this.client = new DefaultHttpClient(); this.client.getParams().setParameter(ClientPNames.COOKIE_POLICY,CookiePolicy.broWSER_COMPATIBILITY); this.client.setCookieStore(this.cookieStore); }
private static int httpNotification(String uri,"*/*"); return client.execute(httpGet).getStatusLine().getStatusCode(); }
今天关于带有SFTP的Paramiko的SSHClient和sftp ssh的分享就到这里,希望大家有所收获,若想了解更多关于org.apache.commons.httpclient.params.HttpClientParams的实例源码、org.apache.http.client.params.AllClientPNames的实例源码、org.apache.http.client.params.ClientParamBean的实例源码、org.apache.http.client.params.ClientPNames的实例源码等相关知识,可以在本站进行查询。
本文标签: