Created
August 15, 2014 14:29
-
-
Save kbhaines/cca8c4c66c067f89c0be to your computer and use it in GitHub Desktop.
Doing URL fetch with Basic authorization from AppEngine
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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