List of usage examples for org.apache.http.auth AuthScope ANY_PORT
int ANY_PORT
To view the source code for org.apache.http.auth AuthScope ANY_PORT.
Click Source Link
From source file:es.tsb.ltba.nomhad.example.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient = wrapClient(httpclient); httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("A100", "123456")); try {/*from ww w . j ava 2 s . co m*/ HttpGet httpget = new HttpGet(NOMHAD_URL_HEADER + "A100" + OBSERVATIONS_REQUEST); HttpPost httppost = new HttpPost(NOMHAD_URL_HEADER + "A100" + OBSERVATIONS_REQUEST); httppost.setEntity(new StringEntity(BODY_TEST)); System.out.println("executing request " + httpget.getURI()); // Create a response handler ResponseHandler<String> responseHandler = new BasicResponseHandler(); String responseBody = httpclient.execute(httppost, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); System.out.println("----------------------------------------"); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpclient.getConnectionManager().shutdown(); } }
From source file:com.cloudhopper.httpclient.util.HttpPostMain.java
static public void main(String[] args) throws Exception { //// w w w . j a v a 2s . co m // target urls // String strURL = "http://209.226.31.233:9009/SendSmsService/b98183b99a1f473839ce569c78b84dbd"; // Username: Twitter // Password: Twitter123 TrustManager easyTrustManager = new X509TrustManager() { public void checkClientTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException { // allow all } public void checkServerTrusted(java.security.cert.X509Certificate[] arg0, String arg1) throws CertificateException { // allow all } public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } }; Scheme http = new Scheme("http", PlainSocketFactory.getSocketFactory(), 80); SSLContext sslcontext = SSLContext.getInstance("TLS"); sslcontext.init(null, new TrustManager[] { easyTrustManager }, null); SSLSocketFactory sf = new SSLSocketFactory(sslcontext); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); Scheme https = new Scheme("https", sf, 443); //SchemeRegistry sr = new SchemeRegistry(); //sr.register(http); //sr.register(https); // create and initialize scheme registry //SchemeRegistry schemeRegistry = new SchemeRegistry(); //schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); // create an HttpClient with the ThreadSafeClientConnManager. // This connection manager must be used if more than one thread will // be using the HttpClient. //ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry); //cm.setMaxTotalConnections(1); DefaultHttpClient client = new DefaultHttpClient(); client.getConnectionManager().getSchemeRegistry().register(https); // for (int i = 0; i < 1; i++) { // // create a new ticket id // //String ticketId = TicketUtil.generate(1, System.currentTimeMillis()); /** StringBuilder string0 = new StringBuilder(200) .append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") .append("<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n") .append(" <S:Header>\n") .append(" <ns3:TransactionID xmlns:ns4=\"http://vmp.vzw.com/schema\"\n") .append("xmlns:ns3=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-4\">" + ticketId + "</ns3:TransactionID>\n") .append(" </S:Header>\n") .append(" <S:Body>\n") .append(" <ns2:OptinReq xmlns:ns4=\"http://schemas.xmlsoap.org/soap/envelope/\"\n") .append("xmlns:ns3=\"http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-6-MM7-1-4\"\n") .append("xmlns:ns2=\"http://vmp.vzw.com/schema\">\n") .append(" <ns2:VASPID>twitter</ns2:VASPID>\n") .append(" <ns2:VASID>tm33t!</ns2:VASID>\n") .append(" <ns2:ShortCode>800080008001</ns2:ShortCode>\n") .append(" <ns2:Number>9257089093</ns2:Number>\n") .append(" <ns2:Source>provider</ns2:Source>\n") .append(" <ns2:Message/>\n") .append(" </ns2:OptinReq>\n") .append(" </S:Body>\n") .append("</S:Envelope>"); */ // simple send sms StringBuilder string1 = new StringBuilder(200).append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n") .append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:loc=\"http://www.csapi.org/schema/parlayx/sms/send/v2_3/local\">\n") .append(" <soapenv:Header/>\n").append(" <soapenv:Body>\n").append(" <loc:sendSms>\n") .append(" <loc:addresses>tel:+16472260233</loc:addresses>\n") .append(" <loc:senderName>6388</loc:senderName>\n") .append(" <loc:message>Test Message &</loc:message>\n").append(" </loc:sendSms>\n") .append(" </soapenv:Body>\n").append("</soapenv:Envelope>\n"); // startSmsNotification - place to deliver SMS to String req = string1.toString(); logger.debug("Request XML -> \n" + req); HttpPost post = new HttpPost(strURL); StringEntity postEntity = new StringEntity(req, "ISO-8859-1"); postEntity.setContentType("text/xml; charset=\"ISO-8859-1\""); post.addHeader("SOAPAction", "\"\""); post.setEntity(postEntity); long start = System.currentTimeMillis(); client.getCredentialsProvider().setCredentials(new AuthScope("209.226.31.233", AuthScope.ANY_PORT), new UsernamePasswordCredentials("Twitter", "Twitter123")); BasicHttpContext localcontext = new BasicHttpContext(); // Generate BASIC scheme object and stick it to the local // execution context BasicScheme basicAuth = new BasicScheme(); localcontext.setAttribute("preemptive-auth", basicAuth); // Add as the first request interceptor client.addRequestInterceptor(new PreemptiveAuth(), 0); HttpResponse httpResponse = client.execute(post, localcontext); HttpEntity responseEntity = httpResponse.getEntity(); // // was the request OK? // if (httpResponse.getStatusLine().getStatusCode() != 200) { logger.error("Request failed with StatusCode=" + httpResponse.getStatusLine().getStatusCode()); } // get an input stream String responseBody = EntityUtils.toString(responseEntity); long stop = System.currentTimeMillis(); logger.debug("----------------------------------------"); logger.debug("Response took " + (stop - start) + " ms"); logger.debug(responseBody); logger.debug("----------------------------------------"); // } // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources client.getConnectionManager().shutdown(); }
From source file:com.emc.cto.ridagent.rid.test.TestScript.java
public static void main(String args[]) throws SAXException, ParserConfigurationException, URISyntaxException, ClientProtocolException, IOException { String xmlData = " <iodef-rid:RID lang=\"en\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:iodef-rid=\"urn:ietf:params:xml:ns:iodef-rid-2.0\" xmlns:iodef=\"urn:ietf:params:xml:ns:iodef-1.42\" xsi:schemaLocation=\"urn:ietf:params:xml:ns:iodef-rid-2.0 iodef-rid-2.0.xsd\">"; xmlData = xmlData + "<iodef-rid:RIDPolicy MsgType=\"Report\" MsgDestination=\"RIDSystem\">"; xmlData = xmlData + "<iodef-rid:PolicyRegion region=\"IntraConsortium\"/>"; xmlData = xmlData + " <iodef:Node>"; xmlData = xmlData + " <iodef:NodeName>192.168.1.1</iodef:NodeName>"; xmlData = xmlData + " </iodef:Node>"; xmlData = xmlData + "<iodef-rid:TrafficType type=\"Network\"/>"; xmlData = xmlData + "</iodef-rid:RIDPolicy>"; xmlData = xmlData + "</iodef-rid:RID>"; String id = TestScript.httpSend(xmlData, "https://ridtest.emc.com:4590/"); HttpGet httpget = new HttpGet("http://localhost:1280/federation/RID/" + id + "/report.xml"); DefaultHttpClient httpclient = new DefaultHttpClient(); Credentials defaultcreds = new UsernamePasswordCredentials("Administrator", "secret"); httpclient.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), defaultcreds);// w ww . ja v a2 s . c o m httpget.setHeader("User-Agent", "EMC RID System"); HttpResponse response = httpclient.execute(httpget); if (response.getEntity() != null) { int code = response.getStatusLine().getStatusCode(); if (code == 404) { System.out.println("Error has occured! Document not found in the xDB"); } else if (code == 200) { System.out.println("Document Successfully saved in the database"); } else { System.out.println("Error could not be determined"); } } }
From source file:se.anyro.tagtider.utils.Http.java
public static DefaultHttpClient getClient() { if (client == null) { client = new DefaultHttpClient(); Credentials creds = new UsernamePasswordCredentials("tagtider", "codemocracy"); AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM); client.getCredentialsProvider().setCredentials(scope, creds); }//from ww w. j av a 2s . c o m return client; }
From source file:org.springframework.cloud.stream.binder.rabbit.admin.RabbitManagementUtils.java
public static RestTemplate buildRestTemplate(String adminUri, String user, String password) { BasicCredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(user, password)); HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); // Set up pre-emptive basic Auth because the rabbit plugin doesn't currently support challenge/response for PUT // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local; from the apache docs... // auth cache BasicScheme basicAuth = new BasicScheme(); URI uri;/*from w w w .j av a2s .c o m*/ try { uri = new URI(adminUri); } catch (URISyntaxException e) { throw new RabbitAdminException("Invalid URI", e); } authCache.put(new HttpHost(uri.getHost(), uri.getPort(), uri.getScheme()), basicAuth); // Add AuthCache to the execution context final HttpClientContext localContext = HttpClientContext.create(); localContext.setAuthCache(authCache); RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient) { @Override protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { return localContext; } }); restTemplate.setMessageConverters( Collections.<HttpMessageConverter<?>>singletonList(new MappingJackson2HttpMessageConverter())); return restTemplate; }
From source file:com.datatorrent.stram.util.WebServicesClientTest.java
public static void checkUserCredentials(String username, String password, AuthScheme authScheme) throws NoSuchFieldException, IllegalAccessException { CredentialsProvider provider = getCredentialsProvider(); String httpScheme = AuthScope.ANY_SCHEME; if (authScheme == AuthScheme.BASIC) { httpScheme = AuthSchemes.BASIC;/*from w ww . j a v a 2 s. c o m*/ } else if (authScheme == AuthScheme.DIGEST) { httpScheme = AuthSchemes.DIGEST; } AuthScope authScope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM, httpScheme); Credentials credentials = provider.getCredentials(authScope); Assert.assertNotNull("Credentials", credentials); Assert.assertTrue("Credentials type is user", UsernamePasswordCredentials.class.isAssignableFrom(credentials.getClass())); UsernamePasswordCredentials pwdCredentials = (UsernamePasswordCredentials) credentials; Assert.assertEquals("Username", username, pwdCredentials.getUserName()); Assert.assertEquals("Password", password, pwdCredentials.getPassword()); }
From source file:com.mycompany.projecta.Test.java
public void Auth() throws Exception { // Credentials String username = "fernandoadp"; String password = "ADP.2017"; // Jenkins url String jenkinsUrl = "http://localhost:8080"; // Build name String jobName = "ServiceA"; // Build token String buildToken = "build"; // Create your httpclient DefaultHttpClient client = new DefaultHttpClient(); // Then provide the right credentials ******************* client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new org.apache.http.auth.UsernamePasswordCredentials(username, password)); //client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), (Credentials) new UsernamePasswordCredentials(username, password)); // Generate BASIC scheme object and stick it to the execution context BasicScheme basicAuth = new BasicScheme(); BasicHttpContext 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); // You get request that will start the build String getUrl = jenkinsUrl + "/job/" + jobName + "/build?token=" + buildToken; HttpGet get = new HttpGet(getUrl); try {/* w ww . j a v a 2 s . c o m*/ // Execute your request with the given context HttpResponse response = client.execute(get, context); HttpEntity entity = response.getEntity(); EntityUtils.consume(entity); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:de.cuseb.bilderbuch.helpers.HttpClientProviderService.java
public HttpClient getHttpClientWithBasicAuth(String username, String password) { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(username, password)); return HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); }
From source file:org.wso2.developerstudio.appfactory.core.client.HttpsJenkinsClient.java
public static HttpResponse getBulildinfo(String applicationId, String version, String builderBaseUrl, int lastBuildNo) { DefaultHttpClient client = new DefaultHttpClient(); client = (DefaultHttpClient) HttpsJaggeryClient.wrapClient(client, builderBaseUrl); client.getCredentialsProvider().setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(Authenticator.getInstance().getCredentials().getUser(), Authenticator.getInstance().getCredentials().getPassword())); // Generate BASIC scheme object and stick it to the execution context BasicScheme basicAuth = new BasicScheme(); BasicHttpContext 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); String getUrl = builderBaseUrl + "/job/" + applicationId + "-" + version + "-default/" + lastBuildNo + "/consoleText"; HttpGet get = new HttpGet(getUrl); try {/*from w w w . ja va2s .c o m*/ // Execute your request with the given context HttpResponse response = client.execute(get, context); return response; } catch (Exception e) { log.error("Jenkins Client err", e); } return null; }
From source file:net.evendanan.android.thumbremote.network.ReusableHttpClientBlocking.java
ReusableHttpClientBlocking(int timeout, String user, String password) { Log.d(TAG, "Creating a new HTTP client"); mHttpClient = new DefaultHttpClient(); if (!TextUtils.isEmpty(password)) { CredentialsProvider credProvider = new BasicCredentialsProvider(); credProvider.setCredentials(new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials(user, password)); mHttpClient.setCredentialsProvider(credProvider); }//ww w.j a v a 2s.c o m mRequest = new HttpGet(); mRequest.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, new Integer(timeout)); mRequest.getParams().setParameter(CoreConnectionPNames.SO_LINGER, new Integer(timeout / 2)); mRequest.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, new Integer(timeout)); }