AWS EC2 HTTP发布内容大小限制

时间:2012-10-24 19:00:50

标签: rest post amazon-ec2 amazon-web-services restlet

我正在通过REST与我的EC2实例上的程序进行通信,一切运行正常,直到我通过POST请求发送的JSON大小达到~20KB。我在本地计算机网络服务器上运行代码时没有这些问题,但是当我将代码上传到EC2时,数据包永远不会到达服务器。

亚马逊阻止数据包超过20KB以防止DoS攻击吗?如果是这样,我该如何删除此功能。我需要能够向我的实例发送至少500KB的JSON。

我正在运行Restlet 2.1并使用Google GSON 2.2.2所以要运行下面的代码,您需要先前链接中的org.restlet.jar和gson.jar。

此代码在EC2实例上启动restlet服务器:

import org.restlet.Application;
import org.restlet.Component;
import org.restlet.Restlet;
import org.restlet.data.Protocol;
import org.restlet.routing.Router;
import org.restlet.service.LogService;

public class StringApplication extends Application {
    public static final int PORT = 8005;
    public static void main(String[] args) throws Exception {

        Component component = new Component();
        component.setLogService(new LogService(false));

        component.getDefaultHost().attach(new StringApplication());

        component.getServers().add(Protocol.HTTP, PORT);
        component.start();
    }

    @Override
    public synchronized Restlet createInboundRoot() {
        Router router = new Router(getContext());
        router.attachDefault(StringResource.class);
        return router;
    }
}    

以下是我的restlet资源的代码

import java.lang.reflect.Type;
import java.util.ArrayList;

import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.ServerResource;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class StringResource extends ServerResource {
    private static ArrayList<String> strings = new ArrayList<String>();

    @Get
    public String getStrings() {
    Gson gson = new Gson();
    String output = gson.toJson(strings);
    return output;
    }

    @Post
    public void postStrings(String input) {
        Gson gson = new Gson();
        Type collectionType = new TypeToken<ArrayList<String>>() {
        }.getType();
        strings = gson.fromJson(input, collectionType);
    }
}

最后,这是我为测试不同数据包大小而创建的代码。当count = 100(10KB)时,它有效,计数= 1000(100KB)时超时。

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;

import org.restlet.Client;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.MediaType;
import org.restlet.data.Method;
import org.restlet.data.Protocol;
import org.restlet.representation.Representation;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class StringDemo {

    private static final int COUNT = 1000;
    private static final String STRING = "THIS IS MY VERY LONG STRING AND IT IS FUN TO READ";
    private static final String SERVER_ADDRESS = "http://localhost:" + StringApplication.PORT;

    public static void main(String[] args) throws IOException {
        Gson gson = new Gson();
        Client client = new Client(Protocol.HTTP);
        Request request = new Request();
        request.setResourceRef(SERVER_ADDRESS);
        request.setMethod(Method.POST);
        ArrayList<String> strings = generateStrings();
        String json = gson.toJson(strings);
        request.setEntity(json, MediaType.APPLICATION_JSON);

        System.out.println("JSON bytesize " + json.length() * Character.SIZE / Byte.SIZE);
        Response handle = client.handle(request);
        Representation entity = handle.getEntity();

        if (handle.getStatus().isSuccess()) {
            System.out.println("Successfully uploaded strings");
        } else {
            System.out.println(entity != null ? entity.getText() : "no response from server");
        }

        request = new Request();
        request.setResourceRef(SERVER_ADDRESS);
        request.setMethod(Method.GET);

        handle = client.handle(request);
        entity = handle.getEntity();

        if (handle.getStatus().isSuccess()) {
            Type collectionType = new TypeToken<ArrayList<String>>() {
            }.getType();
            strings = gson.fromJson(entity.getReader(), collectionType);
            System.out.println("Received " + strings.size() + " strings");
        } else {

            System.out.println(entity != null ? entity.getText() : "no response from server");
        }

    }

    private static ArrayList<String> generateStrings() {
        ArrayList<String> strings = new ArrayList<String>(COUNT);
        for (int i = 0; i < COUNT; i++) {
            strings.add(STRING);
        }
        return strings;
    }

}

您必须将SERVER_ADDRESS更改为运行代码的EC2实例

1 个答案:

答案 0 :(得分:0)

当您使用其中一个较大的实例时,问题就会消失。这些问题似乎与亚马逊提供的免费层机器的默认配置有关。