如何使用httpurlconnection get和参数从couchdb获取数据

rlcwz9us  于 2021-07-06  发布在  Java
关注(0)|答案(0)|浏览(229)

我正在努力使用httpurlconnection发送以下查询。如有任何建议,将不胜感激。
下面是查询(使用linux命令行中的curl可以正常工作)

https://USER:PASSWORD@HO`enter code here`ST/DATABASE/_partition/PARTITION_NAME/_all_docs?limit=1&descending=true

这里是我迄今为止在java中所做的(尝试只使用“标准”java库)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class CouchDBConnector {
    private static final String USER_AGENT = "Mozilla/5.0";
    private static String GET_URL = "https://USER:PASSWORD@HOST";

    public static void main(String[] args) throws IOException {
        sendHttpGETRequest();
    }

    private static void sendHttpGETRequest() throws IOException {
        URL obj = new URL(GET_URL);
        HttpURLConnection httpURLConnection = (HttpURLConnection) obj.openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setRequestProperty("User-Agent", USER_AGENT);
        int responseCode = httpURLConnection.getResponseCode();
        System.out.println("GET Response Code :: " + responseCode);
        if (responseCode == HttpURLConnection.HTTP_OK) { // success
            BufferedReader in = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            // print result
            System.out.println(response.toString());
        } else {
            System.out.println("GET request not worked");
        }
        for (int i = 1; i <= 8; i++) {
            System.out.println(httpURLConnection.getHeaderFieldKey(i) + " = " + httpURLConnection.getHeaderField(i));
        }
    }
}

这给了我成功的返回码,欢迎信息等,但是,我怎么添加

/DATABASE/_partition/PARTITION_NAME/_all_docs?limit=1&descending=true

对请求的回应?

------------ouput of above program--------------
GET Response Code :: 200
{"couchdb":"Welcome","version":"3.1.1","git_sha":"*********","uuid":"**************","features":["access-ready","partitioned","pluggable-storage-engines","reshard","scheduler"],"vendor":{"name":"The Apache Software Foundation"}}
Server = nginx/1.14.0 (Ubuntu)
Date = Thu, 10 Dec 2020 19:18:26 GMT
Content-Type = application/json
Content-Length = 251
Connection = keep-alive
Cache-Control = must-revalidate
X-Couch-Request-ID =**********
X-CouchDB-Body-Time = 0

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题