我正在使用Scala和Akka为Telco构建应用程序,并且需要使用UCIP protocol与帐户信息和重新填充服务器进行通信。
UCIP是一个简单的协议,建立在XMLRPC之上;我遇到的唯一问题是它要求客户端以特定格式User-Agent
设置User-Agent: <client name>/<protocol version>/<client version>
标头,这会将解析为无效。
我尝试创建自定义User-Agent
标头,继承自spray.http.HttpHeader
,但它仍然无法正常工作。这是我到目前为止所得到的:
import akka.actor.ActorSystem
import akka.event.{Logging, LoggingAdapter}
import spray.client.pipelining._
import spray.http._
import spray.httpx._
case class `User-Agent`(value: String) extends HttpHeader {
def lowercaseName: String = "user-agent"
def name: String = "User-Agent"
def render[R <: Rendering](r: R): r.type = r ~~ s"User-Agent: $value"
}
class UcipClient(val url: String, val protocol: String, username: String, password: String) (implicit system: ActorSystem) {
val log = Logging.getLogger(system, this)
val logRequest: HttpRequest => HttpRequest = { r => log.debug(r.toString); r }
val logResponse: HttpResponse => HttpResponse = { r => log.debug(r.toString); r }
val pipeline = (
addHeader(`User-Agent`("USSD-UCIP/%s/1.0".format(protocol)))
~> addCredentials(BasicHttpCredentials(username, password))
~> logRequest
~> sendReceive
~> logResponse
)
def send(req: UcipRequest) = pipeline(Post(url, req.getRequest))
}
我的请求一直返回“抱歉,发生错误:403,无效的协议版本未定义”,但是,当我使用curl
发送相同的详细信息时,它们会返回正确的响应。
我缺少什么,喷雾客户端甚至可以实现这一点?我花了相当多的时间检查互联网(这导致我走向自定义标题路线),但仍然没有想到这一点...真的很感激任何帮助: - )
答案 0 :(得分:2)
原来我离答案不远了。在检查通过网络发送的标头时,我注意到User-Agent
被设置了两次:一次是我的代码,另一次是Spray(因为它认为我的标头无效)。
将spray.can.client.user-agent-header
设置为空字符串""
删除了第二个标头,请求成功。这是自定义标题的最终版本:
import spray.http._
object CustomHttpHeaders {
case class `User-Agent`(val value: String) extends HttpHeader with Product with Serializable {
def lowercaseName: String = "user-agent"
def name: String = "User-Agent"
def render[R <: Rendering](r: R): r.type = r ~~ s"User-Agent: $value"
}
}
最终的UCIP客户:
import akka.actor.ActorRefFactory
import com.typesafe.config.Config
import scala.concurrent.ExecutionContext.Implicits.global
import scala.xml.NodeSeq
import spray.client.pipelining._
import spray.http._
import spray.httpx._
class UcipFault(val code: Int, msg: String) extends RuntimeException(s"$code: $msg")
class AirException(val code: Int) extends RuntimeException(s"$code")
class UcipClient(config: Config, val url: String)(implicit context: ActorRefFactory) {
import CustomHttpHeaders._
val throwOnFailure: NodeSeq => NodeSeq = {
case f if (f \\ "fault").size != 0 =>
val faultData = (f \\ "fault" \\ "member" \ "value")
throw new UcipFault((faultData \\ "i4").text.toInt,
(faultData \\ "string").text)
case el =>
val responseCode = ((el \\ "member")
.filter { n => (n \\ "name").text == "responseCode" }
.map { n => (n \\ "i4").text.toInt }).head
if (responseCode == 0) el else throw new AirException(responseCode)
}
val pipeline = (
addHeader(`User-Agent`("USSD-UCIP/%s/1.0".format(config.getString("ucip.server-protocol"))))
~> addCredentials(BasicHttpCredentials(config.getString("ucip.server-username"), config.getString("ucip.server-password")))
~> sendReceive
~> unmarshal[NodeSeq]
~> throwOnFailure
)
def send(req: UcipRequest) = pipeline(Post(url, req.getRequest))
}