Top / Java / HttpComponents

HttpComponents? はApacheが提供する、HTTPプロトコルをあつかって通信を行うためのライブラリです。これを用いると簡単にHTTPリクエストを送信したり、レスポンスを受信して処理したり出来ます

ダウンロード

http://hc.apache.org/downloads.cgi より

をダウンロードします。上記ライブラリはまた下記のCommonsのライブラリに依存しているので http://commons.apache.org/components.html より

をダウンロードします。

サンプル

GoogleのGoogle AJAX Search APIに対してリクエストを行い、JSON形式のStringを取得してみました。

import java.io.IOException;
import java.net.URLEncoder;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

public class HTTPMain {
  public static void main(String[] args) throws ClientProtocolException,
      IOException {
    String input = URLEncoder.encode("きのさいと", "UTF-8");
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet(
        "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="
            + input);
    System.out.println("executing request " + httpget.getURI());
    // Create a response handler
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = httpclient.execute(httpget, responseHandler);
    System.out.println(responseBody);
  }
}

実行結果は以下の通り(色々整形してます)

executing request http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=%E3%81%8D%E3%81%AE%E3%81%95%E3%81%84%E3%81%A8
{"responseData": 
  {"results":[
    {"GsearchResultClass":"GwebSearch",
     "unescapedUrl":"http://www.masatom.in/pukiwiki/index.php?FrontPage",
     "url":"http://www.masatom.in/pukiwiki/index.php%3FFrontPage",
     "visibleUrl":"www.masatom.in",
     "cacheUrl":"http://www.google.com/search?q\u003dcache:qWbUEoHWZdoJ:www.masatom.in",
     "title":"FrontPage - \u003cb\u003eきのさいと\u003c/b\u003e",
     "titleNoFormatting":"FrontPage - きのさいと",
     "content":"ようこそ「\u003cb\u003eきのさいと\u003c/b\u003e」へ †. このサイトはMasatomi KINOの備忘録として立ち上げた  サイトです。 },
    {"GsearchResultClass":"GwebSearch",
     "unescapedUrl":"http://www.masatom.in/pukiwiki/index.php?Eclipse%2F%A5%D7%A5%E9%A5%B0%A5%A4%A5%F3%B3%AB%C8%AF%A4%CETIPS%BD%B8",
     "url":"http://www.masatom.in/pukiwiki/index.php%3FEclipse%252F%25A5%25D7%25A5%25E9%25A5%25B0%25A5%25A4%25A5%25F3%25B3%25AB%25
              C8%25AF%25A4%25CETIPS%25BD%25B8",
     "visibleUrl":"www.masatom.in",
     "cacheUrl":"http://www.google.com/search?q\u003dcache:_VnlmpUNXRwJ:www.masatom.in",
     "title":"Eclipse/プラグイン開発のTIPS集 - \u003cb\u003eきのさいと\u003c/b\u003e",
     "titleNoFormatting":"Eclipse/プラグイン開発のTIPS集 - きのさいと",
     "content":"自体がこのプラグインのクラスローダでロードされるから、Springを使用したいプラグ  イン側のクラスが見えないっ。。}
    ],
    "cursor":
      {"pages":[
        {"start":"0","label":1},
        {"start":"4","label":2},
        {"start":"8","label":3},
        {"start":"12","label":4},
        {"start":"16","label":5},
        {"start":"20","label":6},
        {"start":"24","label":7},
        {"start":"28","label":8}
        ],
      "estimatedResultCount":"560000",
      "currentPageIndex":0,
      "moreResultsUrl":"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0
                       \u0026hl\u003den\u0026q\u003d%E3%81%8D%E3%81%AE%E3%81%95%E3%81%84%E3%81%A8"
      }
  },
  "responseDetails": null,
  "responseStatus": 200
}

JSON形式のStringが取得できました。

関連リンク


この記事は

選択肢 投票
おもしろかった 0  
そうでもない 0  

Top / Java / HttpComponents

現在のアクセス:13548


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS