Java 多个POST请求在第二天出现500个错误

Java 多个POST请求在第二天出现500个错误,java,rest-assured,Java,Rest Assured,我需要一个接一个地发送许多请求。我有一个密码: public void sendRestRequest(String xmlFile){ try{ String myRequest = generateStringFromResource(xmlFile); given().auth().basic(prop.getProperty("restLogin"), prop.getProperty("restPassword")

我需要一个接一个地发送许多请求。我有一个密码:

   public void sendRestRequest(String xmlFile){

        try{
            String myRequest = generateStringFromResource(xmlFile);
            given().auth().basic(prop.getProperty("restLogin"), prop.getProperty("restPassword"))
                    .contentType("application/xml")
                    .body(myRequest.getBytes(StandardCharsets.UTF_8))
                    .when()
                    .post(prop.getProperty("restURL"))
                    .then().
                    assertThat().statusCode(200).and().
                    assertThat().body("status", equalTo("UPLOADED"));
            }
        catch (Exception e){ LOG.error(String.valueOf(e)); }
    }

public static String generateStringFromResource(String path) throws IOException {
        return new String(Files.readAllBytes(Paths.get(path)));
    }
我可以成功创建第一个请求。但在第二个,我有500个状态码,而不是200个。以及此类错误消息:

at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure.validate(ResponseSpecificationImpl.groovy:483)
        at io.restassured.internal.ResponseSpecificationImpl$HamcrestAssertionClosure$validate$1.call(Unknown Source)
        at io.restassured.internal.ResponseSpecificationImpl.validateResponseIfRequired(ResponseSpecificationImpl.groovy:655)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite$PogoCachedMethodSiteNoUnwrapNoCoerce.invoke(PogoMetaMethodSite.java:210)
        at org.codehaus.groovy.runtime.callsite.PogoMetaMethodSite.callCurrent(PogoMetaMethodSite.java:59)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.callCurrent(AbstractCallSite.java:166)
        at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:123)
        at io.restassured.specification.ResponseSpecification$statusCode$0.callCurrent(Unknown Source)
        at io.restassured.internal.ResponseSpecificationImpl.statusCode(ResponseSpecificationImpl.groovy:131)
        at io.restassured.internal.ValidatableResponseOptionsImpl.statusCode(ValidatableResponseOptionsImpl.java:119)

可能有人有想法吗?我想这应该是某种更紧密的连接或类似的东西。

在服务器端,问题在于xml文件,但这很奇怪,因为如果我第一次发送同一个文件,它就没有问题。经过一些尝试,我决定使用不同的方法,效果非常好:

public void sendRestRequest(String xmlFile) throws IOException {
        FileInputStream fis = new FileInputStream("configuration.properties");
        prop.load(fis);
        try {
            URL url = new URL(prop.getProperty("restURL"));
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");
            conn.setRequestProperty("Authorization", prop.getProperty("basic"));

            String input = generateStringFromResource(xmlFile);

            OutputStream os = conn.getOutputStream();
            os.write(input.getBytes());
            os.flush();

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
            StringBuilder responseStrBuilder = new StringBuilder();

            String output;
            while ((output = br.readLine()) != null) {responseStrBuilder.append(output);}

            conn.disconnect();

            JSONObject result = new JSONObject(responseStrBuilder.toString());
            Assert.assertEquals(result.getString("status"), "UPLOADED");

        } catch (IOException e) {
            LOG.error(String.valueOf(e));
        }
    }

在服务器端,问题在于xml文件,但这很奇怪,因为如果我第一次发送同一个文件,它就不会有问题。经过一些尝试,我决定使用不同的方法,效果非常好:

public void sendRestRequest(String xmlFile) throws IOException {
        FileInputStream fis = new FileInputStream("configuration.properties");
        prop.load(fis);
        try {
            URL url = new URL(prop.getProperty("restURL"));
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/xml");
            conn.setRequestProperty("Authorization", prop.getProperty("basic"));

            String input = generateStringFromResource(xmlFile);

            OutputStream os = conn.getOutputStream();
            os.write(input.getBytes());
            os.flush();

            if (conn.getResponseCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
            }

            BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
            StringBuilder responseStrBuilder = new StringBuilder();

            String output;
            while ((output = br.readLine()) != null) {responseStrBuilder.append(output);}

            conn.disconnect();

            JSONObject result = new JSONObject(responseStrBuilder.toString());
            Assert.assertEquals(result.getString("status"), "UPLOADED");

        } catch (IOException e) {
            LOG.error(String.valueOf(e));
        }
    }

所以,这是客户机代码的摘录和客户机java端异常的一部分(第一行缺少一个重要的位),对吗?问题是服务器在其http响应中返回状态500(内部服务器错误)。你认为你给了我们足够的正确信息来产生有用的东西吗?简短的版本:服务器告诉你它的一端出了问题,你在客户端找不到原因。所以,这是客户端代码的摘录和客户端java端异常的一部分(第一行缺少一个重要的部分),对的问题是服务器在其http响应中返回状态500(内部服务器错误)。你认为你给了我们足够的正确信息来产生有用的东西吗?简短的版本:服务器告诉你它的一端出了问题,你在客户端找不到原因