Difference between url and uri bael 864 hariprasad net (#2108)
* BAEL-864, java.net.* * java-networking Bael-864, revision * Final and static keywords removed. * Bael-486 Static import for JUnit into pom.xml was added. * Bael-486, pom.xml was renamed on networking-pom.xml to deactivate it. * Maven pom.xml changed. * Delete java-networing project. * Bael-684, rearanged.
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.javanetworking.url;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class URLDemo {
|
||||
private static Logger log = LoggerFactory.getLogger(URLDemo.class);
|
||||
|
||||
String URLSTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484";
|
||||
// parsed locator
|
||||
String URLPROTOCOL = "https";
|
||||
// final static String URLAUTHORITY = "wordpress.org:443";
|
||||
String URLHOST = "wordpress.org";
|
||||
String URLPATH = "/support/topic/page-jumps-within-wordpress/";
|
||||
// final static String URLFILENAME = "/support/topic/page-jumps-within-wordpress/?replies=3";
|
||||
// final static int URLPORT = 443;
|
||||
int URLDEFAULTPORT = 443;
|
||||
String URLQUERY = "replies=3";
|
||||
String URLREFERENCE = "post-2278484";
|
||||
String URLCOMPOUND = URLPROTOCOL + "://" + URLHOST + ":" + URLDEFAULTPORT + URLPATH + "?" + URLQUERY + "#" + URLREFERENCE;
|
||||
|
||||
URL url;
|
||||
URLConnection urlConnection = null;
|
||||
HttpURLConnection connection = null;
|
||||
BufferedReader in = null;
|
||||
String urlContent = "";
|
||||
|
||||
public String testURL(String urlString) throws IOException, IllegalArgumentException {
|
||||
String urlStringCont = "";
|
||||
// comment the if clause if experiment with URL
|
||||
/*if (!URLSTRING.equals(urlString)) {
|
||||
throw new IllegalArgumentException("URL String argument is not proper: " + urlString);
|
||||
}*/
|
||||
// creating URL object
|
||||
url = new URL(urlString);
|
||||
// get URL connection
|
||||
urlConnection = url.openConnection();
|
||||
connection = null;
|
||||
// we can check, if connection is proper type
|
||||
if (urlConnection instanceof HttpURLConnection) {
|
||||
connection = (HttpURLConnection) urlConnection;
|
||||
} else {
|
||||
log.info("Please enter an HTTP URL");
|
||||
throw new IOException("HTTP URL is not correct");
|
||||
}
|
||||
// we can check response code (200 OK is expected)
|
||||
log.info(connection.getResponseCode() + " " + connection.getResponseMessage());
|
||||
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
|
||||
String current;
|
||||
|
||||
while ((current = in.readLine()) != null) {
|
||||
urlStringCont += current;
|
||||
}
|
||||
return urlStringCont;
|
||||
}
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
URLDemo demo = new URLDemo();
|
||||
String content = demo.testURL(demo.URLCOMPOUND);
|
||||
log.info(content);
|
||||
}
|
||||
}
|
||||
9
core-java/src/main/java/log4j.properties
Normal file
9
core-java/src/main/java/log4j.properties
Normal file
@@ -0,0 +1,9 @@
|
||||
# Set root logger level to DEBUG and its only appender to A1.
|
||||
log4j.rootLogger=DEBUG, A1
|
||||
|
||||
# A1 is set to be a ConsoleAppender.
|
||||
log4j.appender.A1=org.apache.log4j.ConsoleAppender
|
||||
|
||||
# A1 uses PatternLayout.
|
||||
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
|
||||
Reference in New Issue
Block a user