主题:[讨论]各位高人请赐教,关于C#调用MSN API函数问题!!!
以下是程序原文:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using WindowsApplication2.localhost;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void MSNSearch(string keywords)
{
try
{
richTextBox1.Clear();//清除文本框内容
MSNSearchService s = new MSNSearchService();//初始化服务
SearchRequest searchrequest = new SearchRequest();//初始化请求
int arrykey = 3;
SourceRequest[] sr = new SourceRequest[arrykey];;//建立请求内容数组
sr[0] = new SourceRequest();//初始化其中一个搜索项
sr[0].Source = SourceType.Web;//搜索资源类型
sr[0].Offset = 1;//起始索引
sr[0].Count = 20;//搜索结果数目
sr[0].ResultFields = ResultFieldMask.All;//结果作用域
searchrequest.Query = keywords;//查找内容
searchrequest.Requests = sr;//请求信息
searchrequest.SafeSearch = SafeSearchOptions.Moderate;//安全查找设置
1. searchrequest.AppID = System.Configuration.ConfigurationSettings.AppSettings["AppID"].ToString();//设置ID,通过App.config实现
searchrequest.Flags = SearchFlags.MarkQueryWords;//设置标记
2. searchrequest.CultureInfo = System.Configuration.ConfigurationSettings.AppSettings["SearchLanguage"].ToString();//设置当前位置
SearchResponse searchResponse;//搜索结果对象
searchResponse = s.Search(searchrequest);//执行搜索,获得结果
(结果读取处理)
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
string kk;
kk = textBox1.Text;
MSNSearch(kk);
}
}
}
(注:为了讨论期间我在出问题的语句上标示了数字1,2在原程序中是没有的)
以上是程序原文,问题如下:
1.能告诉我这个函数,如何读取他的搜索结果?(注:他是private void没有返回值的,我也不知道作者为什么这么写)
2.上面的程序编译通过,但是运行出错,错误提示:"未将对象引用设置到对象实例",这是哪里出错了?
3.经过排查,在标示1和2处系统都提示:"System.Configuration.ConfigurationSettings.AppSettings"已过时,应改为"System.Configuration.ConfigurationManager.AppSettings",换成ConfigurationManager这个类后,AppSetings这个属性要求参数是int的,我不会用,那位能给调试一下,谢谢
(注:在程序当前目录下确实有一个app.config,他的目的也是想从这个文件中提取ID值,只是没成功)
下面是app.config中的内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="WindowsApplication2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<WindowsApplication2.Properties.Settings>
<setting name="WindowsApplication2_localhost_MSNSearchService"
serializeAs="String">
<value>http://soap.search.msn.com:80/webservices.asmx</value>
</setting>
</WindowsApplication2.Properties.Settings>
</applicationSettings>
</configuration>
以上就是我的问题,我是新手,能给出程序最好,先谢谢大家了!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using WindowsApplication2.localhost;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void MSNSearch(string keywords)
{
try
{
richTextBox1.Clear();//清除文本框内容
MSNSearchService s = new MSNSearchService();//初始化服务
SearchRequest searchrequest = new SearchRequest();//初始化请求
int arrykey = 3;
SourceRequest[] sr = new SourceRequest[arrykey];;//建立请求内容数组
sr[0] = new SourceRequest();//初始化其中一个搜索项
sr[0].Source = SourceType.Web;//搜索资源类型
sr[0].Offset = 1;//起始索引
sr[0].Count = 20;//搜索结果数目
sr[0].ResultFields = ResultFieldMask.All;//结果作用域
searchrequest.Query = keywords;//查找内容
searchrequest.Requests = sr;//请求信息
searchrequest.SafeSearch = SafeSearchOptions.Moderate;//安全查找设置
1. searchrequest.AppID = System.Configuration.ConfigurationSettings.AppSettings["AppID"].ToString();//设置ID,通过App.config实现
searchrequest.Flags = SearchFlags.MarkQueryWords;//设置标记
2. searchrequest.CultureInfo = System.Configuration.ConfigurationSettings.AppSettings["SearchLanguage"].ToString();//设置当前位置
SearchResponse searchResponse;//搜索结果对象
searchResponse = s.Search(searchrequest);//执行搜索,获得结果
(结果读取处理)
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
string kk;
kk = textBox1.Text;
MSNSearch(kk);
}
}
}
(注:为了讨论期间我在出问题的语句上标示了数字1,2在原程序中是没有的)
以上是程序原文,问题如下:
1.能告诉我这个函数,如何读取他的搜索结果?(注:他是private void没有返回值的,我也不知道作者为什么这么写)
2.上面的程序编译通过,但是运行出错,错误提示:"未将对象引用设置到对象实例",这是哪里出错了?
3.经过排查,在标示1和2处系统都提示:"System.Configuration.ConfigurationSettings.AppSettings"已过时,应改为"System.Configuration.ConfigurationManager.AppSettings",换成ConfigurationManager这个类后,AppSetings这个属性要求参数是int的,我不会用,那位能给调试一下,谢谢
(注:在程序当前目录下确实有一个app.config,他的目的也是想从这个文件中提取ID值,只是没成功)
下面是app.config中的内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="WindowsApplication2.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<WindowsApplication2.Properties.Settings>
<setting name="WindowsApplication2_localhost_MSNSearchService"
serializeAs="String">
<value>http://soap.search.msn.com:80/webservices.asmx</value>
</setting>
</WindowsApplication2.Properties.Settings>
</applicationSettings>
</configuration>
以上就是我的问题,我是新手,能给出程序最好,先谢谢大家了!