Skip to content

Instantly share code, notes, and snippets.

@gclaramunt
Created July 12, 2011 15:01
Show Gist options
  • Save gclaramunt/1078151 to your computer and use it in GitHub Desktop.
Save gclaramunt/1078151 to your computer and use it in GitHub Desktop.
simple REST WS consumer client (but better just use dispatch)
package webtest
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
import java.net._
import scala.xml._
import javax.xml.parsers.SAXParserFactory
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
class MyXMLParserFactory extends SAXParserFactoryImpl {
setNamespaceAware(false);
setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
setFeature("http://xml.org/sax/features/validation", false);
}
object WebTester {
def connectHttp( address:String )={
val url=new URL(address)
val conn=url.openConnection
conn match {
case c:HttpURLConnection => c
case _ => error("Only HTTP connections allowed")
}
}
def post(conn:HttpURLConnection, params:String)={
conn.setRequestMethod("POST")
conn.setDoInput(true)
conn.setDoOutput(true)
conn.connect()
val dataOutputStream = conn.getOutputStream()
dataOutputStream.write(params.getBytes())
dataOutputStream.close()
conn.getInputStream
}
def get(conn:HttpURLConnection)={
conn.setRequestMethod("GET")
conn.setDoOutput(true)
conn.connect()
conn.getInputStream
}
def toString(is:InputStream):String ={
val reader = new BufferedReader(new InputStreamReader(is));
val sb = new StringBuilder();
var line=reader.readLine();
try {
while (line != null){
sb.append(line + "\n");
//println line;
line = reader.readLine();
}
} finally {
is.close();
}
sb.toString
}
def main(args:Array[String]): Unit ={
System.setProperty("javax.xml.parsers.SAXParserFactory", "webtest.MyXMLParserFactory");
val is=get (connectHttp(" http://localhost:8080/WebPrj/SimpleSvc"))
val res=XML.load( is)
println (res)
//println (toString(is))
}
}
@gclaramunt
Copy link
Author

or just use databinder's dispatch lib

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment