如何使用 Falcon RESTful API 获取 URI
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了两个小问题之外,您已经完成了所有繁重的工作:
URLEncoder.encode(...)
。 Javadoc 说它翻译了字符串转换为application/x-www-form-urlencoded
格式,即在执行POST
时。astring.sendGetRequest(...)
而不是astring
本身作为结果。以下作品:
You've done all the heavy lifting except two small problems:
URLEncoder.encode(...)
should not be used here. Javadoc says it translates a string intoapplication/x-www-form-urlencoded
format, i.e. when doingPOST
.astring.sendGetRequest(...)
instead ofastring
itself should be used as result.Following works: