2

Am writing a service using the HTTPServer provided by com.sun.net.httpserver.HttpServer package. I need to send some sizable data as a byte stream to this service (say a million integers).

I have almost searched all the examples available, all point to sending a small GET request in the URL. I need to know how to send the data as a POST request. Also is there any limit on the data that can be sent?

Neuron
  • 5,141
  • 5
  • 38
  • 59
LPD
  • 2,833
  • 2
  • 29
  • 48

3 Answers3

1

Rather than persistent URL connection in the answer above, I would recommend using Apache HTTPClient libraries (if you are not using Spring for applet-servlet communication)

http://hc.apache.org/httpclient-3.x/

In this you can build a client and send a serialized object (for instance serialised to JSON string: https://code.google.com/p/google-gson/ ) as POST request over HTTP to your server:

public HttpResponse sendStuff (args) {
    HttpPost post = null;
    try {
        HttpClient client = HttpClientBuilder.create().build();

        post = new HttpPost(servletUrl);
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(<nameString>, <valueString>));

        post.setEntity(new UrlEncodedFormEntity(params));

        HttpResponse response = client.execute(post);
        response.getStatusLine().getStatusCode();
        return response;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

However Spring saves you a lot of time and hassle so I would recommend to check it out

Neuron
  • 5,141
  • 5
  • 38
  • 59
Dan
  • 106
  • 10
1

You can send the data for the POST request by writing bytes to the connections output stream as shown below

public static String excutePost(String targetURL, String urlParameters)
{
    URL url;
    HttpURLConnection connection = null;    
    try {
        //Create connection
        url = new URL(targetURL);
        connection = (HttpURLConnection)url.openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", 
                 "application/x-www-form-urlencoded");

        connection.setRequestProperty("Content-Length", "" + 
                         Integer.toString(urlParameters.getBytes().length));
        connection.setRequestProperty("Content-Language", "en-US");    

        connection.setUseCaches (false);
        connection.setDoInput(true);
        connection.setDoOutput(true);

        //Send request
        DataOutputStream wr = new DataOutputStream (
                                connection.getOutputStream ());
        wr.writeBytes (urlParameters);
        wr.flush ();
        wr.close ();

        //Get Response  
        InputStream is = connection.getInputStream();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        String line;
        StringBuffer response = new StringBuffer(); 
        while((line = rd.readLine()) != null) {
            response.append(line);
            response.append('\r');
        }
        rd.close();
        return response.toString();

    } catch (Exception e) {
        e.printStackTrace();
        return null;

    } finally {

        if(connection != null) {
            connection.disconnect(); 
        }
    }
}

With POST, there is no limit on the amount of data that can be sent. You can find out the details about the limitations of GET here maximum length of HTTP GET request?

Community
  • 1
  • 1
java_geek
  • 17,585
  • 30
  • 91
  • 113
0

If I got you right, you want to send requests in direction of your HTTPServer, as a GET request instead of using post.

In your HTTP Client implementation you can set the HTTP Headers and also the request Method:

HttpURLConnection connection = null;
    try {
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("GET"); //Or POST
} catch (IOException e) {
    e.printStacktrace();
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
questionaire
  • 2,475
  • 2
  • 14
  • 28
  • No thats the exact opposite. I want data to be sen as POST, and also sizable data as said. – LPD Oct 14 '14 at 08:18