Skip to content

Instantly share code, notes, and snippets.

@kbhaines
Created August 15, 2014 14:29
Show Gist options
  • Save kbhaines/cca8c4c66c067f89c0be to your computer and use it in GitHub Desktop.
Save kbhaines/cca8c4c66c067f89c0be to your computer and use it in GitHub Desktop.
Doing URL fetch with Basic authorization from AppEngine
@GET
@Path("/urlfetch/{url}")
public Response urlFetch(@PathParam("url") String userUrl) {
String result = "";
try {
URL url = new URL("https://" + userUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Header is "Basic <base64 encoded username:password>"
// e.g. echo "kevin:password" | openssl env -base64 will give you this
connection.setRequestProperty("Authorization", "Basic a2V2aW46cGFzc3dvcmQK");
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
log.info("Fetched:" + line);
result+=line;
}
reader.close();
} catch (MalformedURLException e) {
// ...
log.info(e.toString());
} catch (IOException e) {
// ...
log.info(e.toString());
}
return Response.ok(result).build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment