- 追加された行はこの色です。
- 削除された行はこの色です。
*Yahoo!検索Webサービス ウェブ検索のサンプルコード(VB.NET ) [#taf48b2c]
#code(vbnet){{
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'URLを構築する
Dim requestUrl As String = _
"http://api.search.yahoo.co.jp/WebSearchService/V1/webSearch"
'アプリケーションIDを指定(必ず指定すること)
requestUrl += "?appid=XXXXX"
'検索クエリー。UTF-8エンコードする。
requestUrl += "&query=" + Server.UrlEncode(TextBox1.Text)
'検索の種類
If searchType.SelectedValue <> "" AndAlso _
searchType.SelectedValue <> "all" Then
requestUrl += "&type=" + searchType.SelectedValue
End If
'表示件数
If resultsCount.SelectedValue <> "" AndAlso _
resultsCount.SelectedValue <> "10" Then
requestUrl += "&results=" + resultsCount.SelectedValue
End If
'開始位置
'requestUrl += "&start=" + "1";
'検索するファイルの種類
If fileFormat.SelectedValue <> "" AndAlso _
fileFormat.SelectedValue <> "any" Then
requestUrl += "&format=" + fileFormat.SelectedValue
End If
'アダルトコンテンツを含める
If adultOk.Checked Then
requestUrl += "&adult_ok=1"
End If
'同じコンテンツを別の検索結果として表示する
If similarOk.Checked Then
requestUrl += "&similar_ok=1"
End If
'日本語のページのみ検索する(デフォルトでjaとあるのだが...)
If englishOnly.Checked Then
requestUrl += "&language=ja"
End If
'日本のサイトのみ検索する
If japanOnly.Checked Then
requestUrl += "&country=jp"
End If
'検索するドメインを指定する
If searchDomain.Text <> "" Then
requestUrl += "&site=" + Server.UrlEncode(searchDomain.Text)
End If
'HttpWebRequestの作成
Dim request As System.Net.HttpWebRequest = _
CType(System.Net.WebRequest.Create(requestUrl), _
System.Net.HttpWebRequest)
Dim response As System.Net.HttpWebResponse = Nothing
Dim res As Yahoo.jp.WebSearchResponse.ResultSet
Try
'レスポンスの取得
response = CType(request.GetResponse(), _
System.Net.HttpWebResponse)
Dim strm As System.IO.Stream = response.GetResponseStream()
'サーバーから返されたXMLデータを解析
Dim serializer As New System.Xml.Serialization.XmlSerializer( _
GetType(Yahoo.jp.WebSearchResponse.ResultSet))
res = CType(serializer.Deserialize(strm), _
Yahoo.jp.WebSearchResponse.ResultSet)
Catch ex As Exception
Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message)
Return
Finally
If Not (response Is Nothing) Then
response.Close()
End If
End Try
'結果を表示
Label1.Text = ""
Label1.Text += "<b>" + Server.HtmlEncode(TextBox1.Text) + _
"</b> の検索結果 約 " + res.totalResultsAvailable + _
"件中 " + res.firstResultPosition + " - " + _
res.totalResultsReturned + " 件目<br />" + vbLf
'検索されたページを列挙
If Not (res.Result Is Nothing) Then
Label1.Text += "<ul>" + vbLf
Dim item As Yahoo.jp.WebSearchResponse.ResultType
For Each item In res.Result
'リンク
Label1.Text += "<li><a href=""" + item.ClickUrl + _
""">" + item.Title + "</a>"
'要約
If Not (item.Summary Is Nothing) AndAlso _
item.Summary.Length > 0 Then
Label1.Text += "<br />" + item.Summary
End If 'URL
Label1.Text += "<br /><small>" + item.Url
'キャッシュ情報
If Not (item.Cache Is Nothing) Then
Label1.Text += " - " + item.Cache.Size + "byte"
Label1.Text += " - <a href=""" + _
item.Cache.Url + """>キャッシュ</a>"
End If
'更新日
If Not (item.ModificationDate Is Nothing) AndAlso _
item.ModificationDate.Length > 0 Then
'UNIXタイムスタンプをDateTimeに変換
Dim dt As New DateTime(1970, 1, 1)
dt = dt.AddSeconds(Double.Parse(item.ModificationDate))
Label1.Text += " - " + dt.ToShortDateString()
End If
'MIMEタイプ
If Not (item.MimeType Is Nothing) AndAlso _
item.MimeType.Length > 0 Then
Label1.Text += " - " + item.MimeType
End If
Label1.Text += "</small>" + ControlChars.Lf
Next item
Label1.Text += "</ul>" + ControlChars.Lf
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Yahoo!ウェブ検索Webサービスのサンプル</title>
</head>
<body>
<form id="form1" runat="server">
<div>
キーワード :
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click"
Text="ウェブ検索" /><br />
キーワードを<asp:CheckBoxList ID="searchType" runat="server"
RepeatDirection="Horizontal"
RepeatLayout="Flow">
<asp:ListItem Selected="True" Value="all">すべて含む</asp:ListItem>
<asp:ListItem Value="any">少なくとも1つ含む</asp:ListItem>
<asp:ListItem Value="phrase">文章として含む</asp:ListItem>
</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>
<asp:ListItem Value="html">HTML (.htm, .html)</asp:ListItem>
<asp:ListItem Value="pdf">Adobe PDF (.pdf)</asp:ListItem>
<asp:ListItem Value="xls">Microsoft Excel (.xls)</asp:ListItem>
<asp:ListItem Value="ppd">Microsoft Power Point (.ppd)</asp:ListItem>
<asp:ListItem Value="msword">Microsoft Word (.doc)</asp:ListItem>
<asp:ListItem Value="rss">RSS/XML (.xml)</asp:ListItem>
<asp:ListItem Value="txt">テキストフォーマット (.txt)</asp:ListItem>
</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" EnableViewState="False">
</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サービス by Yahoo! JAPAN</a>
</span>
<!-- End Yahoo! JAPAN Web Services Attribution Snippet -->
</body>
</html>
}}
//これより下は編集しないでください
#pageinfo(,2006-09-09 (土) 02:59:13,DOBON!,2006-09-09 (土) 02:59:13,DOBON!)