// 下階層用テンプレート
#topicpath
----
//ここにコンテンツを記述します。

[[HttpComponentsw>http://hc.apache.org/]]はApacheが提供する、HTTPプロトコルを操作して通信を行う時に便利なライブラリです。これを用いると簡単にHTTPリクエストを送信したり、レスポンスを受信して処理したり出来ます
[[HttpComponents>http://hc.apache.org/]]はApacheが提供する、HTTPをつかって通信をおこなうプログラムを書くときに便利なJavaのライブラリです。これを用いると簡単にHTTPリクエストを送信したり、レスポンスを受信して処理したり出来ます。


**ダウンロード [#k8d8673d]
http://hc.apache.org/downloads.cgi
より

-httpcore-4.0.jar
-httpclient-4.0-beta2.jar

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

をダウンロードします。

**サンプル [#v760fcc2]
Googleの[[Google AJAX Search API>http://code.google.com/intl/ja/apis/ajaxsearch/documentation/#fonje]]に対してリクエストを行い、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が取得できました。


**関連リンク [#p93d450f]
-[[Google AJAX Search API>http://code.google.com/intl/ja/apis/ajaxsearch/documentation/#fonje]]
-[[HttpCore Tutorial>http://hc.apache.org/httpcomponents-core/tutorial/html/index.html]]








----
この記事は
#vote(おもしろかった,そうでもない)

#comment
#topicpath


SIZE(10){現在のアクセス:&counter;}

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS