-
-
Save AravindaM/08a2388e183ffaf58e08885b3aa8bb1d to your computer and use it in GitHub Desktop.
Scala - HTTPComponents client example
This file contains 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
package com.semperos.example | |
import org.apache.http.impl.client.{BasicResponseHandler, DefaultHttpClient} | |
import org.apache.http.{HttpRequest, HttpRequestInterceptor} | |
import org.apache.http.client.methods.HttpGet | |
import org.apache.http.protocol.HttpContext | |
import org.apache.log4j.Logger | |
/** | |
* HTTP request handling | |
*/ | |
object Http { | |
val logger = Logger.getRootLogger | |
def simpleRequest(url: String): String = { | |
val client = new DefaultHttpClient | |
simpleRequest(client, url) | |
} | |
def simpleRequest(client: DefaultHttpClient, url: String): String = { | |
try { | |
val get = new HttpGet(url) | |
logger.info("executing request " + get.getURI); | |
val responseHandler = new BasicResponseHandler | |
client.execute(get, responseHandler) | |
} finally { | |
client.getConnectionManager.shutdown() | |
} | |
} | |
def requestWithHeaders(url: String, headers: Map[String,String]): String = { | |
val client = new DefaultHttpClient | |
try { | |
client.addRequestInterceptor(new HttpRequestInterceptor { | |
def process(request: HttpRequest, context: HttpContext) { | |
for ((k,v) <- headers) { | |
request.addHeader(k, v) | |
} | |
} | |
}) | |
simpleRequest(client, url) | |
} | |
} | |
def main(args: Array[String]) { | |
val response = requestWithHeaders("http://tools.ietf.org/rfc/rfc3629.txt", Map("SUBJECT_DN" -> "TEST_DN")) | |
println(response) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment