• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Scala HttpClients类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Scala中org.apache.http.impl.client.HttpClients的典型用法代码示例。如果您正苦于以下问题:Scala HttpClients类的具体用法?Scala HttpClients怎么用?Scala HttpClients使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了HttpClients类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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)
  }
} 
开发者ID:helgeho,项目名称:Tempas2ArchiveSpark,代码行数:57,代码来源:TempasWaybackSpec.scala


示例2: ProvisionUser

//设置package包名称以及导入依赖的类
// Copyright (c) 2017 Grier Forensics. All Rights Reserved.

package com.grierforensics.greatdane.connector

import java.net.URI
import java.nio.charset.StandardCharsets
import java.nio.file.{Files, Paths}

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.apache.commons.io.IOUtils
import org.apache.http.HttpHeaders
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClients


object ProvisionUser {
  def main(args: Array[String]): Unit = {

    def die = {
      println("Usage: provision-user <email-address> [<certificate file>]")
      sys.exit(1)
    }
    val (emailAddress, certPem) = args.toList match {
      case email :: tail => tail match {
        case Nil => (email, "")
        case certFile :: Nil => (email, new String(Files.readAllBytes(Paths.get(certFile)), StandardCharsets.UTF_8))
        case _ => die
      }
      case _ => die
    }

    val client = HttpClients.createDefault()
    val uri = new URI(s"http://${Settings.Host}:${Settings.Port}/api/v1/user/$emailAddress")
    val post = new HttpPost(uri)
    post.addHeader(HttpHeaders.CONTENT_TYPE, "application/json")
    post.addHeader(HttpHeaders.AUTHORIZATION, Settings.ApiKey)
    println(post.toString)

    val req = ProvisionRequest(None, if (certPem.length > 0) Some(Seq(certPem)) else None)
    val mapper = new ObjectMapper().registerModule(DefaultScalaModule)
    val json = mapper.writeValueAsString(req)
    println(json)

    post.setEntity(new StringEntity(json))

    val resp = client.execute(post)
    try {
      val entity = resp.getEntity
      println(resp.getStatusLine.getStatusCode, resp.getStatusLine.getReasonPhrase)
      println(IOUtils.toString(entity.getContent, StandardCharsets.UTF_8))
    } finally {
      resp.close()
    }

  }
} 
开发者ID:grierforensics,项目名称:Great-DANE-Connector,代码行数:59,代码来源:ProvisionUser.scala


示例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()
    }
  }
} 
开发者ID:malliina,项目名称:cognito-utils,代码行数:61,代码来源:TokenVerifier.scala


示例4: AlfrescoFileUploader

//设置package包名称以及导入依赖的类
package pl.com.bms.fileSynchronizer.fileUploader.alfresco

import org.apache.http.client.methods.HttpPost
import org.apache.http.client.utils.URIBuilder
import org.apache.http.entity.mime.MultipartEntityBuilder
import org.apache.http.entity.mime.content.FileBody
import org.apache.http.impl.client.HttpClients
import pl.com.bms.fileSynchronizer.config.Configuration
import pl.com.bms.fileSynchronizer.fileUploader.{FileUploader, UploadResult}


class AlfrescoFileUploader(val configuration: Configuration) extends FileUploader {
  val baseUrl = configuration.connection.url + "/alfresco/service/api/upload"
  val token = Authentication.getToken(configuration.connection.url, configuration.connection.login, configuration.connection.password)

  override def uploadFile(path: String): UploadResult = {
    val sourceFile = configuration.sourceRoot + path
    val uploadFilePart = new FileBody(new java.io.File(sourceFile))

    val reqEntity = MultipartEntityBuilder.create()
      .addPart("filedata", uploadFilePart)
      .addTextBody("destination", configuration.destinationRoot)
      .addTextBody("path", path)
      .build()

    val url = new URIBuilder(baseUrl)
      .addParameter("alf_ticket", token)
      .build()

    val httpPost = new HttpPost(url)
    httpPost.setEntity(reqEntity)

    val response = HttpClients.createDefault().execute(httpPost)
    UploadResult(response.getStatusLine.getStatusCode.toString, Some(response.getStatusLine.getReasonPhrase))
  }

} 
开发者ID:bms-devs,项目名称:FileSynchronizer,代码行数:38,代码来源:AlfrescoFileUploader.scala


示例5: HTTPClient

//设置package包名称以及导入依赖的类
package pi.jenkins.build

import java.io.InputStream
import java.security.cert.X509Certificate

import org.apache.http.HttpResponse
import org.apache.http.client.config.RequestConfig
import org.apache.http.config.{RegistryBuilder, SocketConfig}
import org.apache.http.conn.socket.{ConnectionSocketFactory, PlainConnectionSocketFactory}
import org.apache.http.conn.ssl.{SSLConnectionSocketFactory, SSLContexts, TrustStrategy}
import org.apache.http.impl.client.{CloseableHttpClient, HttpClients}
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.slf4j.LoggerFactory

object HTTPClient {

  type StreamHandler = (String, HttpResponse) ? InputStream

  private final val LOG = LoggerFactory.getLogger(HTTPClient.getClass)

  private val TIMEOUT: Int = 20 * 1000

  private val socketConfig: SocketConfig = SocketConfig.custom()
    .setSoTimeout(TIMEOUT)
    .setTcpNoDelay(true)
    .build()

  private val requestConfig: RequestConfig = RequestConfig.custom()
    .setSocketTimeout(TIMEOUT)
    .setConnectTimeout(TIMEOUT)
    .build()


  private val sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy {
    override def isTrusted(chain: Array[X509Certificate], authType: String): Boolean = true
  }).build()

  private val sslsf = new SSLConnectionSocketFactory(
    sslContext, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)

  private val socketFactoryRegistry = RegistryBuilder.create[ConnectionSocketFactory]().
    register("https", sslsf).
    register("http", PlainConnectionSocketFactory.INSTANCE).build()

  private val cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry)
  cm.setDefaultSocketConfig(socketConfig)
  cm.setMaxTotal(200)
  cm.setDefaultMaxPerRoute(20)

  val client: CloseableHttpClient = HttpClients.custom()
    .setDefaultRequestConfig(requestConfig)
    .setConnectionManager(cm)
    .build()

} 
开发者ID:jdevelop,项目名称:rpi-jenkins,代码行数:56,代码来源:HTTPClient.scala


示例6: Client

//设置package包名称以及导入依赖的类
package com.stulsoft.mailgun4s

import org.apache.http.auth.{AuthScope, UsernamePasswordCredentials}
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.{CloseableHttpResponse, HttpPost}
import org.apache.http.impl.client.{BasicCredentialsProvider, HttpClients}
import org.apache.http.message.BasicNameValuePair
import org.apache.http.util.EntityUtils
import org.apache.http.{HttpStatus, NameValuePair}


class Client {
  private lazy val conf = new Config

  def sendMail(mail: Mail): Unit = {
    val url = conf.urlToSendMessage.format(conf.apiVersion, conf.domain)
    val post = new HttpPost(url)

    val credentials = new UsernamePasswordCredentials("api", conf.apiKey)
    val credentialsProvider = new BasicCredentialsProvider()
    credentialsProvider.setCredentials(new AuthScope("api.mailgun.net", 443), credentials)
    val httpClient = HttpClients.custom().setDefaultCredentialsProvider(credentialsProvider).build()

    val params = new java.util.ArrayList[NameValuePair]()
    params.add(new BasicNameValuePair("from", mail.from))
    params.add(new BasicNameValuePair("to", mail.to))
    mail.cc.foreach(cc => params.add(new BasicNameValuePair("cc", cc)))
    params.add(new BasicNameValuePair("subject", mail.subject))
    params.add(new BasicNameValuePair(mail.body.bodyType, mail.body.bodyContent))

    post.setEntity(new UrlEncodedFormEntity(params))

    var response: CloseableHttpResponse = null
    try {
      response = httpClient.execute(post)
      println(s"response.setStatusCode: ${response.getStatusLine}")

      if (response.getStatusLine.getStatusCode == HttpStatus.SC_OK) {
        val entity = response.getEntity
        println(s"Response: ${EntityUtils.toString(entity)}")
        EntityUtils.consume(entity)
      }
    }
    catch {
      case e: Exception => println(e.getMessage)
    }
    finally {
      if (response != null)
        response.close()
    }
  }
}

object tt extends App {
  val m = Mail("[email protected]", "[email protected]", None, "test 3", HtmlBody("<htm><h1>Header</h1>text</html>"))
  new Client sendMail m
} 
开发者ID:ysden123,项目名称:poc,代码行数:58,代码来源:Client.scala


示例7: Configuration

//设置package包名称以及导入依赖的类
package redmine4s.conf

import com.typesafe.config.Config
import org.apache.http.client.HttpClient
import org.apache.http.impl.client.HttpClients

object Configuration {
  def default: Configuration = new Configuration {
    override val baseUrl: String = sys.env.getOrElse("REDMINE_URL", throw new ConfigurationNotFoundException("""Environment variable not found "REDMINEURL""""))
    override val userPassword: Option[(String, String)] = for {
      user <- sys.env.get("REDMINE_USER")
      password <- sys.env.get("REDMINE_PASSWORD")
    } yield (user, password)
    override val apiKey: Option[String] = sys.env.get("REDMINE_APIKEY")
  }

  def fromConfig(config: Config): Configuration = new Configuration {
    override val baseUrl: String = config.getString("base_url")
    override val userPassword: Option[(String, String)] = for {
      username <- Option(config.getString("username"))
      password <- Option(config.getString("password"))
    } yield (username, password)
    override val apiKey: Option[String] = Option(config.getString("api_key"))
  }
}

trait Configuration {
  def baseUrl: String

  def userPassword: Option[(String, String)] = None

  def apiKey: Option[String] = None

  def httpClient: HttpClient = HttpClients.createDefault()
} 
开发者ID:tomingtoming,项目名称:redmine4s,代码行数:36,代码来源:Configuration.scala


示例8: initHttpClient

//设置package包名称以及导入依赖的类
package freedomandy.client

import org.apache.http.Header
import org.apache.http.client.config.RequestConfig
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.ByteArrayEntity
import org.apache.http.impl.client.{BasicResponseHandler, CloseableHttpClient, HttpClients}
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager
import org.slf4j.LoggerFactory


trait HttpClient {
  val logg =LoggerFactory.getLogger(this.getClass)
  val client: CloseableHttpClient = initHttpClient()
  var connMgr: PoolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager()

  def initHttpClient(): CloseableHttpClient = {
    val config = RequestConfig.custom().build()
    val client: CloseableHttpClient = HttpClients.custom()
      .setConnectionManager(connMgr)
      .setDefaultRequestConfig(config).build()
    return client
  }

  def close(): Unit = {
    connMgr.close()
  }

  def handlePostRequest(url: String, headers: List[Header], jsonBody: String): String = {
    val post = new HttpPost(url)
    headers.foreach { post.addHeader(_) }
    val entity = new ByteArrayEntity(jsonBody.getBytes("UTF-8"))
    post.setEntity(entity)
    val result = client.execute(post, new BasicResponseHandler())
    return result
  }
} 
开发者ID:freedomandy,项目名称:akka-storage,代码行数:38,代码来源:HttpClient.scala


示例9: CloudFormationCustomResourceResponseWriter

//设置package包名称以及导入依赖的类
package com.dwolla.lambda.cloudformation

import org.apache.http.client.methods.HttpPut
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.{CloseableHttpClient, HttpClients}
import org.json4s.{DefaultFormats, Formats}
import org.json4s.native.Serialization._
import org.slf4j.{Logger, LoggerFactory}

import scala.concurrent.{ExecutionContext, Future}
import scala.io.Source

class CloudFormationCustomResourceResponseWriter(implicit ec: ExecutionContext) {
  protected lazy val logger: Logger = LoggerFactory.getLogger("LambdaLogger")
  protected implicit val formats: Formats = DefaultFormats ++ org.json4s.ext.JodaTimeSerializers.all

  def httpClient: CloseableHttpClient = HttpClients.createDefault()

  def logAndWriteToS3(presignedUri: String, cloudFormationCustomResourceResponse: CloudFormationCustomResourceResponse): Future[Unit] = Future {
    val req = new HttpPut(presignedUri)
    val jsonEntity = new StringEntity(write(cloudFormationCustomResourceResponse))
    jsonEntity.setContentType("")

    req.setEntity(jsonEntity)
    req.addHeader("Content-Type", "")

    logger.info(Source.fromInputStream(req.getEntity.getContent).mkString)

    try {
      val res = httpClient.execute(req)
      res.close()
    } finally {
      httpClient.close()
    }
  }
} 
开发者ID:Dwolla,项目名称:scala-cloudformation-custom-resource,代码行数:37,代码来源:CloudFormationCustomResourceResponseWriter.scala


示例10: 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)
    }
  }

} 
开发者ID:aiyanbo,项目名称:sbt-dependency-updates,代码行数:51,代码来源:ModuleUpdatesService.scala


示例11: 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
  }
} 
开发者ID:TopSpoofer,项目名称:CodingCrawler,代码行数:39,代码来源:HttpHelper.scala



注:本文中的org.apache.http.impl.client.HttpClients类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Scala Stack类代码示例发布时间:2022-05-23
下一篇:
Scala Authorization类代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap