本文整理汇总了Scala中org.apache.http.message.BasicNameValuePair类的典型用法代码示例。如果您正苦于以下问题:Scala BasicNameValuePair类的具体用法?Scala BasicNameValuePair怎么用?Scala BasicNameValuePair使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BasicNameValuePair类的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: SendTwilio
//设置package包名称以及导入依赖的类
package com.trn.sms
import com.twilio.sdk.TwilioRestClient
import com.twilio.sdk.TwilioRestException
import com.twilio.sdk.resource.factory.MessageFactory
import com.twilio.sdk.resource.instance.Message
import org.apache.http.NameValuePair
import org.apache.http.message.BasicNameValuePair
import java.util.ArrayList
import java.util.List
object SendTwilio extends App {
val ACCOUNT_SID = "AC270965dc1d10bbe8aa812f944c351e2c"
val AUTH_TOKEN = "57a39ded3bd4b43913fb47703ad5cd65"
val client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
// Build a filter for the MessageList
val params = new ArrayList[NameValuePair]()
params.add(new BasicNameValuePair("Body", "Test Twilio messagessssss"))
params.add(new BasicNameValuePair("To", "+447441907695"))
params.add(new BasicNameValuePair("From", "+441618504732"))
val messageFactory = client.getAccount().getMessageFactory()
val message = messageFactory.create(params)
println(message.getSid())
}
示例3: 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
}
}
示例4: 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)
示例5: SmsProviderTwilio
//设置package包名称以及导入依赖的类
package ch.wavein.sms_partners.models.providers
import javax.inject.Inject
import ch.wavein.sms_partners.models.Sms
import ch.wavein.sms_partners.viewmodels.responses.SmsSendResponse
import com.twilio.sdk.TwilioRestClient
import org.apache.http.NameValuePair
import org.apache.http.message.BasicNameValuePair
import play.api.Configuration
import scala.collection.JavaConversions._
import play.api.libs.concurrent.Execution.Implicits._
import scala.concurrent.Future
import scala.util.{Failure, Success, Try}
class SmsProviderTwilio @Inject()(
configuration: Configuration
) extends SmsProvider {
val sid = configuration.getString("twilio.sid")
.getOrElse(throw new Exception("twilio.sid " + "not found"))
val token = configuration.getString("twilio.token")
.getOrElse(throw new Exception("twilio.token " + "not found"))
override def send(sms: Sms): Future[SmsSendResponse] = Future {
val client = new TwilioRestClient(sid, token)
// Build the parameters
val params = Seq(
new BasicNameValuePair("To", sms.to),
new BasicNameValuePair("From", sms.from),
new BasicNameValuePair("Body", sms.body)
)
val messageFactory = client.getAccount().getMessageFactory
Try(messageFactory.create(params)) match {
case Success(message) => SmsSendResponse(message.getStatus)
case Failure(ex: Exception) => SmsSendResponse(ex.toString)
}
}
}