I'm developing an application. I'm using spring and RESTful webservices. I send JSON to the webservice and I receive one.
I tested the webservice with the firefox RESTClient plugin. But now I am developing the client (part that sends the json to the webservice and receives the response). This is the part to send the JSON:
String reqStr = "{\"customer\":\"test\",\"age\":\"24\"}";
String request = "example.com";
URL url = new URL(request);
HttpURLConnection rc = (HttpURLConnection)url.openConnection();
rc.setRequestMethod("POST");
rc.setDoOutput( true );
rc.setDoInput( true );
rc.setRequestProperty( "Content-Type", "application/x-www-form-urlencoded" );
rc.setRequestProperty("charset", "utf-8");
int len = reqStr.length();
rc.setRequestProperty( "Content-Length", Integer.toString( len ) );
rc.connect();
OutputStreamWriter out = new OutputStreamWriter( rc.getOutputStream() );
out.write( reqStr, 0, len );
out.flush();
I have the idea that this is not the best way to send JSON via POST. But my real question is.
- How can I receive the POST response in my Java code?
- Is there a way to test this?
Kind regards, Charel