My requirement is to do web application to get destination content and have to add request header by using HttpURLConnection.
I use this code
try{
String urlStr = "http://test/STAM/Login";
url = new URL(urlStr);
urlconn = (HttpURLConnection)url.openConnection();
urlconn.setRequestProperty("Accept-Language","en-us,en;q=0.5");
urlconn.setRequestProperty("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");
urlconn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
urlconn.connect();
//read the result from the server
rd = new BufferedReader(new InputStreamReader(urlconn.getInputStream()));
sb = new StringBuilder();
while ((line = rd.readLine()) != null)
{
sb.append(line + '\n');
}
out.println(sb.toString());
} catch(Exception e) {
e.printStackTrace();
} finally {
//close the connection, set all objects to null
urlconn.disconnect();
rd = null;
sb = null;
wr = null;
urlconn = null;
}
Result is: My code will get only source content but cannot get css, image file, javascript and so on because it will load from local host such as "http://localhost:8080/test/img/test.gif" instead of "http://test/STAM/img/test.gif". Please advise me if I'm wrong or you have any way to solve this problem.
Thanks a lot.