开发MSN Search

开发MSN Search API准备工作

在开发MSN Search API的应用程序之前需要完成:相关环境、确认XML Web服务和获得一个用于搜索的访问标记3个步骤。

第一步,需要一台可以连接互联网的计算机,并且可以支持发送和请求SOAP1.1协议、HTTP1.1协议以及支持XML的处理能力。

第二步,需要确保可以成功调用MSN Search的WSDL(Web Service Description Language)WSDL : http://soap.search.msn.com/webservices.asmx?wsdl

第三步,MSN Search API的Web服务处理需要提供一个应用程序ID,该ID可以通过访问http://search.msn.com/developer.后注册获得。
_______________________________________________________________
Default.aspx
________________________________________________________-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>利用webservice实现msn搜索</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <asp:TextBox ID="txtSearchString" Width="300px" runat="server"></asp:TextBox>
        <asp:Button ID="btnSearch" runat="server"   Text="开始搜索" OnClick="btnSearch_Click" /><br />
        <span id="txtShow"  style="width:600px" runat=server></span>
    </div>
    </form>
</body>
</html>
____________________________________-
Default.aspx.cs-
----------------------------------------------
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        MsnSearch(this.txtSearchString.Text);
    }
    private void MsnSearch(string keywords)
    {
        try
        {
            Msn.MSNSearchService s = new Msn.MSNSearchService();
            Msn.SearchRequest searchRequest = new Msn.SearchRequest();
            Msn.SourceRequest[] sr = new Msn.SourceRequest[1];
            sr[0] = new Msn.SourceRequest();
            sr[0].Source = Msn.SourceType.Web;
            sr[0].Offset = 1;//起始搜索
            sr[0].Count = 20;//搜索结果数目
            sr[0].ResultFields = Msn.ResultFieldMask.All;//结果作用域
            searchRequest.Query = keywords;//查找内容
            searchRequest.Requests = sr;//请求信息
            searchRequest.SafeSearch = Msn.SafeSearchOptions.Moderate;//安全查找过程
            searchRequest.AppID = ConfigurationManager.AppSettings["AppID"].ToString();//设置Id
            searchRequest.Flags = Msn.SearchFlags.MarkQueryWords;//设置标记
            searchRequest.CultureInfo = ConfigurationManager.AppSettings["SearchLanguage"].ToString();
            Msn.SearchResponse searchResponse = new Msn.SearchResponse();
            searchResponse = s.Search(searchRequest);//执行搜索获取结果
            foreach (Msn.SourceResponse sourceResponse in searchResponse.Responses)
            {
                string output = "";
                Msn.Result[] sourceResults = sourceResponse.Results;
                if (sourceResponse.Total > 0)
                {
                    output = "--------------" + sourceResponse.Source.ToString() + "--------------";
                    output += "</br> 找到结果数:";
                    output += sourceResponse.Total.ToString() + "</br>";
                    output += "Offset Requested: " + sr[0].Offset.ToString() + "</br>";
                    output += "Offset Returned: " + sourceResponse.Offset.ToString() + "</br>";
                    output += "----------------------------------------</br>";

                }
                else
                {
                    output = "没有找到结果";
                }
                foreach (Msn.Result sourceResult in sourceResults)
                {
                    if ((sourceResult.Title != null) && (sourceResult.Title != String.Empty))
                    {
                        output += "Title: <font color='#0000FF'>" + sourceResult.Title + "</font></br>";
                    }

                    if ((sourceResult.Description != null) && (sourceResult.Description != String.Empty))
                        output += "Description: " + sourceResult.Description + "</br>";
                    if ((sourceResult.DisplayUrl != null) && (sourceResult.DisplayUrl != String.Empty))
                        output += "DisplayUrl: " + sourceResult.DisplayUrl + "</br>";
                    if ((sourceResult.Url != null) && (sourceResult.Url != String.Empty))
                        output += "Url: " + sourceResult.Url + "</br>";
                    if ((sourceResult.SearchTags != null) && (sourceResult.SearchTags != String.Empty))
                        output += "SearchTags: " + sourceResult.SearchTags + "</br>";
                    if ((sourceResult.CacheUrl != null) && (sourceResult.CacheUrl != String.Empty))
                        output += "CacheUrl: " + sourceResult.CacheUrl + "</br>";
                }
                txtShow.InnerHtml = output;
            }
        }
        catch
        {
            Response.Write("<script lauguage='javascript'>alert('有错误发生');</script>");
        }
    }
}
--------------------------------------------------------
Web.config
______________________________________________________
<appSettings>
    <add key="Msn.webservices" value="http://soap.search.msn.com/webservices.asmx"/>
    <add key="AppID" value="0F8BFAA92A36D9A2BB5F4C9214295BA283AA75A8"/>
    <add key="SearchLanguage" value="en-US"/>
  </appSettings>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值