本文整理汇总了Scala中org.apache.http.client.methods.HttpGet类的典型用法代码示例。如果您正苦于以下问题:Scala HttpGet类的具体用法?Scala HttpGet怎么用?Scala HttpGet使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了HttpGet类的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: TempasWaybackSpec
//设置package包名称以及导入依赖的类
package de.l3s.archivespark.specific.warc.tempas
import java.net.URLEncoder
import de.l3s.archivespark.dataspecs.DataSpec
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
import org.apache.spark.SparkContext
import org.apache.spark.rdd.RDD
import scala.io.Source
class TempasWaybackSpec private (query: String, from: Option[Int] = None, to: Option[Int] = None, pages: Int, resultsPerPage: Int) extends DataSpec[TempasYearResult, TempasWaybackRecord] {
import TempasWaybackSpec._
def searchUrl(page: Int): String = {
val queryEncoded = URLEncoder.encode(query, "UTF-8")
var url = TempasSearchUrl.replace("$r", resultsPerPage.toString).replace("$p", page.toString).replace("$q", queryEncoded)
if (from.isDefined) url += "&from=" + from.get
if (to.isDefined) url += "&to=" + to.get
url
}
override def load(sc: SparkContext, minPartitions: Int): RDD[TempasYearResult] = {
sc.parallelize(1 to pages, minPartitions).flatMap { page =>
@transient val client = HttpClients.createDefault
@transient val get = new HttpGet(searchUrl(page))
get.setHeader("Accept", AcceptType)
val in = client.execute(get).getEntity.getContent
try {
Source.fromInputStream(in).getLines().toList.flatMap { line =>
TempasYearResult.resultsFromTsv(line)
}
} finally {
in.close()
}
}.repartition(minPartitions)
}
override def parse(result: TempasYearResult): Option[TempasWaybackRecord] = {
Some(new TempasWaybackRecord(result))
}
}
object TempasWaybackSpec {
val TempasSearchUrl = "http://tempas.l3s.de/v2/query?resultsPerPage=$r&page=$p&q=$q"
val DefaultResultsPerPage = 100
val DefaultPages = 100
val AcceptType = "text/tab-separated-values"
def apply(query: String, from: Int = -1, to: Int = -1, pages: Int = DefaultPages, resultsPerPage: Int = DefaultResultsPerPage): TempasWaybackSpec = {
val fromOpt = if (from < 0) None else Some(from)
val toOpt = if (to < 0) None else Some(to)
new TempasWaybackSpec(query, fromOpt, toOpt, pages, resultsPerPage)
}
}
示例2: JSONHTTP
//设置package包名称以及导入依赖的类
package works.weave.socks.aws.orders.dataaccess.web
import com.amazonaws.util.IOUtils
import com.fasterxml.jackson.core.`type`.TypeReference
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.net.URI
import javax.xml.ws.http.HTTPException
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
import org.slf4j.LoggerFactory
import works.weave.socks.aws.orders.ProjectDefaultJacksonMapper
object JSONHTTP {
val Log = LoggerFactory.getLogger(getClass)
val objectMapper = ProjectDefaultJacksonMapper.build() // after (_.bla)
def get[T : Manifest : NotNothing](uri : URI) : T = {
val client = HttpClientBuilder.create.build
val get = new HttpGet(uri)
get.addHeader("Accept", "application/json")
val response = client.execute(get)
val responseString = IOUtils.toString(response.getEntity.getContent)
Log.info(s"Got status ${response.getStatusLine.getStatusCode}")
if (response.getStatusLine.getStatusCode == 200) {
Log.info(s"Got response from URI $uri: $responseString")
objectMapper.readValue(responseString, typeReference[T])
} else {
throw new HTTPException(response.getStatusLine.getStatusCode)
}
}
def typeReference[T : Manifest] = new TypeReference[T] {
override def getType = typeFromManifest(manifest[T])
}
def typeFromManifest(m : Manifest[_]) : Type = {
if (m.typeArguments.isEmpty) { m.runtimeClass }
else new ParameterizedType {
def getRawType = m.runtimeClass
def getActualTypeArguments = m.typeArguments.map(typeFromManifest).toArray
def getOwnerType = null
}
}
// Trick to disable nothign for type param
sealed trait NotNothing[-T]
object NotNothing {
implicit object notNothing extends NotNothing[Any]
implicit object `The error is because the missing type parameter was resolved to Nothing` extends NotNothing[Nothing]
}
}
示例3: verify
//设置package包名称以及导入依赖的类
package com.malliina.aws.cognito
import java.net.URI
import java.nio.charset.StandardCharsets
import com.amazonaws.regions.Regions
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.{CloseableHttpClient, HttpClients}
import org.apache.http.util.EntityUtils
import play.api.libs.json.Json
trait Verifier {
def verify(token: RawToken): Either[JWTError, VerifiedToken]
def verifyAccess(token: RawAccessToken): Either[JWTError, AccessToken] =
verify(token).right.flatMap(JWT.access)
def verifyId(token: RawIDToken): Either[JWTError, IdToken] =
verify(token).right.flatMap(JWT.id)
}
class TokenVerifier(publicKeys: Seq[PubKey],
val expectedIssuer: Issuer) extends Verifier {
override def verify(token: RawToken): Either[JWTError, VerifiedToken] =
JWT.verifyToken(token, expectedIssuer, publicKeys)
}
object TokenVerifier {
def forUserPool(region: Regions, userPool: UserPool): TokenVerifier = {
val keySetUri = jwtSet(region, userPool)
val keys = Json.parse(fetch(keySetUri)).as[JWTKeys].keys map { key =>
PubKey(key.kid, Keys.publicKey(key))
}
val expectedIssuer = Issuer(s"https://cognito-idp.${region.getName}.amazonaws.com/$userPool")
new TokenVerifier(keys, expectedIssuer)
}
protected def jwtSet(region: Regions, userPool: UserPool): URI =
new URI(s"https://cognito-idp.${region.getName}.amazonaws.com/$userPool/.well-known/jwks.json")
def fetch(uri: URI): String = {
val http = HttpClients.createDefault()
try {
fetch(http, uri)
} finally {
http.close()
}
}
protected def fetch(http: CloseableHttpClient, uri: URI): String = {
val req = new HttpGet(uri)
val res = http execute req
try {
val entity = res.getEntity
EntityUtils.toString(entity, StandardCharsets.UTF_8)
} finally {
res.close()
}
}
}
示例4: TwitterInfoClient
//设置package包名称以及导入依赖的类
package clients
import config.ConfigValues
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer
import org.apache.commons.io.IOUtils
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
object TwitterInfoClient {
def fetchInfo(userId: String): Either[String,String] = {
val consumer = new CommonsHttpOAuthConsumer(ConfigValues.twitterConsumerKey, ConfigValues.twitterConsumerSecret)
consumer.setTokenWithSecret(ConfigValues.twitterAccessToken, ConfigValues.twitterAccessTokenSecret)
val request = new HttpGet(s"https://api.twitter.com/1.1/users/show.json?screen_name=$userId")
consumer.sign(request)
val client = new DefaultHttpClient()
val response = client.execute(request)
println(response.getStatusLine.getStatusCode)
response.getStatusLine.getStatusCode match {
case 200 =>
val responseString = IOUtils.toString(response.getEntity.getContent)
println(s"\n\n$responseString")
Right(responseString)
case 400 =>
Left("Not Found")
}
}
}
示例5: CustomFieldResourceSpec
//设置package包名称以及导入依赖的类
package redmine4s.api.resource
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.scalatest.{DiagrammedAssertions, FlatSpec}
import org.scalatest.mockito.MockitoSugar
import org.mockito.Matchers._
import org.mockito.Mockito._
import org.slf4j.{Logger, LoggerFactory}
import redmine4s.Redmine
import redmine4s.auth.Authorization
import redmine4s.conf.Configuration
class CustomFieldResourceSpec extends FlatSpec with DiagrammedAssertions with MockitoSugar {
"CustomFieldResource.listCustomFields" should "POST simple HTTP request" in {
val mockHttpClient = mock[HttpClient]
val customFieldResource = new CustomFieldResource {
override protected val httpClient: HttpClient = mock[HttpClient]
override protected val configuration: Configuration = mock[Configuration]
override protected val authorization: Option[Authorization] = Some(mock[Authorization])
override protected val logger: Logger = LoggerFactory.getLogger("nolog")
override protected val redmine: Redmine = mock[Redmine]
when(authorization.get.header).thenReturn(("HttpHeaderKey", "HttpHeaderValue"))
when(httpClient.execute(any[HttpGet])).thenReturn(mock[HttpResponse])
}
val customFields = customFieldResource.listCustomFields()
assert(customFields.toList.length === 2)
}
}
示例6: FixerFormats
//设置package包名称以及导入依赖的类
package services
import configuration.Config
import model._
import org.apache.http.client.methods.HttpGet
import org.joda.time.{DateTime, LocalDate}
import scala.concurrent.{ExecutionContext, Future}
object FixerFormats {
import play.api.libs.json._
implicit val dateReads: Reads[DateTime] = Reads.jodaDateReads("yyyy-MM-dd")
case class Conversions(
base: String,
date: LocalDate,
rates: Map[String, BigDecimal]
) {
def convert(fromCurrencyCode: String, toCurrencyCode: String, amount: BigDecimal): Option[BigDecimal] = {
if (fromCurrencyCode == toCurrencyCode) {
Some(amount)
} else {
for {
fromRate <- rates.get(fromCurrencyCode)
toRate <- rates.get(toCurrencyCode)
} yield amount * toRate/fromRate
}
}
}
implicit val conversionFormat: Format[Conversions] = Json.format[Conversions]
}
object Fixer {
import FixerFormats._
val httpClient = new AsyncHttpClient
def constructUrl(date: LocalDate): String = {
val dateString = date.toString("YYYY-MM-DD")
s"${Config.Fixer.baseUri}$dateString"
}
def conversions(date: LocalDate)(implicit ec: ExecutionContext): Future[Either[Error, Conversions]] = {
val url = constructUrl(date)
val request = new HttpGet(url)
httpClient.json[Conversions](request)
}
}
示例7: post
//设置package包名称以及导入依赖的类
package main.java.eyepatch.input_sources
import java.util
import main.java.eyepatch.{Channel, OutputManager}
import org.apache.http.NameValuePair
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.{HttpGet, HttpPost}
import org.apache.http.impl.client.DefaultHttpClient
import org.apache.http.message.BasicNameValuePair
trait Networked {
val HOST = "http://localhost:5000"
def post(operation : String, parameters : Map[String, String]) : String = {
val post = new HttpPost(HOST + "/" + operation)
var nameValuePairs = new util.ArrayList[NameValuePair]()
parameters.foreach { case (key, value) =>
nameValuePairs.add(new BasicNameValuePair(key, value))
}
post.setEntity(new UrlEncodedFormEntity(nameValuePairs))
post.setHeader("Content-type", "application/x-www-form-urlencoded")
val post_response = (new DefaultHttpClient).execute(post)
OutputManager.print(Channel.Debug, "Networked Bot: " + post_response)
return post_response.getEntity.getContent.toString
}
def get(operation : String, parameters : Map[String, String]) : String = {
val query_string = "?" + parameters.map{ case (key, value) => key + "=" + value}.mkString("&")
val get_request = new HttpGet(HOST + "/" + operation + query_string)
get_request.addHeader("Content-type", "application/x-www-form-urlencoded")
val get_response = (new DefaultHttpClient).execute(get_request)
OutputManager.print(Channel.Debug, "Networked Bot: " + get_response)
val inputStream = get_response.getEntity.getContent
val content = io.Source.fromInputStream(inputStream).getLines.mkString
inputStream.close
return content
}
}
示例8: ModuleUpdatesService
//设置package包名称以及导入依赖的类
package org.jmotor.sbt.service
import java.util.concurrent.{Executors, TimeUnit}
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.{CloseableHttpClient, HttpClients}
import org.apache.http.util.EntityUtils
import org.jmotor.sbt.model.{ModuleStatus, Status}
import org.jmotor.sbt.util.ModuleStatusParser
import sbt.ModuleID
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration._
import scala.concurrent.{Await, ExecutionContext, Future}
object ModuleUpdatesService {
private[this] val updatesHost = "https://stack-badges.herokuapp.com"
private[this] val hc: CloseableHttpClient = HttpClients.custom()
.setMaxConnTotal(20)
.setMaxConnPerRoute(20)
.setConnectionTimeToLive(5, TimeUnit.MINUTES).build()
private[this] val executionContext = ExecutionContext.fromExecutor(Executors.newFixedThreadPool(10))
def resolve(modules: Seq[ModuleID]): Seq[ModuleStatus] = {
val result = Future.traverse(modules)(check)
Await.result(result, 1.minutes)
}
private[this] def check(module: ModuleID): Future[ModuleStatus] = {
val location = s"$updatesHost/maven-central/resolves/${module.organization}/${module.name}/${module.revision}"
val get = new HttpGet(location)
Future {
hc.execute(get)
}(executionContext) map { response ?
response.getStatusLine.getStatusCode match {
case s if s / 100 == 2 ?
val body = EntityUtils.toString(response.getEntity, "utf-8")
val (status, version) = ModuleStatusParser.parse(body)
ModuleStatus(module.organization, module.name, module.revision, Status.withName(status), version)
case 404 ? ModuleStatus(module.organization, module.name, module.revision, Status.NotFound, "")
case _ ? ModuleStatus(module.organization, module.name, module.revision, Status.Error, "")
}
} recover {
case t: Throwable ? ModuleStatus(module.organization, module.name, module.revision, Status.Error, t.getLocalizedMessage)
}
}
}
示例9: HttpClientTest
//设置package包名称以及导入依赖的类
package tests
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
import org.scalatest.{FlatSpec, Matchers}
class HttpClientTest extends FlatSpec with Matchers {
def getRestContent(url:String): String = {
val httpClient = new DefaultHttpClient()
val httpResponse = httpClient.execute(new HttpGet(url))
println(httpResponse.getStatusLine.getStatusCode)
val entity = httpResponse.getEntity()
var content = ""
if (entity != null) {
val inputStream = entity.getContent()
content = io.Source.fromInputStream(inputStream).getLines.mkString
inputStream.close
}
// httpResponse.getAllHeaders.foreach(println _)
httpClient.getConnectionManager().shutdown()
return content
}
"" should "" in {
val iter = new Iterator[Int] {
var cnt = 0
override def hasNext: Boolean = true
override def next(): Int = {
// if (cnt<10) { cnt = cnt + 1; return 10 }
// cnt = 0
900
}
}
for (i <- 1 to 50) {
getRestContent("http://localhost:9120/tracking-data/count/nino/BB222222A")
Thread.sleep(iter.next)
}
// println(getRestContent("http://localhost:9120/tracking-data/count/nino/BB222222A"))
}
}
示例10: HttpHelper
//设置package包名称以及导入依赖的类
package util
import java.io.InputStream
import java.util.Scanner
import org.apache.http.HttpStatus
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClients
class HttpHelper {
def downloadPageByGet(url: String): Option[String] = {
val httpClient = HttpClients.createDefault()
val httpGetter = new HttpGet(url)
val response = httpClient.execute(httpGetter)
if (response.getStatusLine.getStatusCode == HttpStatus.SC_OK) {
var is: InputStream = null
var sc: Scanner = null
try {
val entity = response.getEntity
val buffer = new StringBuilder()
is = entity.getContent
sc = new Scanner(is)
while (sc.hasNext) {
buffer.append(sc.nextLine())
}
Some(buffer.toString())
} catch {
case ex: Exception =>
if (is != null) is.close()
if (sc != null) sc.close()
if (response != null) response.close()
None
}
} else None
}
}
示例11: healthCheckUrl
//设置package包名称以及导入依赖的类
package io.github.shogowada.scala.jsonrpc.example.test.utils
import java.net.ServerSocket
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.HttpClientBuilder
import scala.util.Try
trait BaseTarget {
lazy val port = freePort()
lazy val url = s"http://localhost:$port"
lazy val jarLocation = System.getProperty("jarLocation")
def healthCheckUrl = url
def freePort(): Int = {
val server = new ServerSocket(0)
val localPort = server.getLocalPort
server.close()
localPort
}
private def startProcess(jarLocation: String, port: Int): Process = {
new ProcessBuilder(
"java", s"-Dport=$port", "-jar", jarLocation
).start()
}
private def waitUntilReady(): Unit = {
Range(0, 10).toStream
.map(_ => {
Thread.sleep(1000)
val client = HttpClientBuilder.create().build()
val maybeCode = Try {
val response = client.execute(new HttpGet(healthCheckUrl))
val code = response.getStatusLine.getStatusCode
response.close()
code
}.toOption
client.close()
maybeCode
})
.filter(code => code.contains(200))
.head
}
val target = startProcess(jarLocation, port)
Runtime.getRuntime.addShutdownHook(new Thread() {
override def run(): Unit = {
target.destroy()
}
})
waitUntilReady()
}
示例12: PeopleController
//设置package包名称以及导入依赖的类
import org.apache.http.impl.client.DefaultHttpClient
import java.io._
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
import scala.collection.mutable.StringBuilder
import org.json4s._
import org.json4s.native.JsonMethods._
object PeopleController {
val Url = "http://swapi.co/api/people"
implicit val formats = DefaultFormats
case class Person(name: String,height: String,mass: String,hair_color: String,skin_color: String,eye_color: String,birth_year: String,gender: String,species: List[String],created: String,edited: String,url: String)
case class PeoplePage(count : String, next : String, previous : String, results : List[Person])
def GetPeople : List[Person] = {
var content: String = GetContent(Url)
return parse(content).extract[PeoplePage].results
}
//repetido
def GetContent(url : String): String = {
val httpClient = new DefaultHttpClient
val httpResponse = httpClient.execute(new HttpGet(url))
val entity = httpResponse.getEntity()
var content = ""
if (entity != null) {
val inputStream = entity.getContent()
content = io.Source.fromInputStream(inputStream).getLines.mkString
inputStream.close
}
httpClient.getConnectionManager().shutdown()
content
}
}
示例13: GetContent
//设置package包名称以及导入依赖的类
import org.apache.http.impl.client.DefaultHttpClient
import java.io._
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
import scala.collection.mutable.StringBuilder
import org.json4s._
import org.json4s.native.JsonMethods._
trait BaseController {
def GetContent(url : String): String = {
val httpClient = new DefaultHttpClient
val httpResponse = httpClient.execute(new HttpGet(url))
val entity = httpResponse.getEntity()
var content = ""
if (entity != null) {
val inputStream = entity.getContent()
content = io.Source.fromInputStream(inputStream).getLines.mkString
inputStream.close
}
httpClient.getConnectionManager().shutdown()
content
}
}
示例14: SpeciesController
//设置package包名称以及导入依赖的类
import org.apache.http.impl.client.DefaultHttpClient
import java.io._
import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient
import scala.collection.mutable.StringBuilder
import org.json4s._
import org.json4s.native.JsonMethods._
object SpeciesController {
val Url = "http://swapi.co/api/species/?page=1&format=json"
implicit val formats = DefaultFormats
case class Specie(name: String, classification : String, designation : String, average_height : String,
average_lifespan : String, eye_colors : String, hair_colors : String, skin_colors : String, homeworld: String, language : String,
created : String, edited : String, url : String)
case class SpeciesPage(count : String, next : String, previous : String, results : List[Specie])
def GetSpecies : List[Specie] = {
var content: String = GetContent(Url)
return parse(content).extract[SpeciesPage].results
}
//repetido
def GetContent(url : String): String = {
val httpClient = new DefaultHttpClient
val httpResponse = httpClient.execute(new HttpGet(url))
val entity = httpResponse.getEntity()
var content = ""
if (entity != null) {
val inputStream = entity.getContent()
content = io.Source.fromInputStream(inputStream).getLines.mkString
inputStream.close
}
httpClient.getConnectionManager().shutdown()
content
}
}