的HttpServer张贴参数[关闭]

问题描述 投票:2回答:3

我写使用HTTPServer包提供的com.sun.net.httpserver.HttpServer的服务。我需要发送一些相当大的数据作为字节流这个服务(比如说一百万个整数)。

我几乎找遍了所有可用的例子,都指向发送URL中的小GET请求。我需要知道如何将数据发送的POST请求。也有可以发送的数据的任何限制?

java httpserver
3个回答
1
投票

而不是在上面的答案持久URL连接,我会建议使用Apache了HTTPClient库(如果你不使用Spring的小程序,小服务程序通信)

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

在此你可以建立一个客户端发送一个序列化对象(例如序列化JSON字符串:https://code.google.com/p/google-gson/)作为POST请求通过HTTP服务器:

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;
}

但是春天为您节省了大量的时间和麻烦,所以我会建议check it out


1
投票

可以通过字节写入的连接输出流发送对POST请求中的数据,如下所示

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(); 
        }
    }
}

与POST,存在对可发送的数据量没有限制。你可以找到约来到这里的局限性细节maximum length of HTTP GET request?


0
投票

如果我有你的权利,你要在你的HTTPServer的方向发送请求,如GET请求,而不是使用后。

在您的HTTP客户端实现,您可以设置HTTP头,并请求的方法:

HttpURLConnection connection = null;
    try {
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("GET"); //Or POST
} catch (IOException e) {
    e.printStacktrace();
}
© www.soinside.com 2019 - 2024. All rights reserved.