如何使用 Falcon RESTful API 获取 URI

发布于 2024-12-22 21:24:29 字数 1878 浏览 1 评论 0原文

我正在使用 FALCON 语义搜索引擎 RESTful API 和写了这个程序 但没有得到应该从搜索引擎响应的结果。请查看代码&帮我。

package httpProject;

import java.io.*;
import java.net.*;
import java.lang.*;

public class HTTPRequestPoster {
    public String sendGetRequest(String endpoint, String requestParameters) {
        String result = null;
        if (endpoint.startsWith("http://")) {
            try {
                String urlStr = endpoint;
                if (requestParameters != null && requestParameters.length () > 0) {
                    urlStr += "?" + requestParameters;
                }
                URL url = new URL(urlStr);
                URLConnection conn = url.openConnection ();

                // Get the response
                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuffer sb = new StringBuffer();
                String line;
                while ((line = rd.readLine()) != null) {
                    sb.append(line);
                }
                rd.close();
                result = sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * @param args
     * @throws UnsupportedEncodingException 
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        // TODO Auto-generated method stub
        //HTTPRequestPoster a = new HTTPRequestPoster();//
        HTTPRequestPoster astring = new HTTPRequestPoster ();
        String param = "query=Person";
        String stringtoreverse = URLEncoder.encode(param, "UTF-8");
        astring.sendGetRequest("http://ws.nju.edu.cn/falcons/api/classsearch.jsp", stringtoreverse);
        astring.toString();

        System.out.println(astring);
        //PrintStream.class.toString();
    }
}

I m using FALCON semantic search engine RESTful API & wrote this program
But not getting the results that should respond from Search engine.Please look at code & help me.

package httpProject;

import java.io.*;
import java.net.*;
import java.lang.*;

public class HTTPRequestPoster {
    public String sendGetRequest(String endpoint, String requestParameters) {
        String result = null;
        if (endpoint.startsWith("http://")) {
            try {
                String urlStr = endpoint;
                if (requestParameters != null && requestParameters.length () > 0) {
                    urlStr += "?" + requestParameters;
                }
                URL url = new URL(urlStr);
                URLConnection conn = url.openConnection ();

                // Get the response
                BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                StringBuffer sb = new StringBuffer();
                String line;
                while ((line = rd.readLine()) != null) {
                    sb.append(line);
                }
                rd.close();
                result = sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    /**
     * @param args
     * @throws UnsupportedEncodingException 
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        // TODO Auto-generated method stub
        //HTTPRequestPoster a = new HTTPRequestPoster();//
        HTTPRequestPoster astring = new HTTPRequestPoster ();
        String param = "query=Person";
        String stringtoreverse = URLEncoder.encode(param, "UTF-8");
        astring.sendGetRequest("http://ws.nju.edu.cn/falcons/api/classsearch.jsp", stringtoreverse);
        astring.toString();

        System.out.println(astring);
        //PrintStream.class.toString();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

﹏雨一样淡蓝的深情 2024-12-29 21:24:29

除了两个小问题之外,您已经完成了所有繁重的工作:

  • 此处不应使用

    URLEncoder.encode(...)Javadoc 说它翻译了字符串转换为 application/x-www-form-urlencoded 格式,即在执行 POST 时。

  • 应该使用

    astring.sendGetRequest(...) 而不是 astring 本身作为结果。

以下作品:

public static void main(String[] args) throws UnsupportedEncodingException {
    // TODO Auto-generated method stub
    //HTTPRequestPoster a = new HTTPRequestPoster();//
    HTTPRequestPoster astring = new HTTPRequestPoster ();
    String param = "query=Person";
    String result = astring.sendGetRequest("http://ws.nju.edu.cn/falcons/api/classsearch.jsp", param);

    System.out.println(result);
}

You've done all the heavy lifting except two small problems:

  • URLEncoder.encode(...) should not be used here. Javadoc says it translates a string into application/x-www-form-urlencoded format, i.e. when doing POST.

  • astring.sendGetRequest(...) instead of astring itself should be used as result.

Following works:

public static void main(String[] args) throws UnsupportedEncodingException {
    // TODO Auto-generated method stub
    //HTTPRequestPoster a = new HTTPRequestPoster();//
    HTTPRequestPoster astring = new HTTPRequestPoster ();
    String param = "query=Person";
    String result = astring.sendGetRequest("http://ws.nju.edu.cn/falcons/api/classsearch.jsp", param);

    System.out.println(result);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文