Yahoo! Search Web Services: Contextual Web Search サンプルコード(C#) †
以下のコードを実行するには、xsd.exeで生成されたクラスが必要です。これは../Web Searchコード(C#)に添付されているクラスと同じです。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
| | <%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
string requestUrl =
"http://api.search.yahoo.com/WebSearchService/V1/contextSearch";
string postData = "";
postData += "appid=XXXXX";
postData += "&query=" + Server.UrlEncode(TextBox1.Text);
postData += "&context=" + Server.UrlEncode(TextBox2.Text);
byte[] postDataBytes = System.Text.Encoding.ASCII.GetBytes(postData);
System.Net.HttpWebRequest request =
(System.Net.HttpWebRequest)System.Net.WebRequest.Create(
requestUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = postDataBytes.Length;
System.IO.Stream reqStream = request.GetRequestStream();
reqStream.Write(postDataBytes, 0, postDataBytes.Length);
reqStream.Close();
System.Net.HttpWebResponse response = null;
Yahoo.us.WebSearchResponse.ResultSet res;
try
{
response = (System.Net.HttpWebResponse)request.GetResponse();
System.IO.Stream strm = response.GetResponseStream();
System.Xml.Serialization.XmlSerializer serializer =
new System.Xml.Serialization.XmlSerializer(
typeof(Yahoo.us.WebSearchResponse.ResultSet));
res = (Yahoo.us.WebSearchResponse.ResultSet)
serializer.Deserialize(strm);
}
catch (Exception ex)
{
Label1.Text = "エラー:" + Server.HtmlEncode(ex.Message);
return;
}
finally
{
if (response != null)
response.Close();
}
Label1.Text = "";
if (res.totalResultsReturned == "0")
{
Label1.Text += "該当するページが見つかりませんでした。";
}
else
{
Label1.Text += "<ul>\n";
foreach (Yahoo.us.WebSearchResponse.ResultType item in res.Result)
{
Label1.Text += "<li><a href=\"" + item.ClickUrl + "\">" +
item.Title + "</a>";
}
Label1.Text += "</ul>\n";
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Yahoo! Search Web Services: Contextual Web Search サンプル</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:TextBox ID="TextBox2" runat="server"
Height="85px" TextMode="MultiLine"></asp:TextBox><br />
<br />
<asp:Label ID="Label1" runat="server" EnableViewState="False">
</asp:Label></div>
</form>
<!-- Begin Yahoo Web Services HTML Attribution Snippet -->
<a href="http://developer.yahoo.net/about/">
Web Services by Yahoo!
</a>
<!-- End Yahoo Web Services HTML Attribution Snippet -->
</body>
</html>
|
ページ情報 |
- 作成日 : 2006-09-12 (火) 01:54:58
- 作成者 : DOBON!
- 最終編集日 : 2006-09-12 (火) 02:32:47
- 最終編集者 : DOBON!
|