我做了一项研究:
我发现在 2008 年,Yahoo 对 java 编程的 http 方法进行了一项更改,这曾经是为“伞”这个词所做的:
"字符串请求 = " http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=YahooDemo&query=umbrella&results=10 ";"
关于:
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(request);
// Send GET request
int statusCode = client.executeMethod(method);
等等..现在他们有 BOSS 搜索 API 但直到现在我什么都没发现:
“如何在 Yahoo 搜索引擎中从 Java 发出 HTTP 请求?”
ps:请记住,雅虎会进行某种加密搜索,而 urlencoder.encode(query) 之类的东西将不起作用。
有人对我有意见吗?提前致谢!=]
编辑一:在 yahoo developer 找到这个“ http://developer.yahoo.com/boss/search/boss_api_guide/codeexamples.html#oauth_java ”,我正在尝试回答我自己的问题。
编辑一:
try {
WebSearch ws = new WebSearch();
ws.search(userQuery);
List<WebSearchResult> results = ws.getResults();
for(WebSearchResult result : results){
System.out.println(result.getTitle());
}
// Setup connection properties (this doesn't open the connection)
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)");
//Setup a reader
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
//Read line by line
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println (line);
}
//Close connection
reader.close();
}
控制台报错:
com.jellymold.boss.util.BOSSException 线程“AWT-EventQueue-0”中的异常:com.jellymold.boss.WebSearch.search(WebSearch.java:103) 处 com.jellymold.boss.WebSearch.search( WebSearch.java:66) 在 com.sh.st.HttpRequest.(HttpRequest.java:33) 在 com.sh.st.EventSearch$1.actionPerformed(EventSearch.java:32) 在 javax.swing.SwingUtilities.notifyAction(Unknown源) 在 javax.swing.JComponent.processKeyBinding(Unknown Source) 在 javax.swing.JComponent.processKeyBindings(Unknown Source) 在 javax.swing.JComponent.processKeyEvent(Unknown Source) 在 java.awt.Component.processEvent(Unknown Source)在 java.awt.Container.processEvent(Unknown Source) 在 java.awt.Component.dispatchEventImpl(Unknown Source) 在 java.awt.Container.dispatchEventImpl(Unknown Source) 在 java.awt.Component。dispatchEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.redispatchEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(未知源)在 java.awt.DefaultKeyboardFocusManager.dispatchEvent(未知源)在 java.awt.Component.dispatchEventImpl(未知源)在 java.awt.Container.dispatchEventImpl(未知源)在 java.awt.Window.dispatchEventImpl(未知源) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source ) 在 java.awt.EventQueue$3。在 java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 在 java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) 在 java.awt.EventQueue$4 的 java.security.AccessController.doPrivileged(Native Method) 运行(Unknown Source) .run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue .dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents (未知来源)在 java.awt。EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) 原因:java.io.IOException: IO Exceptionboss.yahooapis.com at com.jellymold.boss.util.HTTPRequestImpl.sendGetRequest(HTTPRequestImpl.java :62) 在 com.jellymold.boss.WebSearch.search(WebSearch.java:92) ... 39 更多
编辑二:
public int sendGetRequest(String url) throws IOException{
//int ret = 500;
try {
URL u = new URL(url);
HttpURLConnection uc = (HttpURLConnection) u.openConnection();
uc.setRequestProperty("User-Agent", "Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)");
// ret = uc.getResponseCode();
// if(200==ret){
BufferedReader rd = new BufferedReader(new InputStreamReader(uc.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
sb.append(line);
// }
rd.close();
setResponseBody(sb.toString());
}
}catch (MalformedURLException ex) {
throw new IOException(url+" is not valid");
}catch (IOException ie) {
throw new IOException("IO Exception" + ie.getMessage());
}
return ret=0;
}
查看来自 javaboss API 的这段代码——我对其进行了更改以设置阅读器——来自 uc.getResponseCode() 的值;为零。它在“主要”类名bosssearch中。这意味着 if 始终不同于 200 并且返回值始终为零...我认为这可能会使代码崩溃,但现在我需要将 java 文件导出为 jar 来替换它,因为它来自项目之外我遇到了一些问题,你怎么看?