簡體   English   中英

java.io.IOException:服務器為URL返回HTTP響應代碼:400

[英]java.io.IOException: Server returned HTTP response code: 400 for URL:

我正在執行GET請求,並且此代碼適用於我們所有的現有測試,並且為新端點給出了400錯誤。 我可以在郵遞員那里達到終點。 conn.getErrorStream()不輸出任何內容。 代碼似乎在url.openConnection上失敗(沒有引發異常)。 如果我在該行之后立即執行conn.getResponseCode(),它將返回400。我認為這可能不喜歡查詢中的管道字符,而是通過URLEncoder.encode(resourceContext)以及URLEncoder.encode(baseUrl)對其進行編碼+ resourceContext)沒有幫助,最終拋出MalformedURLException。 這是我要命中的端點(匿名): https ://usqa0003d.usa.company.com:17462/positions/1535000400000/CUR/USA|415|415|CUST|GL53I7TPYBYM|MATCHED

當我將調試器連接到服務器時,我不想執行任何操作,因此請求永遠不會到達服務器。 我進入openConnection,這會導致充滿var5,var6等的兔子洞。我切換到HttpsURLConnection,但那里也沒有骰子。 還有其他建議嗎?

URL url = new URL(baseUrl + resourceContext);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod(requestType);
        if(messageLibraryJson) {
            conn.setRequestProperty("Content-Type", "application/json;class=true");
            conn.setRequestProperty("Accept", "application/json;class=true");
        } else {
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Accept", "application/json");
        }

        String userpass = username + ":" + password;
        String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes());
        conn.setRequestProperty("Authorization", basicAuth);

        if(requestType.equals("POST")) {
            OutputStream os = conn.getOutputStream();
            os.write(requestJson.getBytes());
            os.flush();
        }

        if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
            logger.error("ERROR in REST CLIENT");
            logger.error("URL = " + baseUrl + resourceContext);
            logger.error("Request = " + requestJson);
            throw new RuntimeException("Failed : HTTP error code : "
                    + conn.getResponseCode());
        }

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

確實是“ |” 炭引起麻煩。 如果我執行以下操作:

        String urlString = (baseUrl + resourceContext).replace("|", "%7C");
        URL url = new URL(urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

有用。 產生以下urlString: https ://usqa0003d.usa.company.com:17462/positions/1535000400000/CUR/USA%7C415%7C415%7CCUST%7CGL53I7TPYBYM%7CMATCHED

但是,當我這樣做時:

String enc = baseUrl + URLEncoder.encode(resourceContext);

我得到: https : //clqa0003d.usa.company.com : 17462/positions%2F1535000400000%2FCUR%2FUSA%7C415%7C415%7CCUST%7CGL53I7TPYBYM%7CMATCHED

'/'被'%2F'代替(除了管道字符),並且由於某種原因會引起麻煩。 作為參考,端點實際上是這樣定義的:

@RequestMapping(value = "/{param1}/{param2}/{param3:.*}", method = RequestMethod.GET)

使用類級別的終結點(如何稱呼??)為:

@RequestMapping(value = "/positions",
    produces = {"application/vnd.cme.cdo+json", "application/json;class=true", "application/x-protobuf", "application/x-protobuf-text"},
    consumes = {"application/vnd.cme.cdo+json", "application/json;class=true", "application/x-protobuf", "application/x-protobuf-text"})
public class PositionController implements IPositionController {

誰能評論為什么將'/'轉換為'%2F'會引起麻煩?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM