Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7514265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T00:26:43+00:00 2026-05-30T00:26:43+00:00

I’m struggling to use Twitter’s Finagle library to implement an HTTP request to a

  • 0

I’m struggling to use Twitter’s Finagle library to implement an HTTP request to a SOAP server.

The code below executes the first test successfully (using
java.net.URL), but I’m having a hard time with the second test (using
Finagle client). What am I doing wrong?

Also, I keep being dragged into and imperative style of writing. If
you could help me make the Finagle bit more ‘scala’ like, I will be
extremely grateful.

Here goes:

import java.net.InetSocketAddress 
import scala.xml.{Elem, XML} 
import org.jboss.netty.buffer.ChannelBuffers 
import org.jboss.netty.util.CharsetUtil.UTF_8 
import com.twitter.finagle.Service; 
import com.twitter.finagle.builder.ClientBuilder; 
import com.twitter.finagle.http.Http; 
import org.jboss.netty.handler.codec.http._ 

class SoapClient { 
  private def error(msg: String) = { 
    println("SoapClient error: " + msg) 
  } 

  def wrap(xml: Elem): String = { 
    val buf = new StringBuilder 
    buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone= 
\"no\"?>\n") 
    buf.append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http:// 
schemas.xmlsoap.org/soap/envelope/\">\n") 
    buf.append("<SOAP-ENV:Body>\n") 
    buf.append(xml.toString) 
    buf.append("\n</SOAP-ENV:Body>\n") 
    buf.append("</SOAP-ENV:Envelope>\n") 
    buf.toString 
  } 

  def sendWithJavaNetURL(host: String, req: Elem): Option[Elem] = { 
    val url = new java.net.URL(host) 
    val outs = wrap(req).getBytes 
    val conn = 
url.openConnection.asInstanceOf[java.net.HttpURLConnection] 
    try { 
      conn.setRequestMethod("POST") 
      conn.setDoOutput(true) 
      conn.setRequestProperty("Content-Length", outs.length.toString) 
      conn.setRequestProperty("Content-Type", "text/xml") 
      conn.getOutputStream.write(outs) 
      conn.getOutputStream.close 
      Some(XML.load(conn.getInputStream)) 
    } 
    catch { 
      case e: Exception => error("post: " + e) 
      error("post:" + 
scala.io.Source.fromInputStream(conn.getErrorStream).mkString) 
      None 
    } 
  } 

  def sendWithFinagle(host: String, path: String, req: Elem) = { 
    val clientService: Service[HttpRequest, HttpResponse] = 
ClientBuilder() 
      .codec(Http()) 
      .hosts(new InetSocketAddress(host, 80)) 
      .hostConnectionLimit(1) 
      .build() 
    val request: HttpRequest = new 
DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/") 
    val soapPacket: String = wrap(req) 
    request.setContent(ChannelBuffers.copiedBuffer(soapPacket, UTF_8)) 
    request.setHeader("Content-Lenght", soapPacket.length()) 
    request.setHeader("Content-Type", "text/xml") 
    request.setUri("path") 
    val client = clientService(request) 
    val response = client.get() 
    println(response) 
  } 
} 

object SoapTest { 

  def testWithJavaNetURL { 
    val host = "https://apitest.authorize.net/soap/v1/Service.asmx" 
    val req = <IsAlive xmlns="https://api.authorize.net/soap/v1/"/> 
    val cli = new SoapClient 
    println("##### Test with Java Net URL: request:\n" + 
cli.wrap(req)) 
    val resp = cli.sendWithJavaNetURL(host, req) 
    if (resp.isDefined) { 
      println("##### response:\n" + resp.get.toString) 
    } 
  } 

  def testWithFinagle { 
    val host = "apitest.authorize.net" 
    val path = "/soap/v1/Service.asmx" 
    val req = <IsAlive xmlns="https://api.authorize.net/soap/v1/"/> 
    val cli = new SoapClient 
    println("##### Test with Finagle: request:\n" + cli.wrap(req)) 
    cli.sendWithFinagle(host, path, req) 
  } 

  def main(args: Array[String]) { 
    testWithJavaNetURL 
    testWithFinagle 
  } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T00:26:44+00:00Added an answer on May 30, 2026 at 12:26 am

    Jean-Phillippe, from the Finagles forum, kindly supplied an answer on github

    Here is the full working and nicely updated code:

    import java.net.InetSocketAddress
    import scala.xml.{Elem, XML}
    import org.jboss.netty.buffer.ChannelBuffers
    import org.jboss.netty.util.CharsetUtil.UTF_8
    import com.twitter.finagle.Service;
    import com.twitter.finagle.builder.ClientBuilder;
    import com.twitter.finagle.http.{Http, RequestBuilder};
    import org.jboss.netty.handler.codec.http._
    import org.jboss.netty.buffer.ChannelBuffers.wrappedBuffer
    import java.net.URL
    class SoapClient {
      private def error(msg: String) = {
        println("SoapClient error: " + msg)
      }
      def wrap(xml: Elem): String = {
        val wrapper = <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
                    <SOAP-ENV:Body>
                      {xml}
                    </SOAP-ENV:Body>
                  </SOAP-ENV:Envelope>
        wrapper.toString
      }
      def sendWithJavaNetURL(host: String, req: Elem): Option[Elem] = {
        val url = new java.net.URL(host)
        val outs = wrap(req).getBytes
        val conn =
    url.openConnection.asInstanceOf[java.net.HttpURLConnection]
        try {
          conn.setRequestMethod("POST")
          conn.setDoOutput(true)
          conn.setRequestProperty("Content-Length", outs.length.toString)
          conn.setRequestProperty("Content-Type", "text/xml")
          conn.getOutputStream.write(outs)
          conn.getOutputStream.close
          Some(XML.load(conn.getInputStream))
        }
        catch {
          case e: Exception => error("post: " + e)
          error("post:" +
    scala.io.Source.fromInputStream(conn.getErrorStream).mkString)
          None
        }
      }
      def sendWithFinagle(host: String, path: String, req: Elem) = {
        val clientService: Service[HttpRequest, HttpResponse] =
    ClientBuilder()
          .codec(Http())
          .hosts(new InetSocketAddress(host, 443))
          .tls(host)
          .hostConnectionLimit(1)
          .build()
        val payload = wrap(req).getBytes("UTF-8")
        val request: HttpRequest = RequestBuilder().url(new URL("https", host, 443, path))
                                                    .setHeader("Content-Type", "text/xml")
                                                    .setHeader("Content-Length", payload.length.toString)
                                                    .buildPost(wrappedBuffer(payload))
    
        val client = clientService(request)
    
        for(response <- client) {
          println(response)
          println(response.getContent.toString("UTF-8"))
        }
        // val response = client.get()
      }
    }
    
    object SoapTest {
      def testWithJavaNetURL {
        val host = "https://apitest.authorize.net/soap/v1/Service.asmx"
        val req = <IsAlive xmlns="https://api.authorize.net/soap/v1/"/>
        val cli = new SoapClient
        println("##### Test with Java Net URL: request:\n" +
    cli.wrap(req))
        val resp = cli.sendWithJavaNetURL(host, req)
        if (resp.isDefined) {
          println("##### response:\n" + resp.get.toString)
        }
      }
      def testWithFinagle {
        val host = "apitest.authorize.net"
        val path = "/soap/v1/Service.asmx"
        val req = <IsAlive xmlns="https://api.authorize.net/soap/v1/"/>
        val cli = new SoapClient
        println("##### Test with Finagle: request:\n" + cli.wrap(req))
        cli.sendWithFinagle(host, path, req)
      }
      def main(args: Array[String]) {
        testWithJavaNetURL
        testWithFinagle
      }
    }
    
    SoapTest.main(Array[String]())
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.