Skip to content

Instantly share code, notes, and snippets.

@mpen
Created October 30, 2009 10:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpen/222254 to your computer and use it in GitHub Desktop.
Save mpen/222254 to your computer and use it in GitHub Desktop.
Apache HttpComponents HttpClient Get ResponseHandler
// Apache HttpComponents HttpClient Get ResponseHandler
import java.io.File
import java.io.IOException
import java.net.URI
import java.net.URISyntaxException
import java.net.URLDecoder
import java.net.UnknownHostException
import org.apache.commons.io.FileUtils
import org.apache.commons.io.FilenameUtils
import org.apache.http.HttpResponse
import org.apache.http.HttpStatus
import org.apache.http.client.ResponseHandler
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.util.EntityUtils
class DownloadResponseHandler(uri: URI) extends ResponseHandler[Boolean] {
override def handleResponse(response: HttpResponse): Boolean = {
try {
if (response.getStatusLine.getStatusCode == HttpStatus.SC_OK) {
val decodedPath = URLDecoder.decode(uri.getPath, "UTF-8")
var fileName = FilenameUtils.getName(decodedPath)
if (fileName == "") fileName = "index.html"
val file = new File("test-" + fileName)
val ba = EntityUtils.toByteArray(response.getEntity)
FileUtils.writeByteArrayToFile(file, ba)
printf("ダウンロード : %s : %s (%1.1f KB)%n", file.getName, uri, ba.length/1024.0)
}
return true
} catch {
case e: IOException => e.printStackTrace
}
return false
}
}
object ApacheHttpClientGet2 {
def main(args: Array[String]): Unit = {
val pages = List(
"http://www.yubin-nenga.jp/design_kit/hagaki_temp.html",
"https://www.google.com/accounts/Login?hl=ja",
"http://www.yahoo.co.jp/",
"http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8"
)
val client = new DefaultHttpClient
pages.foreach { page =>
try {
val uri = new URI(page)
val get = new HttpGet(uri)
client.execute[Boolean](get, new DownloadResponseHandler(uri))
} catch {
case e: UnknownHostException => e.printStackTrace
case e: IOException => e.printStackTrace
case e: URISyntaxException => e.printStackTrace
}
}
client.getConnectionManager.shutdown
}
}
// Apache HttpComponents HttpClient Get ResponseHandler
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.UnknownHostException;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
class DownloadResponseHandler implements ResponseHandler<Boolean> {
private URI uri;
DownloadResponseHandler(URI uri) {
this.uri = uri;
}
@Override
public Boolean handleResponse(HttpResponse response) {
try {
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String decodedPath = URLDecoder.decode(uri.getPath(), "UTF-8");
String fileName = FilenameUtils.getName(decodedPath);
if (fileName.equals("")) fileName = "index.html";
File file = new File("test-" + fileName);
byte[] ba = EntityUtils.toByteArray(response.getEntity());
FileUtils.writeByteArrayToFile(file, ba);
System.out.printf("ダウンロード : %s : %s (%1.1f KB)%n", file.getName(), uri, ba.length/1024.0);
}
return Boolean.TRUE;
} catch (IOException e) {
e.printStackTrace();
}
return Boolean.FALSE;
}
}
public class JavaApacheHttpClientGet2 {
public static void main(String[] args) {
String[] pages = {
"http://www.yubin-nenga.jp/design_kit/hagaki_temp.html",
"https://www.google.com/accounts/Login?hl=ja",
"http://www.yahoo.co.jp/",
"http://ja.wikipedia.org/wiki/%E3%83%A1%E3%82%A4%E3%83%B3%E3%83%9A%E3%83%BC%E3%82%B8",
};
DefaultHttpClient client = new DefaultHttpClient();
for (int i=0; i<pages.length; i++) {
String page = pages[i];
try {
URI uri = new URI(page);
HttpGet get = new HttpGet(uri);
client.execute(get, new DownloadResponseHandler(uri));
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
client.getConnectionManager().shutdown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment