DoboWiki
Top
> 自由区/Webサービス紹介/Yahoo!検索Webサービス/ウェブ検索/コード/C# をテンプレートにして作成
自由区/Webサービス紹介/Yahoo!検索Webサービス/ウェブ検索/コード/C# をテンプレートにして作成
開始行:
*Yahoo!検索Webサービス ウェブ検索のサンプルコード(C# )...
Yahoo!検索Webサービスのウェブ検索機能を使って検索を行うサ...
#attach(noform)
#code(csharp){{
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional...
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs...
{
//URLを構築する
string requestUrl =
"http://api.search.yahoo.co.jp/WebSearchServi...
//アプリケーションIDを指定(必ず指定すること)
requestUrl += "?appid=XXXXX";
//検索クエリー。UTF-8エンコードする。
requestUrl += "&query=" + Server.UrlEncode(TextBo...
//検索の種類
if (searchType.SelectedValue != "" && searchType....
requestUrl += "&type=" + searchType.SelectedV...
//表示件数
if (resultsCount.SelectedValue != "" && resultsCo...
requestUrl += "&results=" + resultsCount.Sele...
//開始位置
//requestUrl += "&start=" + "1";
//検索するファイルの種類
if (fileFormat.SelectedValue != "" && fileFormat....
requestUrl += "&format=" + fileFormat.Selecte...
//アダルトコンテンツを含める
if (adultOk.Checked)
requestUrl += "&adult_ok=1";
//同じコンテンツを別の検索結果として表示する
if (similarOk.Checked)
requestUrl += "&similar_ok=1";
//日本語のページのみ検索する(デフォルトでjaとある...
if (englishOnly.Checked)
requestUrl += "&language=ja";
//日本のサイトのみ検索する
if (japanOnly.Checked)
requestUrl += "&country=jp";
//検索するドメインを指定する
if (searchDomain.Text != "")
requestUrl += "&site=" + Server.UrlEncode(sea...
//HttpWebRequestの作成
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequ...
requestUrl);
System.Net.HttpWebResponse response = null;
Yahoo.jp.WebSearchResponse.ResultSet res;
try
{
//レスポンスの取得
response = (System.Net.HttpWebResponse)reques...
System.IO.Stream strm = response.GetResponseS...
//サーバーから返されたXMLデータを解析
System.Xml.Serialization.XmlSerializer serial...
new System.Xml.Serialization.XmlSerializer(
typeof(Yahoo.jp.WebSearchResponse.ResultS...
res = (Yahoo.jp.WebSearchResponse.ResultSet)
serializer.Deserialize(strm);
}
catch (Exception ex)
{
Label1.Text = "エラー:" + Server.HtmlEncode(...
return;
}
finally
{
if (response != null)
response.Close();
}
//結果を表示
Label1.Text = "";
if (res.totalResultsReturned == "0")
{
Label1.Text += "該当するページが見つかりませ...
}
else
{
Label1.Text += "<b>" + Server.HtmlEncode(Text...
"</b> の検索結果 約 " +
res.totalResultsAvailable + "件中 " +
res.firstResultPosition + " - " +
res.totalResultsReturned + " 件目<br />\n";
//検索されたページを列挙
if (res.Result != null)
{
Label1.Text += "<ul>\n";
foreach (Yahoo.jp.WebSearchResponse.Resul...
{
//リンク
Label1.Text += "<li><a href=\"" + ite...
item.Title + "</a>";
//要約
if (item.Summary != null && item.Summ...
Label1.Text += "<br />" + item.Su...
//URL
Label1.Text += "<br /><small>" + item...
//キャッシュ情報
if (item.Cache != null)
{
Label1.Text += " - " + item.Cache...
Label1.Text += " - <a href=\"" + ...
"\">キャッシュ</a>";
}
//更新日
if (item.ModificationDate != null &&
item.ModificationDate.Length > 0)
{
//UNIXタイムスタンプをDateTimeに...
DateTime dt = new DateTime(1970, ...
dt = dt.AddSeconds(double.Parse(i...
Label1.Text += " - " + dt.ToShort...
}
//MIMEタイプ
if (item.MimeType != null &&
item.MimeType.Length > 0)
Label1.Text += " - " + item.MimeT...
Label1.Text += "</small>\n";
}
Label1.Text += "</ul>\n";
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Yahoo!ウェブ検索Webサービスのサンプル</title>
</head>
<body>
<form id="form1" runat="server">
<div>
キーワード :
<asp:TextBox ID="TextBox1" runat="server"></asp:T...
<asp:Button ID="Button1" runat="server" OnClick="...
Text="ウェブ検索" /><br />
キーワードを<asp:CheckBoxList ID="searchType" run...
RepeatDirection="Horizontal"
RepeatLayout="Flow">
<asp:ListItem Selected="True" Value="all">す...
<asp:ListItem Value="any">少なくとも1つ含む</...
<asp:ListItem Value="phrase">文章として含む</...
</asp:CheckBoxList><br />
表示件数 :
<asp:DropDownList ID="resultsCount" runat="server">
<asp:ListItem Value="10">10件</asp:ListItem>
<asp:ListItem Value="25">25件</asp:ListItem>
<asp:ListItem Value="50">50件</asp:ListItem>
</asp:DropDownList><br />
ファイル形式 :
<asp:DropDownList ID="fileFormat" runat="server">
<asp:ListItem Value="any">すべてのファイル形...
<asp:ListItem Value="html">HTML (.htm, .html)...
<asp:ListItem Value="pdf">Adobe PDF (.pdf)</a...
<asp:ListItem Value="xls">Microsoft Excel (.x...
<asp:ListItem Value="ppd">Microsoft Power Poi...
<asp:ListItem Value="msword">Microsoft Word (...
<asp:ListItem Value="rss">RSS/XML (.xml)</asp...
<asp:ListItem Value="txt">テキストフォーマッ...
</asp:DropDownList><br />
<asp:CheckBox ID="adultOk" runat="server"
Text="アダルトコンテンツを含める" /><br />
<asp:CheckBox ID="similarOk" runat="server"
Text="類似の結果を含める" /><br />
<asp:CheckBox ID="englishOnly" runat="server"
Text="日本語のページのみ" /><br />
<asp:CheckBox ID="japanOnly" runat="server"
Text="日本にあるウェブサイトのみ" /><br />
ドメイン :
<asp:TextBox ID="searchDomain" runat="server">
</asp:TextBox><br />
<br />
<asp:Label ID="Label1" runat="server" EnableViewS...
</asp:Label>
</div>
</form>
<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet ...
<span style="margin:15px 15px 15px 15px">
<a href="http://developer.yahoo.co.jp/about">Webサービス ...
</span>
<!-- End Yahoo! JAPAN Web Services Attribution Snippet -->
</body>
</html>
}}
//これより下は編集しないでください
#pageinfo(,2006-09-09 (土) 02:56:50,DOBON!,2016-12-19 (月...
終了行:
*Yahoo!検索Webサービス ウェブ検索のサンプルコード(C# )...
Yahoo!検索Webサービスのウェブ検索機能を使って検索を行うサ...
#attach(noform)
#code(csharp){{
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional...
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs...
{
//URLを構築する
string requestUrl =
"http://api.search.yahoo.co.jp/WebSearchServi...
//アプリケーションIDを指定(必ず指定すること)
requestUrl += "?appid=XXXXX";
//検索クエリー。UTF-8エンコードする。
requestUrl += "&query=" + Server.UrlEncode(TextBo...
//検索の種類
if (searchType.SelectedValue != "" && searchType....
requestUrl += "&type=" + searchType.SelectedV...
//表示件数
if (resultsCount.SelectedValue != "" && resultsCo...
requestUrl += "&results=" + resultsCount.Sele...
//開始位置
//requestUrl += "&start=" + "1";
//検索するファイルの種類
if (fileFormat.SelectedValue != "" && fileFormat....
requestUrl += "&format=" + fileFormat.Selecte...
//アダルトコンテンツを含める
if (adultOk.Checked)
requestUrl += "&adult_ok=1";
//同じコンテンツを別の検索結果として表示する
if (similarOk.Checked)
requestUrl += "&similar_ok=1";
//日本語のページのみ検索する(デフォルトでjaとある...
if (englishOnly.Checked)
requestUrl += "&language=ja";
//日本のサイトのみ検索する
if (japanOnly.Checked)
requestUrl += "&country=jp";
//検索するドメインを指定する
if (searchDomain.Text != "")
requestUrl += "&site=" + Server.UrlEncode(sea...
//HttpWebRequestの作成
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequ...
requestUrl);
System.Net.HttpWebResponse response = null;
Yahoo.jp.WebSearchResponse.ResultSet res;
try
{
//レスポンスの取得
response = (System.Net.HttpWebResponse)reques...
System.IO.Stream strm = response.GetResponseS...
//サーバーから返されたXMLデータを解析
System.Xml.Serialization.XmlSerializer serial...
new System.Xml.Serialization.XmlSerializer(
typeof(Yahoo.jp.WebSearchResponse.ResultS...
res = (Yahoo.jp.WebSearchResponse.ResultSet)
serializer.Deserialize(strm);
}
catch (Exception ex)
{
Label1.Text = "エラー:" + Server.HtmlEncode(...
return;
}
finally
{
if (response != null)
response.Close();
}
//結果を表示
Label1.Text = "";
if (res.totalResultsReturned == "0")
{
Label1.Text += "該当するページが見つかりませ...
}
else
{
Label1.Text += "<b>" + Server.HtmlEncode(Text...
"</b> の検索結果 約 " +
res.totalResultsAvailable + "件中 " +
res.firstResultPosition + " - " +
res.totalResultsReturned + " 件目<br />\n";
//検索されたページを列挙
if (res.Result != null)
{
Label1.Text += "<ul>\n";
foreach (Yahoo.jp.WebSearchResponse.Resul...
{
//リンク
Label1.Text += "<li><a href=\"" + ite...
item.Title + "</a>";
//要約
if (item.Summary != null && item.Summ...
Label1.Text += "<br />" + item.Su...
//URL
Label1.Text += "<br /><small>" + item...
//キャッシュ情報
if (item.Cache != null)
{
Label1.Text += " - " + item.Cache...
Label1.Text += " - <a href=\"" + ...
"\">キャッシュ</a>";
}
//更新日
if (item.ModificationDate != null &&
item.ModificationDate.Length > 0)
{
//UNIXタイムスタンプをDateTimeに...
DateTime dt = new DateTime(1970, ...
dt = dt.AddSeconds(double.Parse(i...
Label1.Text += " - " + dt.ToShort...
}
//MIMEタイプ
if (item.MimeType != null &&
item.MimeType.Length > 0)
Label1.Text += " - " + item.MimeT...
Label1.Text += "</small>\n";
}
Label1.Text += "</ul>\n";
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Yahoo!ウェブ検索Webサービスのサンプル</title>
</head>
<body>
<form id="form1" runat="server">
<div>
キーワード :
<asp:TextBox ID="TextBox1" runat="server"></asp:T...
<asp:Button ID="Button1" runat="server" OnClick="...
Text="ウェブ検索" /><br />
キーワードを<asp:CheckBoxList ID="searchType" run...
RepeatDirection="Horizontal"
RepeatLayout="Flow">
<asp:ListItem Selected="True" Value="all">す...
<asp:ListItem Value="any">少なくとも1つ含む</...
<asp:ListItem Value="phrase">文章として含む</...
</asp:CheckBoxList><br />
表示件数 :
<asp:DropDownList ID="resultsCount" runat="server">
<asp:ListItem Value="10">10件</asp:ListItem>
<asp:ListItem Value="25">25件</asp:ListItem>
<asp:ListItem Value="50">50件</asp:ListItem>
</asp:DropDownList><br />
ファイル形式 :
<asp:DropDownList ID="fileFormat" runat="server">
<asp:ListItem Value="any">すべてのファイル形...
<asp:ListItem Value="html">HTML (.htm, .html)...
<asp:ListItem Value="pdf">Adobe PDF (.pdf)</a...
<asp:ListItem Value="xls">Microsoft Excel (.x...
<asp:ListItem Value="ppd">Microsoft Power Poi...
<asp:ListItem Value="msword">Microsoft Word (...
<asp:ListItem Value="rss">RSS/XML (.xml)</asp...
<asp:ListItem Value="txt">テキストフォーマッ...
</asp:DropDownList><br />
<asp:CheckBox ID="adultOk" runat="server"
Text="アダルトコンテンツを含める" /><br />
<asp:CheckBox ID="similarOk" runat="server"
Text="類似の結果を含める" /><br />
<asp:CheckBox ID="englishOnly" runat="server"
Text="日本語のページのみ" /><br />
<asp:CheckBox ID="japanOnly" runat="server"
Text="日本にあるウェブサイトのみ" /><br />
ドメイン :
<asp:TextBox ID="searchDomain" runat="server">
</asp:TextBox><br />
<br />
<asp:Label ID="Label1" runat="server" EnableViewS...
</asp:Label>
</div>
</form>
<!-- Begin Yahoo! JAPAN Web Services Attribution Snippet ...
<span style="margin:15px 15px 15px 15px">
<a href="http://developer.yahoo.co.jp/about">Webサービス ...
</span>
<!-- End Yahoo! JAPAN Web Services Attribution Snippet -->
</body>
</html>
}}
//これより下は編集しないでください
#pageinfo(,2006-09-09 (土) 02:56:50,DOBON!,2016-12-19 (月...
ページ名:
▲
▼
[
トップ
] [
新規
|
子ページ作成
|
一覧
|
単語検索
|
最終更新
|
ヘルプ
]