1
我通過REST與我的EC2實例上的程序進行通信,並且一切都正常運行,直到通過POST請求發送的JSON大小達到〜20KB。當我在本地計算機Web服務器上運行代碼時,我沒有這些問題,但是當我將代碼上傳到EC2時,數據包永遠不會到達服務器。AWS EC2 HTTP Post內容大小限制
亞馬遜是否阻止了大約20KB的數據包以防止DoS攻擊?如果是的話,我該如何刪除此功能。我需要能夠將至少500KB的JSON發佈到我的實例。
我正在運行Restlet 2.1並使用Google GSON 2.2.2所以要運行下面的代碼,您需要先前鏈接中的org.restlet.jar和gson.jar。
此代碼啓動一個的Restlet服務器上的EC2實例:
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);
}
}
最後,這裏是我的代碼創建用於測試不同的數據包大小。計數= 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更改爲您運行的問題會消失,當您使用較大的一個實例上
您使用哪個API調用該POST? – awendt
curl -i -X POST -H「Content-Type:application/json」http:// localhost:8080/user -d'{「key」:「value」}'',但用我的ec2實例替換localhost。我使用Java和Restlet在使用GSON將其轉換後發送數據。我會發布一些示例代碼。 – JoeLaws
所以這不是對EC2 API的調用......我認爲*是問題所在。 – awendt