本文整理汇总了Scala中org.apache.http.client.entity.UrlEncodedFormEntity类的典型用法代码示例。如果您正苦于以下问题:Scala UrlEncodedFormEntity类的具体用法?Scala UrlEncodedFormEntity怎么用?Scala UrlEncodedFormEntity使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UrlEncodedFormEntity类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: 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
}
示例2: 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
}
}
示例3: ReCaptchaImpl
//设置package包名称以及导入依赖的类
package webby.form.field.recaptcha
import java.io.IOException
import java.util
import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import org.apache.http.NameValuePair
import org.apache.http.client.entity.UrlEncodedFormEntity
import org.apache.http.client.methods.HttpPost
import org.apache.http.message.BasicNameValuePair
import webby.api.mvc.RequestHeader
import webby.commons.io.StdJs
import scala.annotation.tailrec
import scala.util.Random
class ReCaptchaImpl(val config: ReCaptchaConfig) extends ReCaptcha {
protected val httpClient = config.initHttpClient
@tailrec
private def readUrl[A](fn: => A, remainingTries: Int = 15): A = {
try {
fn
} catch {
case e: IOException =>
if (remainingTries <= 1) throw new RuntimeException("Cannot receive captcha response for 15 tries", e)
Thread.sleep(Random.nextInt(500))
readUrl(fn, remainingTries - 1)
}
}
override def solve(reCaptchaResponse: String)(implicit req: RequestHeader): Boolean = {
if (reCaptchaResponse.isEmpty) false
else {
val verifyResult: ReCaptchaVerify =
readUrl({
val post: HttpPost = new HttpPost("https://www.google.com/recaptcha/api/siteverify")
post.setEntity(
new UrlEncodedFormEntity(util.Arrays.asList[NameValuePair](
new BasicNameValuePair("secret", config.secretKey),
new BasicNameValuePair("response", reCaptchaResponse),
new BasicNameValuePair("remoteip", req.remoteAddress))))
val response = httpClient.execute(post)
StdJs.get.mapper.readValue(response.getEntity.getContent, classOf[ReCaptchaVerify])
})
verifyResult.success
}
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
case class ReCaptchaVerify(success: Boolean)