List of usage examples for java.net URL URL
public URL(String spec) throws MalformedURLException
From source file:ParseURL.java
public static void main(String[] args) throws Exception { URL aURL = new URL("http://java.sun.com:80/docs/books/" + "tutorial/index.html#DOWNLOADING"); System.out.println("protocol = " + aURL.getProtocol()); System.out.println("host = " + aURL.getHost()); System.out.println("filename = " + aURL.getFile()); System.out.println("port = " + aURL.getPort()); System.out.println("ref = " + aURL.getRef()); }
From source file:course.wicket.app.Start.java
public static void main(String[] args) { Server jettyServer = null;/* w ww . j ava 2 s . c o m*/ try { URL jettyConfig = new URL("file:" + findJettyConfigPath()); if (jettyConfig != null) { jettyServer = new Server(jettyConfig); jettyServer.start(); } } catch (Exception e) { log.fatal("Could not start the Jetty server: " + e); } }
From source file:OldExtractor.java
/** * @param args the command line arguments *//*from w w w . j a v a2 s . c o m*/ public static void main(String[] args) { // TODO code application logic here String bingUrl = "https://api.datamarket.azure.com/Bing/Search/Web?$top=10&$format=Atom&Query=%27gates%27"; //Provide your account key here. String accountKey = "ghTYY7wD6LpyxUO9VRR7e1f98WFhHWYERMcw87aQTqQ"; // String accountKey = "xqbCjT87/MQz25JWdRzgMHdPkGYnOz77IYmP5FUIgC8"; byte[] accountKeyBytes = Base64.encodeBase64((accountKey + ":" + accountKey).getBytes()); String accountKeyEnc = new String(accountKeyBytes); try { URL url = new URL(bingUrl); URLConnection urlConnection = url.openConnection(); urlConnection.setRequestProperty("Authorization", "Basic " + accountKeyEnc); InputStream inputStream = (InputStream) urlConnection.getContent(); byte[] contentRaw = new byte[urlConnection.getContentLength()]; inputStream.read(contentRaw); String content = new String(contentRaw); //System.out.println(content); try { File file = new File("Results.xml"); FileWriter fileWriter = new FileWriter(file); fileWriter.write(content); //fileWriter.write("a test"); fileWriter.flush(); fileWriter.close(); } catch (IOException e) { System.out.println(e); } DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { //System.out.println("here"); //Using factory get an instance of document builder DocumentBuilder db = dbf.newDocumentBuilder(); //parse using builder to get DOM representation of the XML file Document dom = db.parse("Results.xml"); Element docEle = (Element) dom.getDocumentElement(); //get a nodelist of elements NodeList nl = docEle.getElementsByTagName("d:Url"); if (nl != null && nl.getLength() > 0) { for (int i = 0; i < nl.getLength(); i++) { //get the employee element Element el = (Element) nl.item(i); // System.out.println("here"); System.out.println(el.getTextContent()); //get the Employee object //Employee e = getEmployee(el); //add it to list //myEmpls.add(e); } } NodeList n2 = docEle.getElementsByTagName("d:Title"); if (n2 != null && n2.getLength() > 0) { for (int i = 0; i < n2.getLength(); i++) { //get the employee element Element e2 = (Element) n2.item(i); // System.out.println("here"); System.out.println(e2.getTextContent()); //get the Employee object //Employee e = getEmployee(el); //add it to list //myEmpls.add(e); } } NodeList n3 = docEle.getElementsByTagName("d:Description"); if (n3 != null && n3.getLength() > 0) { for (int i = 0; i < n3.getLength(); i++) { //get the employee element Element e3 = (Element) n3.item(i); // System.out.println("here"); System.out.println(e3.getTextContent()); //get the Employee object //Employee e = getEmployee(el); //add it to list //myEmpls.add(e); } } } catch (SAXException se) { se.printStackTrace(); } catch (ParserConfigurationException pe) { pe.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } catch (IOException e) { System.out.println(e); } //The content string is the xml/json output from Bing. }
From source file:net.rptools.maptool.client.WebDownloader.java
public static void main(String[] args) throws Exception { WebDownloader downloader = new WebDownloader(new URL("http://library.rptools.net/1.3/listArtPacks")); String result = downloader.read(); System.out.println(result);//from ww w. ja v a2 s . c o m }
From source file:URLReader.java
public static void main(String[] args) throws Exception { URL yahoo = new URL("http://www.yahoo.com/"); BufferedReader in = new BufferedReader(new InputStreamReader(yahoo.openStream())); String inputLine;//ww w . ja v a 2 s . c o m while ((inputLine = in.readLine()) != null) System.out.println(inputLine); in.close(); }
From source file:info.bitoo.utils.BiToorrentRemaker.java
public static void main(String[] args) throws IOException, TOTorrentException { CommandLineParser parser = new PosixParser(); CommandLine cmd = null;/*www . ja va 2s. c o m*/ try { cmd = parser.parse(createCommandLineOptions(), args); } catch (ParseException e) { System.err.println("Parsing failed. Reason: " + e.getMessage()); } StringTokenizer stLocations = new StringTokenizer(cmd.getOptionValue("l"), "|"); List locations = new ArrayList(stLocations.countTokens()); while (stLocations.hasMoreTokens()) { URL locationURL = new URL((String) stLocations.nextToken()); locations.add(locationURL.toString()); } String torrentFileName = cmd.getOptionValue("t"); File torrentFile = new File(torrentFileName); TOTorrentDeserialiseImpl torrent = new TOTorrentDeserialiseImpl(torrentFile); torrent.setAdditionalListProperty("alternative locations", locations); torrent.serialiseToBEncodedFile(torrentFile); }
From source file:org.wso2.charon3.samples.group.sample01.CreateGroupSample.java
public static void main(String[] args) { try {/*from w ww .j a va 2s .c o m*/ String url = "http://localhost:8080/scim/v2/Groups"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // Setting basic post request con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", "application/scim+json"); // Send post request con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(createRequestBody); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); BufferedReader in; if (responseCode == HttpURLConnection.HTTP_CREATED) { // success in = new BufferedReader(new InputStreamReader(con.getInputStream())); } else { in = new BufferedReader(new InputStreamReader(con.getErrorStream())); } String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); //printing result from response System.out.println("Response Code : " + responseCode); System.out.println("Response Message : " + con.getResponseMessage()); System.out.println("Response Content : " + response.toString()); } catch (ProtocolException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:main.java.demo.jaxrs.client.Client.java
public static void main(String args[]) throws Exception { // Sent HTTP GET request to query all customer info /*//from ww w .j a va 2 s . c o m * URL url = new URL("http://localhost:9000/customers"); * System.out.println("Invoking server through HTTP GET to query all * customer info"); InputStream in = url.openStream(); StreamSource * source = new StreamSource(in); printSource(source); */ // Sent HTTP GET request to query customer info System.out.println("Sent HTTP GET request to query customer info"); URL url = new URL("http://localhost:8989/RestFull/service1/customerservice/customers/123"); InputStream in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP GET request to query sub resource product info System.out.println("\n"); System.out.println("Sent HTTP GET request to query sub resource product info"); url = new URL("http://localhost:9000/customerservice/orders/223/products/323"); in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP PUT request to update customer info System.out.println("\n"); System.out.println("Sent HTTP PUT request to update customer info"); Client client = new Client(); String inputFile = client.getClass().getResource("update_customer.xml").getFile(); URIResolver resolver = new URIResolver(inputFile); File input = new File(resolver.getURI()); PutMethod put = new PutMethod("http://localhost:9000/customerservice/customers"); RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); put.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(put); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(put.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done put.releaseConnection(); } // Sent HTTP POST request to add customer System.out.println("\n"); System.out.println("Sent HTTP POST request to add customer"); inputFile = client.getClass().getResource("add_customer.xml").getFile(); resolver = new URIResolver(inputFile); input = new File(resolver.getURI()); PostMethod post = new PostMethod("http://localhost:9000/customerservice/customers"); post.addRequestHeader("Accept", "text/xml"); entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); post.setRequestEntity(entity); httpclient = new HttpClient(); try { int result = httpclient.executeMethod(post); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(post.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done post.releaseConnection(); } System.out.println("\n"); System.exit(0); }
From source file:edu.techseekers.training.client.Client.java
public static void main(String args[]) throws Exception { // Sent HTTP GET request to query all customer info /*//from www .j a v a 2s .c o m * URL url = new URL("http://localhost:9000/customers"); * System.out.println("Invoking server through HTTP GET to query all * customer info"); InputStream in = url.openStream(); StreamSource * source = new StreamSource(in); printSource(source); */ // Sent HTTP GET request to query customer info System.out.println("Sent HTTP GET request to query customer info"); URL url = new URL("http://localhost:9000/customerservice/customers/123"); InputStream in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP GET request to query sub resource product info System.out.println("\n"); System.out.println("Sent HTTP GET request to query sub resource product info"); url = new URL("http://localhost:9000/customerservice/orders/223/products/323"); in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP PUT request to update customer info System.out.println("\n"); System.out.println("Sent HTTP PUT request to update customer info"); Client client = new Client(); String inputFile = client.getClass().getResource("/update_customer.xml").getFile(); URIResolver resolver = new URIResolver(inputFile); File input = new File(resolver.getURI()); PutMethod put = new PutMethod("http://localhost:9000/customerservice/customers"); RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); put.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(put); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(put.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done put.releaseConnection(); } // Sent HTTP POST request to add customer System.out.println("\n"); System.out.println("Sent HTTP POST request to add customer"); inputFile = client.getClass().getResource("/add_customer.xml").getFile(); resolver = new URIResolver(inputFile); input = new File(resolver.getURI()); PostMethod post = new PostMethod("http://localhost:9000/customerservice/customers"); post.addRequestHeader("Accept", "text/xml"); entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); post.setRequestEntity(entity); httpclient = new HttpClient(); try { int result = httpclient.executeMethod(post); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(post.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done post.releaseConnection(); } System.out.println("\n"); System.exit(0); }
From source file:main.java.client.Client.java
public static void main(String args[]) throws Exception { // Sent HTTP GET request to query all customer info /*// www . j a v a2s. c om * URL url = new URL("http://localhost:9000/customers"); * System.out.println("Invoking server through HTTP GET to query all * customer info"); InputStream in = url.openStream(); StreamSource * source = new StreamSource(in); printSource(source); */ // Sent HTTP GET request to query customer info System.out.println("Sent HTTP GET request to query customer info"); URL url = new URL("http://localhost:8080/authenticate/customers/123"); InputStream in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP GET request to query sub resource product info System.out.println("\n"); System.out.println("Sent HTTP GET request to query sub resource product info"); url = new URL("http://localhost:9000/authenticate/orders/223/products/323"); in = url.openStream(); System.out.println(getStringFromInputStream(in)); // Sent HTTP PUT request to update customer info System.out.println("\n"); System.out.println("Sent HTTP PUT request to update customer info"); Client client = new Client(); String inputFile = client.getClass().getResource("/update_customer.xml").getFile(); URIResolver resolver = new URIResolver(inputFile); File input = new File(resolver.getURI()); PutMethod put = new PutMethod("http://localhost:9000/authenticate/customers"); RequestEntity entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); put.setRequestEntity(entity); HttpClient httpclient = new HttpClient(); try { int result = httpclient.executeMethod(put); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(put.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done put.releaseConnection(); } // Sent HTTP POST request to add customer System.out.println("\n"); System.out.println("Sent HTTP POST request to add customer"); inputFile = client.getClass().getResource("/add_customer.xml").getFile(); resolver = new URIResolver(inputFile); input = new File(resolver.getURI()); PostMethod post = new PostMethod("http://localhost:9000/authenticate/customers"); post.addRequestHeader("Accept", "text/xml"); entity = new FileRequestEntity(input, "text/xml; charset=ISO-8859-1"); post.setRequestEntity(entity); httpclient = new HttpClient(); try { int result = httpclient.executeMethod(post); System.out.println("Response status code: " + result); System.out.println("Response body: "); System.out.println(post.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done post.releaseConnection(); } // Sent HTTP POST request to add json customer System.out.println("\n"); System.out.println("Sent HTTP POST request to add json customer"); String inputFilej = client.getClass().getResource("/add_customer.json").getFile(); URIResolver resolverj = new URIResolver(inputFilej); File inputj = new File(resolverj.getURI()); PostMethod postj = new PostMethod("http://localhost:9000/authenticate/customersjson"); postj.addRequestHeader("Accept", "application/json"); RequestEntity entityj = new FileRequestEntity(inputj, "application/json; charset=ISO-8859-1"); postj.setRequestEntity(entityj); HttpClient httpclientj = new HttpClient(); try { int resultj = httpclientj.executeMethod(postj); System.out.println("Response status code json: " + resultj); System.out.println("Response body json: "); System.out.println(postj.getResponseBodyAsString()); } finally { // Release current connection to the connection pool once you are // done postj.releaseConnection(); } System.out.println("\n"); System.exit(0); }