import akka.actor._ import akka.stream._ import akka.stream.scaladsl._ import akka.http.scaladsl.Http import scala.util._ import akka._ import akka.http.scaladsl.common._ import spray.json.DefaultJsonProtocol import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.common.EntityStreamingSupport import akka.http.scaladsl.model._ import spray.json._ trait MyFormats extends SprayJsonSupport with DefaultJsonProtocol object Converters extends MyFormats { case class County(id: Int, name: String) implicit val countyFormat = jsonFormat2(County) } object HttpClientDemo extends App { import Converters._ ) implicit val mat = ActorMaterializer() implicit val ec = sys.dispatcher implicit val jsonStreamingSupport: JsonEntityStreamingSupport = EntityStreamingSupport.json() import akka.util.ByteString import akka.http.scaladsl.model.HttpEntity.limitableByteSource val source: Source[County,NotUsed] = Source()} def countyToByteString(c: County) = { ByteString(c.toJson.toString) } val flowCountyToByteString : Flow[County,ByteString,NotUsed] = Flow.fromFunction(countyToByteString) val rowBytes = limitableByteSource(source via flowCountyToByteString) import akka.http.scaladsl.model.headers._ val request = HttpRequest(HttpMethods.POST,uri = s) .addHeader(RawHeader(,)) .addCredentials(BasicHttpCredentials(, )) val data = HttpEntity( ContentTypes.`application/json`, rowBytes ) def uploadRows(request: HttpRequest, dataEntity: RequestEntity) = { val futResp = Http(sys).singleRequest( 枣庄京彩request.copy(entity = dataEntity) ) futResp .andThen { case Success(r@HttpResponse(StatusCodes.OK, _, entity, _)) => entity.dataBytes.map(_.utf8String).runForeach(println) case Success(r@HttpResponse(code, _, _, _)) => println(s) r.discardEntityBytes() ) ) } } uploadRows(request,data) s盐城京彩cala.io.StdIn.readLine() sys.terminate() }