Get在Curl和Java中工作正常,但是在有诸如from和to之类的参数时效果不佳

时间:2019-05-27 10:03:39

标签: java url curl

因此,我使用from和to来卷曲URL以查询参数,它工作正常。但是,当我对Java执行相同操作时,会导致422错误。 当我正常使用不带from和to的URL时,它在java中返回正常。 因此,仅通过添加和添加为什么我会收到422 http错误?

我的网址中包含from和to的内容。

public Pair<Integer, JSONObject> GetJO(String httpURL, String authorization) throws IOException {
    httpUrl = "www.myurl.com/path/to/file?from=10&to=30";
    URL url = new URL(httpURL);
    HttpsURLConnection conn = null;
    Pair<Integer, JSONObject> resPair = null;

    authorization = "secret authorization";
    String orgid = "secret orgid";
    try {
        SSLContext sslContext = TrustSSLCertificates();
        HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
        try {
            conn = (HttpsURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setDoOutput(true);
            conn.setConnectTimeout(TIMEOUTVAL);
            conn.setReadTimeout(TIMEOUTVAL);
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setRequestProperty("Authorization", authorization);
            conn.setRequestProperty("orgId", orgid);
        } catch (IOException e) {
            Logger.getLogger(RequestHttp.class.getName()).log(Level.SEVERE, null, e.getMessage());
        }

        BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        String data = br.readLine();

        JSONObject jsonArrayData = new JSONObject(data);
        resPair = new Pair<>(conn.getResponseCode(), jsonArrayData);
    } catch (Exception e) {
        System.out.println("ERROR GET JO " + httpURL);
        e.printStackTrace();
        Logger.getLogger(RequestHttp.class.getName()).log(Level.SEVERE, null, e.getMessage());
    }

    return resPair;
}

错误在下面

java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1913)
    at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1509)
    at java.base/sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:245)
    a

0 个答案:

没有答案