Skip to content

Instantly share code, notes, and snippets.

@tsurdilo
Created June 8, 2011 13:37
Show Gist options
  • Save tsurdilo/1014429 to your computer and use it in GitHub Desktop.
Save tsurdilo/1014429 to your computer and use it in GitHub Desktop.
private InputStream getInputStreamForURL(String urlLocation, IDiagramProfile profile) throws Exception{
// pretend we are mozilla
URL url = new URL(urlLocation);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection
.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16");
connection
.setRequestProperty("Accept",
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
connection.setRequestProperty("Accept-Language", "en-us,en;q=0.5");
connection.setRequestProperty("Accept-Encoding", "gzip,deflate");
connection.setRequestProperty("charset", "UTF-8");
connection.setReadTimeout(5 * 1000);
if(profile.getUsr() != null && profile.getUsr().trim().length() > 0 && profile.getPwd() != null && profile.getPwd().trim().length() > 0) {
BASE64Encoder enc = new sun.misc.BASE64Encoder();
String userpassword = profile.getUsr() + ":" + profile.getPwd();
String encodedAuthorization = enc.encode( userpassword.getBytes() );
connection.setRequestProperty("Authorization", "Basic "+ encodedAuthorization);
}
connection.connect();
BufferedReader sreader = new BufferedReader(new InputStreamReader(
connection.getInputStream(), "UTF-8"));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
while ((line = sreader.readLine()) != null) {
stringBuilder.append(line + "\n");
}
return new ByteArrayInputStream(stringBuilder.toString()
.getBytes("UTF-8"));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment