本文整理汇总了Scala中org.apache.http.entity.StringEntity类的典型用法代码示例。如果您正苦于以下问题:Scala StringEntity类的具体用法?Scala StringEntity怎么用?Scala StringEntity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了StringEntity类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Scala代码示例。
示例1: Item
//设置package包名称以及导入依赖的类
package com.github.vladminzatu.surfer.persist
import com.github.vladminzatu.surfer.Score
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.{HttpClientBuilder}
import org.apache.spark.rdd.RDD
import org.json4s.jackson.Serialization.write
case class Item(item:String, score:Double)
class RestPersister extends Persister {
val url = "http://localhost:8080/items"
override def persist(scores: RDD[(String, Score)]): Unit = {
implicit val formats = org.json4s.DefaultFormats
val payload = write(scores.collect().sortWith((a,b) => a._2.value > b._2.value).map(x => Item(x._1, x._2.value)))
val client = HttpClientBuilder.create().build();
client.execute(postRequest(payload))
}
private def postRequest(payload: String): HttpPost = {
val post = new HttpPost(url)
post.setEntity(new StringEntity(payload))
post
}
}
开发者ID:VladMinzatu,项目名称:surfer,代码行数:29,代码来源:RestPersister.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: CinepolisHTTP
//设置package包名称以及导入依赖的类
package com.rho.scrap
object CinepolisHTTP {
import org.apache.http.client.methods.HttpPost
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.DefaultHttpClient
import com.rho.client.RhoClient
import com.rho.scrap.CinepolisParse.{parseZones,parseCinemas,parseComplex}
import com.rho.scrap.CinepolisClasses.{Cinema,Complex}
val scheme = "http"
val host = "www.cinepolis.com"
val client = new RhoClient(Scheme=scheme,Host=host)
val optionsPath = "/cartelera/cdmx-centro/"
val cinemasPath = "/Cartelera.aspx/GetNowPlayingByCity"
val complexPath = "/Cartelera.aspx/ObtenerInformacionComplejo"
val prefix = "[HTTP] "
def getZones: List[String] = {
System.out.println(prefix+"Fetching zone list")
val list = parseZones(client.doGET(Map(),optionsPath))
System.out.println(prefix+"Done")
list
}
private def constructPOST(json: String, path: String): HttpPost = {
val post = new HttpPost(scheme+"://"+host+path)
post.setHeader("content-type", "application/json")
post.setEntity(new StringEntity(json))
post
}
def getCinemas(zone: String): List[Cinema] = {
val json = "{\"claveCiudad\":\""+zone+"\",\"esVIP\":false}"
val post = constructPOST(json, cinemasPath)
System.out.println(prefix+"Fetching Cinemas for zone "+zone)
val body = client.doRequest(post)
System.out.println(prefix+"Done")
parseCinemas(body)
}
def getComplex(cinema: Cinema): Complex = {
val json = "{\"idComplejo\":"+cinema.Id+",\"HijosComplejo\":\"\"}"
val post = constructPOST(json, complexPath)
System.out.println(prefix+"Fetching complex for "+cinema.Id+": "+cinema.Name)
val body = client.doRequest(post)
System.out.println(prefix+"Done")
parseComplex(body)
}
}
开发者ID:Rhodolfo,项目名称:ScrapCines,代码行数:53,代码来源:http.scala
示例4: 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
示例5: UserListRequestHandler
//设置package包名称以及导入依赖的类
package handlers
import com.google.inject.{Inject, Singleton}
import exceptions.{ServerNotRespondingException, ServerNotStartedException}
import org.apache.http.entity.StringEntity
import org.apache.http.protocol.{HttpContext, HttpRequestHandler}
import org.apache.http.{HttpRequest, HttpResponse}
import services.UserService
import keys.ResponseKeys._
import scala.util.{Failure, Success}
@Singleton
class UserListRequestHandler @Inject()(userService: UserService) extends HttpRequestHandler {
override def handle(request: HttpRequest, response: HttpResponse, context: HttpContext): Unit = {
userService.fetchOnlineUsers() match {
case Success(users) =>
response.setStatusCode(OK)
response.setEntity(new StringEntity(users))
case Failure(notRespondingException: ServerNotRespondingException) =>
response.setStatusCode(GATEWAY_TIMEOUT)
response.setEntity(new StringEntity(notRespondingException.getMessage))
case Failure(notStartedException: ServerNotStartedException) =>
response.setStatusCode(BAD_GATEWAY)
response.setEntity(new StringEntity(notStartedException.getMessage))
case Failure(error) =>
response.setStatusCode(INTERNAL_SERVER_ERROR)
response.setEntity(new StringEntity(error.getMessage))
}
}
}
开发者ID:jameshforster,项目名称:ToL-Scout-Plugin,代码行数:32,代码来源:UserListRequestHandler.scala
注:本文中的org.apache.http.entity.StringEntity类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论