java - 使用 Java 进行 HTTP 请求 - 雅虎

标签 java http httprequest yahoo

我做了一项研究:

  1. http://developer.yahoo.com/blogs/ydn/important-api-updates-changes-8060.html

  2. http://developer.yahoo.com/boss/search/

  3. http://tech.groups.yahoo.com/group/ysearchboss/msearch?query=http+request&submit=Search&charset=windows-1252

  4. http://developer.yahoo.com/java/howto-reqRestJava.html

我发现雅虎在 2008 年对 Java 编程的 http 方法进行了一项更改,即对单词“umbrella”进行的更改:

“字符串请求=”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,但到目前为止我什么也没发现:

“如何在雅虎搜索引擎中从java发出HTTP请求?”

ps:记住雅虎进行了某种加密搜索,像 urlencoder.encode(query) 这样的东西将不起作用。

有人可以告诉我这件事吗?提前致谢! =]

编辑一:在雅虎开发者处找到了这个“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();
}

控制台报告错误:

Exception in thread "AWT-EventQueue-0" com.jellymold.boss.util.BOSSException: IO Exception at com.jellymold.boss.WebSearch.search(WebSearch.java:103) at com.jellymold.boss.WebSearch.search(WebSearch.java:66) at com.sh.st.HttpRequest.(HttpRequest.java:33) at com.sh.st.EventSearch$1.actionPerformed(EventSearch.java:32) at javax.swing.SwingUtilities.notifyAction(Unknown Source) at javax.swing.JComponent.processKeyBinding(Unknown Source) at javax.swing.JComponent.processKeyBindings(Unknown Source) at javax.swing.JComponent.processKeyEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source) at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source) at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) 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) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.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(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) Caused by: java.io.IOException: IO Exceptionboss.yahooapis.com at com.jellymold.boss.util.HTTPRequestImpl.sendGetRequest(HTTPRequestImpl.java:62) at com.jellymold.boss.WebSearch.search(WebSearch.java:92) ... 39 more

编辑两个:

 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() 的值;为零。它位于“Main”类名称 bosssearch 中。这意味着 if 始终不同于 200 并且返回值始终为零...我认为这可能会使代码崩溃,但现在我需要将 java 文件导出为 jar 来替换它,因为它来自项目外部我我遇到了一些问题,你觉得怎么样?

最佳答案

您可以使用javaboss对 Yahoo! 执行搜索搜索引擎:

WebSearch ws = new WebSearch();

ws.search("your_search_keywords_here");

System.out.println("Total hits : " + ws.getTotalResults());

//get a list of results
List<WebSearchResult> results = ws.getResults();

//iterate over the list and print every result title
for(WebSearchResult result : results){
     System.out.println(result.getTitle());
}

但是,您应该将用户代理设置为发出此类请求,而不会出现 403 错误 as i explained in the answer to one of your previous questions (Google search from java request)

关于java - 使用 Java 进行 HTTP 请求 - 雅虎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17380715/

相关文章:

java - 将特定于类和特定于方法的泛型封装在一种类型中

java - 使用 power mockito 抑制私有(private)静态方法

node.js - 将文件流式传输到客户端后,永远不会发送 res.end()

wcf - IIS 上的 WCF 服务出现 HTTP 错误 403.1

java - 轮流从 IP 地址池发送 HTTP 请求以避免限制

java - 我如何使用我们的私钥进行网络服务调用?

java - 词法语法和句法语法有什么区别?

json - 如何在powershell中输出从REST api返回的json对象?

java - 从 ReSTLet 请求中读出自定义 header

java - 在 Java 中请求 HTTPS 并提交表单