【问题标题】:Need a rest call from java需要来自java的休息电话
【发布时间】:2015-11-14 04:50:58
【问题描述】:

我正在作为客户端调用 API。我使用 java 来做到这一点。我现在只需要一个回应。稍后我将转换输出。 我的代码如下:

String data = "User=admin&Password=1234&Authorization=basic&Keyword=nana";
URL url;
try {
    url = new URL("http://119.235.102.65/library/index.php/API/Search/basic");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");

        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

    int responseCode = conn.getResponseCode();
        System.out.println("Response code: " + responseCode);

        boolean isSuccesResponse = responseCode < 400;

        InputStream responseStream = isSuccesResponse ? conn.getInputStream() : conn.getErrorStream();

        wr.close();
         } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

我收到错误代码 401。

my url is : http://119.235.102.65/library/index.php/API/Search/basic
my username is : admin
my password is : 1234

我还要从数据库中搜索一个关键字,即 nana。 关键字 = 娜娜。

【问题讨论】:

  • 是的。它需要授权。
  • 在您的代码示例中,您没有将凭据作为请求的一部分发送。这些可能需要作为标头或请求参数发送。

标签: java api rest restful-authentication restful-architecture


【解决方案1】:

实际上我使用谷歌浏览器的 POSTMAN REST 客户端尝试了你的 REST API,但你有错误:

不要使用Keyword=nana

使用:keyword=nana

答案是:

{
    "responsecode": 0,
    "responsemessage": "Success",
    "result": [
        {
            "BOOKID": "1",
            "BOOKTITLE": "Nana in the City",
            "BOOKCATALOGNUMBER": "abc 123",
            "BOOKDESCRIPTION": "In this magical picture book, a young boy spends an overnight visit with his nana and is frightened to find that the city where she lives is filled with noise and crowds and scary things. But then Nana makes him a special cape to help him be brave, and soon the everyday sights, sounds, and smells of the city are not scary—but wonderful. The succinct text is paired with watercolor illustrations that capture all the vitality, energy, and beauty of the city.",
            "PUBLISHERNAME": "Clarion Books",
            "PUBLISHERCITY": "New York",
            "TOTALCOPIES": "2",
            "AVAILABLECOPIES": "1",
            "LOANEDCOPIES": "1",
            "AUTHOR": [
                {
                    "AUTHORFNAME": "Lauren",
                    "AUTHORMNAME": null,
                    "AUTHORLNAME": "Castilo"
                }
            ],
            "BOOKCOPIES": [
                {
                    "REFERENCENUMBER": "123456",
                    "COPYNUMBER": "1",
                    "ISBN": "978-0544104433",
                    "EDITION": "1",
                    "PUBLICATIONYEAR": "2014",
                    "LOANEDSTATUS": "1"
                },
                {
                    "REFERENCENUMBER": "1234567",
                    "
...
...

Image

【讨论】:

    猜你喜欢
    • 2018-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多