List of usage examples for org.apache.http.impl.client DefaultHttpClient getCredentialsProvider
public synchronized final CredentialsProvider getCredentialsProvider()
From source file:com.lgallardo.youtorrentcontroller.JSONParser.java
public JSONArray getJSONArrayFromUrl(String url) throws JSONParserStatusCodeException { // if server is published in a subfolder, fix url if (subfolder != null && !subfolder.equals("")) { url = subfolder + "/" + url; }/*from w w w .j a v a 2 s.c o m*/ HttpResponse httpResponse; DefaultHttpClient httpclient; HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. // The default value is zero, that means the timeout is not used. int timeoutConnection = connection_timeout * 1000; // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = data_timeout * 1000; // Set http parameters HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpProtocolParams.setUserAgent(httpParameters, "youTorrent"); HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8); // Making HTTP request HttpHost targetHost = new HttpHost(hostname, port, protocol); httpclient = getNewHttpClient(); // Set http parameters httpclient.setParams(httpParameters); try { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); httpclient.getCredentialsProvider().setCredentials(authScope, credentials); url = protocol + "://" + hostname + ":" + port + "/" + url; HttpGet httpget = new HttpGet(url); if (this.cookie != null) { httpget.setHeader("Cookie", this.cookie); } httpResponse = httpclient.execute(targetHost, httpget); StatusLine statusLine = httpResponse.getStatusLine(); int mStatusCode = statusLine.getStatusCode(); if (mStatusCode != 200) { httpclient.getConnectionManager().shutdown(); throw new JSONParserStatusCodeException(mStatusCode); } HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); // Build JSON BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); jArray = new JSONArray(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } catch (UnsupportedEncodingException e) { } catch (ClientProtocolException e) { Log.e("JSON", "Client: " + e.toString()); e.printStackTrace(); } catch (IOException e) { Log.e("JSON", "IO: " + e.toString()); // e.printStackTrace(); httpclient.getConnectionManager().shutdown(); throw new JSONParserStatusCodeException(TIMEOUT_ERROR); } catch (JSONParserStatusCodeException e) { httpclient.getConnectionManager().shutdown(); throw new JSONParserStatusCodeException(e.getCode()); } catch (Exception e) { Log.e("JSON", "Generic: " + e.toString()); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } // return JSON String return jArray; }
From source file:org.xmlsh.internal.commands.http.java
private void setOptions(DefaultHttpClient client, HttpHost host, Options opts) throws KeyManagementException, NoSuchAlgorithmException, UnrecoverableKeyException, CertificateException, FileNotFoundException, KeyStoreException, IOException { HttpParams params = client.getParams(); HttpConnectionParamBean connection = new HttpConnectionParamBean(params); if (opts.hasOpt("connectTimeout")) connection.setConnectionTimeout((int) (opts.getOptDouble("connectTimeout", 0) * 1000.)); if (opts.hasOpt("readTimeout")) connection.setSoTimeout((int) (opts.getOptDouble("readTimeout", 0) * 1000.)); /*/*from www. j a v a 2 s. c o m*/ * if( opts.hasOpt("useCaches")) * client.setUseCaches( opts.getOpt("useCaches").getFlag()); * * * if( opts.hasOpt("followRedirects")) * * client.setInstanceFollowRedirects( * opts.getOpt("followRedirects").getFlag()); * * * * * * * String disableTrustProto = opts.getOptString("disableTrust", null); * * String keyStore = opts.getOptString("keystore", null); * String keyPass = opts.getOptString("keypass", null); * String sslProto = opts.getOptString("sslProto", "SSLv3"); * * if(disableTrustProto != null && client instanceof HttpsURLConnection ) * disableTrust( (HttpsURLConnection) client , disableTrustProto ); * * else * if( keyStore != null ) * setClient( (HttpsURLConnection) client , keyStore , keyPass , sslProto ); * */ String disableTrustProto = opts.getOptString("disableTrust", null); if (disableTrustProto != null) disableTrust(client, disableTrustProto); String user = opts.getOptString("user", null); String pass = opts.getOptString("password", null); if (user != null && pass != null) { UsernamePasswordCredentials creds = new UsernamePasswordCredentials(user, pass); client.getCredentialsProvider().setCredentials(AuthScope.ANY, creds); /* * // Create AuthCache instance * AuthCache authCache = new BasicAuthCache(); * // Generate DIGEST scheme object, initialize it and add it to the local * // auth cache * DigestScheme digestAuth = new DigestScheme(); * // Suppose we already know the realm name * digestAuth.overrideParamter("realm", "some realm"); * // Suppose we already know the expected nonce value * digestAuth.overrideParamter("nonce", "whatever"); * authCache.put(host, digestAuth); * * // Add AuthCache to the execution context * BasicHttpContext localcontext = new BasicHttpContext(); * localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); */ } }
From source file:com.lgallardo.youtorrentcontroller.JSONParser.java
public String getApi() throws JSONParserStatusCodeException { String url = "version/api"; // if server is publish in a subfolder, fix url if (subfolder != null && !subfolder.equals("")) { url = subfolder + "/" + url; }/*from ww w . j a v a2s.c o m*/ String APIVersionString = null; HttpResponse httpResponse; DefaultHttpClient httpclient; HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. // The default value is zero, that means the timeout is not used. int timeoutConnection = connection_timeout * 1000; // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = data_timeout * 1000; // Set http parameters HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpProtocolParams.setUserAgent(httpParameters, "qBittorrent for Android"); HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8); // Making HTTP request HttpHost targetHost = new HttpHost(hostname, port, protocol); // httpclient = new DefaultHttpClient(); httpclient = getNewHttpClient(); // Set http parameters httpclient.setParams(httpParameters); try { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(this.username, this.password); httpclient.getCredentialsProvider().setCredentials(authScope, credentials); // set http parameters url = protocol + "://" + hostname + ":" + port + "/" + url; HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(targetHost, httpget); HttpEntity entity = response.getEntity(); StatusLine statusLine = response.getStatusLine(); int mStatusCode = statusLine.getStatusCode(); // Log.d("Debug", "API - mStatusCode: " + mStatusCode); if (mStatusCode == 200) { // Save API APIVersionString = EntityUtils.toString(response.getEntity()); // Log.d("Debug", "API - ApiString: " + APIVersionString); } if (entity != null) { entity.consumeContent(); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } catch (Exception e) { // Log.i("APIVer", "Exception " + e.toString()); } // if (APIVersionString == null) { // APIVersionString = ""; // } return APIVersionString; }
From source file:com.lgallardo.qbittorrentclient.JSONParser.java
public JSONArray getJSONArrayFromUrl(String url) throws JSONParserStatusCodeException { // if server is published in a subfolder, fix url if (subfolder != null && !subfolder.equals("")) { url = subfolder + "/" + url; }/*www . j a va2 s . com*/ HttpResponse httpResponse; DefaultHttpClient httpclient; HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. // The default value is zero, that means the timeout is not used. int timeoutConnection = connection_timeout * 1000; // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = data_timeout * 1000; // Set http parameters HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpProtocolParams.setUserAgent(httpParameters, "qBittorrent for Android"); HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8); // Making HTTP request HttpHost targetHost = new HttpHost(hostname, port, protocol); httpclient = getNewHttpClient(); httpclient.setParams(httpParameters); try { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password); httpclient.getCredentialsProvider().setCredentials(authScope, credentials); url = protocol + "://" + hostname + ":" + port + "/" + url; HttpGet httpget = new HttpGet(url); if (this.cookie != null) { httpget.setHeader("Cookie", this.cookie); } httpResponse = httpclient.execute(targetHost, httpget); StatusLine statusLine = httpResponse.getStatusLine(); int mStatusCode = statusLine.getStatusCode(); if (mStatusCode != 200) { httpclient.getConnectionManager().shutdown(); throw new JSONParserStatusCodeException(mStatusCode); } HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); // Build JSON BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); jArray = new JSONArray(json); } catch (JSONException e) { Log.e("JSON Parser", "Error parsing data " + e.toString()); } catch (UnsupportedEncodingException e) { } catch (ClientProtocolException e) { Log.e("JSON", "Client: " + e.toString()); e.printStackTrace(); } catch (SSLPeerUnverifiedException e) { Log.e("JSON", "SSLPeerUnverifiedException: " + e.toString()); throw new JSONParserStatusCodeException(NO_PEER_CERTIFICATE); } catch (IOException e) { Log.e("JSON", "IO: " + e.toString()); // e.printStackTrace(); throw new JSONParserStatusCodeException(TIMEOUT_ERROR); } catch (JSONParserStatusCodeException e) { throw new JSONParserStatusCodeException(e.getCode()); } catch (Exception e) { Log.e("JSON", "Generic: " + e.toString()); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } // return JSON String return jArray; }
From source file:com.lgallardo.youtorrentcontroller.JSONParser.java
public String[] getToken() throws JSONParserStatusCodeException { String url = "gui/token.html"; // if server is publish in a subfolder, fix url if (subfolder != null && !subfolder.equals("")) { url = subfolder + "/" + url; }/*w w w . ja v a2s . c o m*/ String tokenString = null; String cookieString = "NULL"; HttpResponse httpResponse; DefaultHttpClient httpclient; HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. // The default value is zero, that means the timeout is not used. int timeoutConnection = connection_timeout * 1000; // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = data_timeout * 1000; // Set http parameters HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpProtocolParams.setUserAgent(httpParameters, "youTorrent Controller"); HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8); // Making HTTP request HttpHost targetHost = new HttpHost(hostname, port, protocol); // httpclient = new DefaultHttpClient(); httpclient = getNewHttpClient(); // Set http parameters httpclient.setParams(httpParameters); try { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(this.username, this.password); httpclient.getCredentialsProvider().setCredentials(authScope, credentials); url = protocol + "://" + hostname + ":" + port + "/" + url; HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(targetHost, httpget); HttpEntity entity = response.getEntity(); StatusLine statusLine = response.getStatusLine(); int mStatusCode = statusLine.getStatusCode(); // Log.d("Debug", "Token - mStatusCode: " + mStatusCode); if (mStatusCode != 200) { httpclient.getConnectionManager().shutdown(); throw new JSONParserStatusCodeException(mStatusCode); } if (mStatusCode == 200) { // Save cookie List<Cookie> cookies = httpclient.getCookieStore().getCookies(); if (!cookies.isEmpty()) { cookieString = cookies.get(0).getName() + "=" + cookies.get(0).getValue() + "; domain=" + cookies.get(0).getDomain(); cookieString = cookies.get(0).getName() + "=" + cookies.get(0).getValue(); } // Log.d("Debug", "JSONParser - cookieString: " + cookieString); // Get token is = entity.getContent(); // Build response BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); tokenString = sb.toString(); // Replace html tags with "" tokenString = tokenString.replaceAll("<.*?>", "").trim(); // Log.d("Debug", "API - Token: " + tokenString); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } catch (Exception e) { Log.i("Debug", "Exception " + e.toString()); } return new String[] { tokenString, cookieString }; }
From source file:com.lgallardo.youtorrentcontroller.JSONParser.java
public String getVersion() throws JSONParserStatusCodeException { String url = "about.html"; // if server is publish in a subfolder, fix url if (subfolder != null && !subfolder.equals("")) { url = subfolder + "/" + url; }//from w w w .ja va 2 s . com String aboutHtml = null; String version = null; HttpResponse httpResponse; DefaultHttpClient httpclient; HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. // The default value is zero, that means the timeout is not used. int timeoutConnection = connection_timeout * 1000; // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = data_timeout * 1000; // Set http parameters HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpProtocolParams.setUserAgent(httpParameters, "qBittorrent for Android"); HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8); // Making HTTP request HttpHost targetHost = new HttpHost(hostname, port, protocol); // httpclient = new DefaultHttpClient(); httpclient = getNewHttpClient(); // Set http parameters httpclient.setParams(httpParameters); try { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(this.username, this.password); httpclient.getCredentialsProvider().setCredentials(authScope, credentials); // set http parameters url = protocol + "://" + hostname + ":" + port + "/" + url; // Log.d("Debug", "URL: " + url); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(targetHost, httpget); HttpEntity entity = response.getEntity(); StatusLine statusLine = response.getStatusLine(); int mStatusCode = statusLine.getStatusCode(); // Log.d("Debug", "Version - mStatusCode: " + mStatusCode); if (mStatusCode == 200) { // Save API aboutHtml = EntityUtils.toString(response.getEntity()); String aboutStartText = "qBittorrent v"; String aboutEndText = " (Web UI)"; int aboutStart = aboutHtml.indexOf(aboutStartText); int aboutEnd = aboutHtml.indexOf(aboutEndText); if (aboutEnd == -1) { aboutEndText = " Web UI"; aboutEnd = aboutHtml.indexOf(aboutEndText); } if (aboutStart >= 0 && aboutEnd > aboutStart) { version = aboutHtml.substring(aboutStart + aboutStartText.length(), aboutEnd); } // Log.d("Debug", "Version - VersionString: " + version); } if (entity != null) { entity.consumeContent(); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } catch (Exception e) { // Log.i("APIVer", "Exception " + e.toString()); } if (version == null) { version = ""; } return version; }
From source file:neembuu.vfs.test.FileNameAndSizeFinderService.java
private DefaultHttpClient newClient() { DefaultHttpClient client = new DefaultHttpClient(); GlobalTestSettings.ProxySettings proxySettings = GlobalTestSettings.getGlobalProxySettings(); HttpContext context = new BasicHttpContext(); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", new PlainSocketFactory(), 80)); try {/*from w w w . ja v a2 s . c o m*/ KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); schemeRegistry.register(new Scheme("https", new SSLSocketFactory(keyStore), 8080)); } catch (Exception a) { a.printStackTrace(System.err); } context.setAttribute(ClientContext.SCHEME_REGISTRY, schemeRegistry); context.setAttribute(ClientContext.AUTHSCHEME_REGISTRY, new BasicScheme()/*file.httpClient.getAuthSchemes()*/); context.setAttribute(ClientContext.COOKIESPEC_REGISTRY, client.getCookieSpecs()/*file.httpClient.getCookieSpecs()*/ ); BasicCookieStore basicCookieStore = new BasicCookieStore(); context.setAttribute(ClientContext.COOKIE_STORE, basicCookieStore/*file.httpClient.getCookieStore()*/); context.setAttribute(ClientContext.CREDS_PROVIDER, new BasicCredentialsProvider()/*file.httpClient.getCredentialsProvider()*/); HttpConnection hc = new DefaultHttpClientConnection(); context.setAttribute(ExecutionContext.HTTP_CONNECTION, hc); //System.out.println(file.httpClient.getParams().getParameter("http.useragent")); HttpParams httpParams = new BasicHttpParams(); if (proxySettings != null) { AuthState as = new AuthState(); as.setCredentials(new UsernamePasswordCredentials(proxySettings.userName, proxySettings.password)); as.setAuthScope(AuthScope.ANY); as.setAuthScheme(new BasicScheme()); httpParams.setParameter(ClientContext.PROXY_AUTH_STATE, as); httpParams.setParameter("http.proxy_host", new HttpHost(proxySettings.host, proxySettings.port)); } client = new DefaultHttpClient( new SingleClientConnManager(httpParams/*file.httpClient.getParams()*/, schemeRegistry), httpParams/*file.httpClient.getParams()*/); if (proxySettings != null) { client.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(proxySettings.userName, proxySettings.password)); } return client; }
From source file:com.lgallardo.qbittorrentclient.JSONParser.java
public String getVersion() throws JSONParserStatusCodeException { String url = "about.html"; // if server is publish in a subfolder, fix url if (subfolder != null && !subfolder.equals("")) { url = subfolder + "/" + url; }/*from ww w. j a v a 2 s. co m*/ String aboutHtml = null; String version = null; HttpResponse httpResponse; DefaultHttpClient httpclient; HttpParams httpParameters = new BasicHttpParams(); // Set the timeout in milliseconds until a connection is established. // The default value is zero, that means the timeout is not used. int timeoutConnection = connection_timeout * 1000; // Set the default socket timeout (SO_TIMEOUT) // in milliseconds which is the timeout for waiting for data. int timeoutSocket = data_timeout * 1000; // Set http parameters HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection); HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); HttpProtocolParams.setUserAgent(httpParameters, "qBittorrent for Android"); HttpProtocolParams.setVersion(httpParameters, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(httpParameters, HTTP.UTF_8); // Making HTTP request HttpHost targetHost = new HttpHost(hostname, port, protocol); // httpclient = new DefaultHttpClient(); httpclient = getNewHttpClient(); httpclient.setParams(httpParameters); try { AuthScope authScope = new AuthScope(targetHost.getHostName(), targetHost.getPort()); UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(this.username, this.password); httpclient.getCredentialsProvider().setCredentials(authScope, credentials); // set http parameters url = protocol + "://" + hostname + ":" + port + "/" + url; // Log.d("Debug", "URL: " + url); HttpGet httpget = new HttpGet(url); HttpResponse response = httpclient.execute(targetHost, httpget); HttpEntity entity = response.getEntity(); StatusLine statusLine = response.getStatusLine(); int mStatusCode = statusLine.getStatusCode(); // Log.d("Debug", "Version - mStatusCode: " + mStatusCode); if (mStatusCode == 200) { // Save API aboutHtml = EntityUtils.toString(response.getEntity()); String aboutStartText = "qBittorrent v"; String aboutEndText = " (Web UI)"; int aboutStart = aboutHtml.indexOf(aboutStartText); int aboutEnd = aboutHtml.indexOf(aboutEndText); if (aboutEnd == -1) { aboutEndText = " Web UI"; aboutEnd = aboutHtml.indexOf(aboutEndText); } if (aboutStart >= 0 && aboutEnd > aboutStart) { version = aboutHtml.substring(aboutStart + aboutStartText.length(), aboutEnd); } // Log.d("Debug", "Version - VersionString: " + version); } // if (mStatusCode != 200) { // httpclient.getConnectionManager().shutdown(); // throw new JSONParserStatusCodeException(mStatusCode); // } // if (entity != null) { entity.consumeContent(); } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } catch (Exception e) { // Log.i("APIVer", "Exception " + e.toString()); } if (version == null) { version = ""; } return version; }