Showing posts with label URL connection. Show all posts
Showing posts with label URL connection. Show all posts

Wednesday

URLs to Local Files






The URL class can also be used to access files in the local file system. Thus the URL class can be a handy way to open a file, if you need your code to not know whether the file came from the network or local file system.

Here is an example of how to open a file in the local file system using the URL class: URL url = new URL("file:/c:/data/test.txt");

URLConnection urlConnection = url.openConnection();
InputStream input = urlConnection.getInputStream();

int data = input.read();
while(data != -1){
System.out.print((char) data);
data = input.read();
}
input.close();


Notice how the only difference from accessing a file on a web server via HTTP is the the URL: "file:/c:/data/test.txt".

Tuesday

HTTP GET and POST



By default the URLConnection sends an HTTP GET request to the webserver. If you want to send an HTTP POST request instead, call the URLConnection.setDoOutput(true) method, like this:
URL url = new URL("https://vigyancode.blogspot.com/");
 
URLConnection urlConnection = url.openConnection();
urlConnection.setDoOutput(true);
Once you have set called setDoOutput(true) you can open the URLConnection's OutputStream like this:
OutputStream output = urlConnection.getOutputStream();
Using this OutputStream you can write any data you want in the body of the HTTP request.  
Remember to close the OutputStream when you are done writing data to it.