Here is a simple example how to post a URL to an application deployed somewhere in an application server from a simple java class. You just need to have jdk installed to run this program.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class XMLLoader {
public void sendPostRequest() throws Exception {
try {
// create a URL to be fired
URL url = new URL("http://10.10.99.19:8000/Test/StudentData");
// open a Connection
URLConnection connection = url.openConnection();
// set the username:pwd
String encoding = new sun.misc.BASE64Encoder().encode("username:password".getBytes());
// set the authorization as Basic
connection.setRequestProperty("Authorization", "Basic " + encoding);
// get the inputstream from the connection
InputStream inputStream = connection.getInputStream();
// Parse the stream to get the text of it
String responseXml = new XMLLoader().readAllAsText(inputStream);
System.out.println("The response is --- " +responseXml );
} catch(Exception e) {
e.printStackTrace();
}
}
/**
* Starts the program
*/
public static void main(String[] args) throws Exception {
new XMLLoader().sendPostRequest();
}
public final static char EOL='\n';
/**
* Helper methods
*/
public String readAllAsText(InputStream stream) throws Exception {
InputStreamReader reader=null;
try {
reader=new InputStreamReader(stream);
return readAllAsText(reader);
} finally {
if(reader != null)
reader.close();
}
}
public String readAllAsText(Reader reader) throws Exception {
BufferedReader bReader=null;
try {
bReader=new BufferedReader(reader);
StringBuffer buf=new StringBuffer();
String line=bReader.readLine();
if (null!=line) {
buf.append(line);
}
while ((line=bReader.readLine()) != null) {
buf.append(EOL);
buf.append(line);
}
return buf.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if(bReader != null)
bReader.close();
}
}
}
SpringBoot: Features: SpringApplication
4 years ago

1 comment:
...please where can I buy a unicorn?
Post a Comment