亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

多個放心的 POST 請求在第二次出現 500 錯誤

多個放心的 POST 請求在第二次出現 500 錯誤

子衿沉夜 2022-10-26 16:31:24
我需要一一發送許多請求。我有一個代碼:   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)可能有人有想法嗎?我想這應該是一些更緊密的聯系或類似的東西。
查看完整描述

1 回答

?
烙印99

TA貢獻1829條經驗 獲得超13個贊

在服務器端,問題出在 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));

        }

    }


查看完整回答
反對 回復 2022-10-26
  • 1 回答
  • 0 關注
  • 77 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號