亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網為廣大站長提供免費收錄網站服務,提交前請做好本站友鏈:【 網站目錄:http://www.430618.com 】, 免友鏈快審服務(50元/站),

點擊這里在線咨詢客服
新站提交
  • 網站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會員:747

本文介紹了使用HttpURLConnection執行cURL命令的Java返回204(HTTP_NO_CONTENT)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我使用的是Java 11。在命令行上執行時,我有以下cURL命令:

curl --location --request GET 'http://xxx.xxx.co.za:8080/document/details/Select+docId+From+%27Workflow+Club%2FCustomer+Invoices%27+where+recursive+%3D+true+and+invoice_number+%3D%271221669023%27' --header 'Authorization: Basic xxx'

返回以下內容:

{errorMessage: 'PaperTrail API only available in enterprise edition'}

但是,當我嘗試使用HttpURLConnection在Java應用程序中執行相同的URL時,它返回空白響應。

private static final String USER_AGENT = "Mozilla/5.0";

private static final String GET_URL = "http://xxx.xxx.co.za:8080/document/details/";
private static final String GET_URL_QRY = "Select docId From 'Workflow Club/Customer Invoices' where recursive = true and invoice_number =':1'";
private static final String GET_AUTH_ENC = "Basic xxx";

@Override
public String getDocId(Long invoiceNumber) {
    String get_url_qry = StringUtils.replace(GET_URL_QRY, ":1", Long.toString(invoiceNumber));
    get_url_qry = URLEncoder.encode(get_url_qry, StandardCharsets.UTF_8);
    final String get_url = GET_URL+get_url_qry;
    try {
        URL url = new URL(get_url);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty("Authorization", GET_AUTH_ENC);
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", USER_AGENT);
        int responseCode = con.getResponseCode();
        logger.info(get_url+" -> GET Response Code :: " + responseCode);
        if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) { // success
            BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            String resp = response.toString();
            logger.info(responseCode+" Response: '"+resp+"'.");
        } else {
            logger.error("GET request did not work (responseCode: "+responseCode+").");
        }
    } catch (MalformedURLException e) {
        logger.error("MalformedURLException creating URL '"+get_url+"'. "+e.getMessage());
    } catch (IOException e) {
        logger.error("IOException creating connection from URL '"+get_url+"'. "+e.getMessage());
    }
    return null;
}

以空白響應輸出以下內容:

204 Response: ''.

問題

如何使Java應用程序也返回與命令行調用相同的結果?

更新

我有一個不同的POST URL,我也需要調用它,我可以成功地調用它。所以我的Get調用有問題。

private static final String USER_AGENT = "Mozilla/5.0";

例如,Get調用返回204,沒有內容。

private String getDocId(Long invoiceNumber) {
    String get_url_qry = StringUtils.replace(GET_URL_QRY, ":1", Long.toString(invoiceNumber));
    get_url_qry = URLEncoder.encode(get_url_qry, StandardCharsets.UTF_8);
    final String get_url = GET_URL+get_url_qry;
    try {
        URL url = new URL(get_url);
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestProperty("Authorization", GET_AUTH_ENC);
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", USER_AGENT);
        con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        Map<String,String> data = handleResponse(con);
        return data.get("docId");
    } catch (MalformedURLException e) {
        logger.error("MalformedURLException creating URL '"+get_url+"'. "+e.getMessage());
    } catch (IOException e) {
        logger.error("IOException creating connection from URL '"+get_url+"'. "+e.getMessage());
    }
    return null;
}

返回200的POST調用和預期內容。

private String getDocLink(String docId) {
    if (StringUtils.isNotBlank(docId)) {
        try {
            URL url = new URL(POST_URL);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestProperty("Authorization", GET_AUTH_ENC);
            con.setRequestMethod("POST");
            con.setRequestProperty("User-Agent", USER_AGENT);
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            byte[] postDataBytes = getPostData(docId);
            con.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
            con.setDoOutput(true);
            con.getOutputStream().write(postDataBytes);
            Map<String,String> data = handleResponse(con);
            return data.get("url");
        } catch (IOException e) {
            logger.error("IOException creating connection from URL '"+POST_URL+"'. "+e.getMessage());
        }
    } else {
        logger.error("No docId provided when trying to get a document link.");
    }
    return null;
}

所以看到POST調用起作用了,我想我一定是GET調用出了什么問題。

推薦答案

您是否嘗試在您的Java代碼中設置cURL將使用的相同用戶代理?類似curl/7.37.0的內容?
就我所知,這應該就是所有的不同之處。在重定向之后,撇開卷曲。但由于沒有重定向,我猜可能是用戶代理起了作用。

有很多服務器應用程序在被瀏覽器調用時表現不同(就像您通過將User-Agent設置為Mozilla/5.0來使其思考一樣),而不是像cURL這樣的其他應用程序。

這篇關于使用HttpURLConnection執行cURL命令的Java返回204(HTTP_NO_CONTENT)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,

分享到:
標簽:curl HTTP_NO_CONTENT HttpURLConnection Java 命令 執行 返回
用戶無頭像

網友整理

注冊時間:

網站:5 個   小程序:0 個  文章:12 篇

  • 51998

    網站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會員

趕快注冊賬號,推廣您的網站吧!
最新入駐小程序

數獨大挑戰2018-06-03

數獨一種數學游戲,玩家需要根據9

答題星2018-06-03

您可以通過答題星輕松地創建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學四六

運動步數有氧達人2018-06-03

記錄運動步數,積累氧氣值。還可偷

每日養生app2018-06-03

每日養生,天天健康

體育訓練成績評定2018-06-03

通用課目體育訓練成績評定