Web服务的创建和使用
2006-07-30 16:35
316 查看
web服务的引用
一个简单的东东
[WebMethod]
public double WebAdd(double db1,double db2)
{
return db1+db2;
}
引用其Web服务
[STAThread]
static void Main(string[] args)
{
localhost.MyService WebMath = new localhost.MyService();
double db1 = double.Parse(Console.ReadLine());
double db2 = double.Parse(Console.ReadLine());
double dbResult = WebMath.WebAdd(db1,db2);
Console.WriteLine("结果为:"+dbResult.ToString());
}
一个SMS的例子
引用http://www.wsms.cn/ws/sms.asmx?wsdl上的服务
private void tbSend_Click(object sender, System.EventArgs e)
{
//以下有局域网内有代理时用。
System.Net.WebProxy proxy = new System.Net.WebProxy("itgproxy.redmond.corp.microsoft.com:80");
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
System.Net.GlobalProxySelection.Select = proxy;
cn.wsms.www.sms MySms = new cn.wsms.www.sms();
MySms.SendSMS(tbContent.Text,tbDes.Text,tbUser.Text,tbPass.Text,"test");
}
不使用VS.NET,调用web服务
1、 wsdl /out:myWebService.cs http://localhost/MyService/MathService.asmx
2、 csc /out:RegWebService.dll /t:library /r:System.xml.dll /r:System.Web.Services.dll myWebService.cs
3、 有了组件库,我们就可以编写调用WebService的客户端程序了。见CallWebService.cs。
4、 csc /r:myWebService.dll CallWebService.cs
5、 运行CallWebService.exe。
建立数据连接
[WebMethod]
public DataSet SQLDB(string Query)
{
try
{
SqlConnection CS = new SqlConnection("server=(local);database=Northwind;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter (Query, CS);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "Results");
return myDataSet;
}
catch(Exception ex)
{
return DataError(ex);
}
}
[WebMethod]
public DataSet AccessDB(string Query)
{
try
{
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ this.Server.MapPath("AccessWebServices.mdb");
OleDbConnection myAccessConn = new OleDbConnection(strAccessConn);
OleDbCommand myAccessCommand = new OleDbCommand(Query,myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myAccessConn.Open();
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet,"Results");
myAccessConn.Close();
return myDataSet;
}
catch(Exception ex)
{
return DataError(ex);
}
}
public DataSet DataError(Exception ex)
{
DataSet errDS = new DataSet("Errors");
DataTable errTable = errDS.Tables.Add("Error");
errTable.Columns.Add("Message");
errTable.Rows.Add(new Object[] {ex.Message});
return errDS;
}
一个简单的东东
[WebMethod]
public double WebAdd(double db1,double db2)
{
return db1+db2;
}
引用其Web服务
[STAThread]
static void Main(string[] args)
{
localhost.MyService WebMath = new localhost.MyService();
double db1 = double.Parse(Console.ReadLine());
double db2 = double.Parse(Console.ReadLine());
double dbResult = WebMath.WebAdd(db1,db2);
Console.WriteLine("结果为:"+dbResult.ToString());
}
一个SMS的例子
引用http://www.wsms.cn/ws/sms.asmx?wsdl上的服务
private void tbSend_Click(object sender, System.EventArgs e)
{
//以下有局域网内有代理时用。
System.Net.WebProxy proxy = new System.Net.WebProxy("itgproxy.redmond.corp.microsoft.com:80");
proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
System.Net.GlobalProxySelection.Select = proxy;
cn.wsms.www.sms MySms = new cn.wsms.www.sms();
MySms.SendSMS(tbContent.Text,tbDes.Text,tbUser.Text,tbPass.Text,"test");
}
不使用VS.NET,调用web服务
1、 wsdl /out:myWebService.cs http://localhost/MyService/MathService.asmx
2、 csc /out:RegWebService.dll /t:library /r:System.xml.dll /r:System.Web.Services.dll myWebService.cs
3、 有了组件库,我们就可以编写调用WebService的客户端程序了。见CallWebService.cs。
4、 csc /r:myWebService.dll CallWebService.cs
5、 运行CallWebService.exe。
建立数据连接
[WebMethod]
public DataSet SQLDB(string Query)
{
try
{
SqlConnection CS = new SqlConnection("server=(local);database=Northwind;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter (Query, CS);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "Results");
return myDataSet;
}
catch(Exception ex)
{
return DataError(ex);
}
}
[WebMethod]
public DataSet AccessDB(string Query)
{
try
{
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
+ this.Server.MapPath("AccessWebServices.mdb");
OleDbConnection myAccessConn = new OleDbConnection(strAccessConn);
OleDbCommand myAccessCommand = new OleDbCommand(Query,myAccessConn);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myAccessConn.Open();
DataSet myDataSet = new DataSet();
myDataAdapter.Fill(myDataSet,"Results");
myAccessConn.Close();
return myDataSet;
}
catch(Exception ex)
{
return DataError(ex);
}
}
public DataSet DataError(Exception ex)
{
DataSet errDS = new DataSet("Errors");
DataTable errTable = errDS.Tables.Add("Error");
errTable.Columns.Add("Message");
errTable.Rows.Add(new Object[] {ex.Message});
return errDS;
}
相关文章推荐
- 使用BPEL创建Web服务组合
- 学习笔记:使用Web Service Software Factory开发简易留言本服务以及Mobile调用实现-1.创建Service
- 使用 gSOAP 通过 HTTP 和 HTTPS 调用由 WSAD 创建的 J2EE Web 服务
- 使用 ASP.NET 创建的 XML Web 服务的概述
- 使用 PowerDesigner 9.5 创建 Web 服务
- 使用 ASP.NET 创建的 XML Web 服务的概述
- 使用 ASP.NET 创建的 XML Web 服务的设计指南
- 使用 ASP.NET 所创建的 XML Web 服务的设计指南
- 在C++中创建并使用WEB服务
- 在MyEclipse下用XFire创建web服务及使用
- 使用BPEL创建Web服务组合
- 使用 ASP.NET 创建的 XML Web 服务的概述
- IBM的云平台Bluemix使用初体验——创建PHP Web 应用程序,添加并使用语言翻译服务
- 错误:"无法从命名空间绑定的导入"错误消息出现,当您试图通过使用 Web 服务描述语言工具创建 XML Web 服务代理文件时
- 使用Node.js快速创建web服务
- 如何创建和使用Web服务
- 创建,测试和使用Web服务的小结
- 使用 Apache Axis 和 Castor 创建 Web 服务
- 使用 gSOAP 通过 HTTP 和 HTTPS 调用由 WSAD 创建的 J2EE Web 服务
- Maven搭建webService (二) 创建服务端---使用web方式发布服务