2017-07-28 107 views
0

的阿卡-HTTP文檔提供了一個例子來查詢http服務:阿卡http客戶端跟隨重定向

http://doc.akka.io/docs/akka-http/current/scala/http/client-side/request-level.html

我怎麼能告訴阿卡-HTTP來自動執行重定向,而不是接收的HttpResponse與代碼== 302?

阿卡2.5.3,阿卡-HTTP 10.0.9

import akka.actor.{ Actor, ActorLogging } 
import akka.http.scaladsl.Http 
import akka.http.scaladsl.model._ 
import akka.stream.{ ActorMaterializer, ActorMaterializerSettings } 
import akka.util.ByteString 

class Myself extends Actor 
    with ActorLogging { 

    import akka.pattern.pipe 
    import context.dispatcher 

    final implicit val materializer: ActorMaterializer = ActorMaterializer(ActorMaterializerSettings(context.system)) 

    val http = Http(context.system) 

    override def preStart() = { 
    http.singleRequest(HttpRequest(uri = "http://akka.io")) 
     .pipeTo(self) 
    } 

    def receive = { 
    case HttpResponse(StatusCodes.OK, headers, entity, _) => 
     entity.dataBytes.runFold(ByteString(""))(_ ++ _).foreach { body => 
     log.info("Got response, body: " + body.utf8String) 
     } 
    case resp @ HttpResponse(code, _, _, _) => 
     log.info("Request failed, response code: " + code) 
     resp.discardEntityBytes() 
    } 

} 

回答

2

你不能告訴阿卡的HTTP客戶端自動完成這些工作。它是爲阿卡項目開放的問題:https://github.com/akka/akka-http/issues/195

你可能喜歡的東西手動處理這個問題:

case resp @ HttpResponse(StatusCodes.Redirection(_, _, _, _), headers, _, _) => 
    //Extract Location from headers and retry request with another pipeTo 

你可能會想保持你重定向到避免次數的計數無限循環。