Thursday, 28 September 2017

Java URL


URL Encoding basic


import java.net.URLEncoder;

public class URLTesting {
public static void main(String args[]) throws Exception{
String para1Before="Java Programming";
String para1After=URLEncoder.encode(para1Before,"UTF-8");

System.out.println(para1Before);
System.out.println(para1After);


String para2Before = "Java, Programming Tutorial";

String para2After=URLEncoder.encode(para2Before, "UTF-8");

System.out.println(para2Before);
System.out.println(para2After);

}
}



Java Programming
Java+Programming
Java, Programming Tutorial
Java%2C+Programming+Tutorial


// space is replace by +
// , is replaced by %2c

--------------------------------------------------------------------------------------------------------------

URL Decoding


import java.net.URLDecoder;

public class URLDecodingTest {

public static void main(String args[]) throws Exception{
 
String para1="Java+Programming";
String para2="Java%2C+Programming+Tutorial";
 
String para1After=URLDecoder.decode(para1,"UTF-8");
         String para2After=URLDecoder.decode(para2,"UTF-8");
         
         System.out.println(para1After);
         System.out.println(para2After);
 
}

}


Java Programming
Java, Programming Tutorial


--------------------------------------------------------------------------------------------------------------

Reading directly from URL


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class ReadingFromURL {

public static void main(String args[]) throws Exception{

URL url =new URL("http://www.oracle.com/");
BufferedReader br =new BufferedReader(
new InputStreamReader(url.openStream()));

String inputLine;

while((inputLine=br.readLine())!=null);
System.out.println(inputLine);

br.close();
}

}


--------------------------------------------------------------------------------------------------------------

Convert URI to URL

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

public class ConvertURItoURL {

public static void main(String args[]) throws URISyntaxException, MalformedURLException{

URI uri =new URI("http", "java.com", "/hello world/", "");

URL url=uri.toURL();
System.out.println(url.toString());
}
}




java.net.URI class to automatically take care of the encoding 


--------------------------------------------------------------------------------------------------------------

Parsing URL

import java.net.MalformedURLException;
import java.net.URL;

public class ParseURL {

public static void main(String args[]) throws MalformedURLException{

URL url =new URL("http://java.com:80/docs/books/tutorial"
                           + "/index.html?name=networking#DOWNLOADING");




     System.out.println(url.getProtocol());

     System.out.println(url.getAuthority());

     System.out.println(url.getHost());

     System.out.println(url.getPort());

     System.out.println(url.getPath());

     System.out.println(url.getQuery());

     System.out.println(url.getFile());

     System.out.println(url.getRef());
    
}

}




http
java.com:80
java.com
80
/docs/books/tutorial/index.html
name=networking
/docs/books/tutorial/index.html?name=networking

DOWNLOADING


--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------

https://docs.oracle.com/javase/tutorial/networking/urls/index.html

No comments:

Post a Comment