【问题标题】:How do I set a non-standard User-Agent using spray-client?如何使用 spray-client 设置非标准 User-Agent?
【发布时间】:2015-07-19 13:18:13
【问题描述】:

我正在使用 Scala 和 Akka 为电信公司构建应用程序,并且需要使用 UCIP protocol 与帐户信息和充值服务器通信。

UCIP 是一个简单的协议,建立在 XMLRPC 之上;我遇到的唯一问题是它要求客户端以特定格式 User-Agent: <client name>/<protocol version>/<client version> 设置 User-Agent 标头,这将喷雾解析为无效。

我尝试创建一个自定义的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 发送相同的详细信息时,它们会返回正确的响应。

我错过了什么,这甚至可以通过喷雾客户端实现吗?我花了相当多的时间检查互联网(这使我转向自定义标头路由),但仍然没有弄清楚这一点......非常感谢任何帮助:-)

【问题讨论】:

    标签: scala spray spray-client ucip


    【解决方案1】:

    事实证明,我离答案不远了。在检查通过网络发送的标头时,我注意到 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))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-03
      • 1970-01-01
      • 2010-11-26
      相关资源
      最近更新 更多