我正在通过 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) 它可以工作,使用 count = 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 实例