软件新技术WEB实验报告.docx
《软件新技术WEB实验报告.docx》由会员分享,可在线阅读,更多相关《软件新技术WEB实验报告.docx(22页珍藏版)》请在冰豆网上搜索。
软件新技术WEB实验报告
实验报告
实验课程名称:
实验二WEB服务技术
学生姓名:
管成功
学院(系):
怀德学院班级学号:
10416519
指导教师:
李宁成绩:
实验时间:
2013年4月17日
实验一
实验目的:
1、掌握Web服务的创建
2、掌握Web服务的发布
3、掌握Web服务的调用
实验内容:
(1)在VS2008上创建下面2个Web服务
创建一个实现两个数相加、相减、相乘、相除的Web服务;
利用实验一第三部分作业中创建的数据库表,创建可实现对病人住院信息的查询、录入、删除、修改的Web服务。
(2)在IIS上发布上面建立的Web服务
(3)编写一应用程序,实现上述两个Web服务的调用
实验具体实现
创建一个实现两个数相加、相减、相乘、相除的Web服务;
WEB服务中的代码如下:
[WebMethod(Description="返回两个整数的差")]
publicintSubtract(intx,inty)
{
returnx-y;
}
[WebMethod(Description="返回两个整数的和")]
publicintSubtrac(intx,inty)
{
returnx+y;
}
[WebMethod(Description="返回两个整数的积")]
publicintSubtra(intx,inty)
{
returnx*y;
}
[WebMethod(Description="返回两个整数的商")]
publicintSubtr(intx,inty)
{
returnx/y;
}
在IIS上发布此服务截图:
步骤如下:
以上的WEB服务存放在test_21文件夹中,将该文件夹存放到C:
\inetpub\wwwroot中,
启动IIS管理器,如图:
右击DefaultWebSite添加虚拟目录,如图:
右击test_21,找到管理应用程序,再点击浏览。
跳到如下界面:
点击Service.asmx,继续跳转到如下界面:
例如点击Subtrac
如能成功跳转结果值,则说明发布成功。
下面接着创建可实现对病人住院信息的查询、录入、删除、修改的Web服务。
WEBSERVICE代码如下:
[WebMethod(Description="查询病人信息,输入病人姓名")]
publicDataTableSubtract(stringxingming)
{
xingming="%"+xingming+"%";
stringsql="server=.;database=Patientimform;user=sa;pwd=;";
SqlConnectionstrsql=newSqlConnection(sql);
strsql.Open();
stringserch="select*fromdbo.patientwhere姓名like'"+xingming+"'";
DataTabletb=newDataTable();
tb.TableName="guanchenggong";
SqlDataAdapteradp=newSqlDataAdapter(serch,strsql);
adp.Fill(tb);
strsql.Close();
returntb;
}
[WebMethod(Description="添加病人信息")]
publicstringAD(stringid,stringsname,intage,stringsex,stringdoctor,stringtypea,stringtypeb)
{
stringi;
SqlConnectionconn=newSqlConnection("server=.;database=Patientimform;user=sa;pwd=;");
stringsql="insertintodbo.patientvalues('"+id+"','"+sname+"','"+age+"','"+sex+"','"+doctor+"','"+typea+"','"+typeb+"')";
conn.Open();
SqlCommandcmd=newSqlCommand(sql,conn);
i=cmd.ExecuteNonQuery().ToString();
conn.Close();
returni;
}
[WebMethod(Description="删除病人信息")]
publicstringde(stringindexid)
{
stringts;
SqlConnectionconn=newSqlConnection("server=.;database=Patientimform;user=sa;pwd=;");
stringsql="deletefromdbo.patientwhere病人编号='"+indexid+"'";
SqlCommandcmd=newSqlCommand(sql,conn);
conn.Open();
ts=cmd.ExecuteNonQuery().ToString();
conn.Close();
returnts;
}
[WebMethod(Description="更新病人信息")]
publicstringup(stringid,stringsname,intage,stringsex,stringdoctor,stringtypea,stringtypeb)
{
stringi;
SqlConnectionconn=newSqlConnection("server=.;database=Patientimform;user=sa;pwd=;");
conn.Open();
//stringsql="updatedbo.patientset学号='"+id+"',姓名='"+sname+"',年龄='"+age+"',性别='"+sex+"',医生='"+doctor+"',病症='"+typea+"',类别='"+typeb+"'"+"where病人编号="+id;
stringsql="updatedbo.patientset姓名='"+sname+"',年龄='"+age+"',性别='"+sex+"',医生='"+doctor+"',病症='"+typea+"',类别='"+typeb+"'where病人编号='"+id+"'";
SqlCommandcmd=newSqlCommand(sql,conn);
i=cmd.ExecuteNonQuery().ToString();
conn.Close();
returni;
}
在IIS上发布此服务截图:
步骤如下:
以上的WEB服务存放在test_22文件夹中,将该文件夹存放到C:
\inetpub\wwwroot中,
例如:
我们要根据病人姓名来查询病人信息。
点击Subtract ,跳转到如下界面:
点击调用,显示要查询结果的XML文档:
(3)编写一应用程序,实现上述两个Web服务的调用
应用程序界面:
代码如下:
usingSystem;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
usingSystem.Web.Services;
usingSystem.Web.Services.Protocols;
usingSystem.Data.SqlClient;
publicpartialclass_Default:
System.Web.UI.Page
{
localhost.Servicecd=newlocalhost.Service();
protectedvoidPage_Load(objectsender,EventArgse)
{
//有个值会改变,改变之后要刷新当前页面的textbox里
if(!
Page.IsPostBack)
TextBox4.Text=DateTime.Now.Date.ToString("yyyyMMdd_");
SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=Patientimform;IntegratedSecurity=True");
conn.Open();
stringstr="select医生编号,姓名fromdbo.doctor";
SqlCommandcom=newSqlCommand(str,conn);
//SqlDataReaderreader=com.ExecuteReader();
//inti=0;
//while(reader.Read())
//{
//comboBox1.Items.Add(reader.);
//i++;
//}
SqlDataAdapteradp=newSqlDataAdapter(str,conn);
DataTabledb=newDataTable();
adp.Fill(db);
DropDownList1.Items.Add("未选择");
inti=0;
while(i{
DropDownList1.Items.Add(db.Rows[i][0].ToString().Trim()+""+db.Rows[i][1].ToString().Trim());
i++;
}
//DropDownList1.SelectedIndex=1;
str="select类别编号,类别名称fromdbo.category";
adp=newSqlDataAdapter(str,conn);
DataTableda1=newDataTable();
adp.Fill(da1);
DropDownList2.Items.Add("未选择");
i=0;
while(i{
DropDownList2.Items.Add(da1.Rows[i][0].ToString().Trim()+""+da1.Rows[i][1].ToString().Trim());
i++;
}
//DropDownList2.SelectedIndex=1;
conn.Close();
}
protectedvoidButton1_Click(objectsender,EventArgse)
{
inti1=Int32.Parse(TextBox1.Text.ToString());
inti2=Int32.Parse(TextBox2.Text.ToString());
intresult=cd.Subtr(i1,i2);
TextBox3.Text=result.ToString();
}
protectedvoidButton2_Click(objectsender,EventArgse)
{
inti1=Int32.Parse(TextBox1.Text.ToString());
inti2=Int32.Parse(TextBox2.Text.ToString());
intresult=cd.Subtra(i1,i2);
TextBox3.Text=result.ToString();
}
protectedvoidButton3_Click(objectsender,EventArgse)
{
inti1=Int32.Parse(TextBox1.Text.ToString());
inti2=Int32.Parse(TextBox2.Text.ToString());
intresult=cd.Subtrac(i1,i2);
TextBox3.Text=result.ToString();
}
protectedvoidButton4_Click(objectsender,EventArgse)
{
inti1=Int32.Parse(TextBox1.Text.ToString());
inti2=Int32.Parse(TextBox2.Text.ToString());
intresult=cd.Subtract(i1,i2);
TextBox3.Text=result.ToString();
}
protectedvoidButton5_Click(objectsender,EventArgse)
{
localhost1.Serviceds=newlocalhost1.Service();
stringname=TextBox6.Text.ToString();
DataTabletb=ds.Subtract(name);
guanchenggong.DataSource=tb;
guanchenggong.DataBind();
}
protectedvoidguanchenggong_SelectedIndexChanged(objectsender,EventArgse)
{
GridViewRowgridview=guanchenggong.Rows[guanchenggong.SelectedIndex];
TextBox4.Text=gridview.Cells[1].Text;
TextBox6.Text=gridview.Cells[2].Text;
TextBox5.Text=gridview.Cells[3].Text;
stringxingb=gridview.Cells[4].ToString().Trim();
if(xingb=="男")
{
RadioButton1.Checked=true;
}
else
RadioButton2.Checked=true;
DropDownList1.SelectedIndex=int.Parse(gridview.Cells[5].Text);
DropDownList2.SelectedIndex=int.Parse(gridview.Cells[7].Text);
TextBox7.Text=gridview.Cells[6].Text;
}
protectedvoidButton7_Click(objectsender,EventArgse)
{
SqlConnectionconn=newSqlConnection("DataSource=.;InitialCatalog=Patientimform;IntegratedSecurity=True");
conn.Open();
stringsex;
localhost1.Serviceda=newlocalhost1.Service();
stringid=TextBox4.Text.ToString();
stringsname=TextBox6.Text.ToString();
intage=int.Parse(TextBox5.Text);
if(RadioButton1.Checked)
{
sex=RadioButton1.Text.ToString();
}
else
{
sex=RadioButton2.Text.ToString();
}
stringdoctor=DropDownList1.SelectedIndex.ToString();
stringdct=numN(doctor);
stringtypea=DropDownList2.SelectedIndex.ToString();
stringty=numN(typea.ToString());
stringtype=TextBox7.Text.ToString();
Response.Write("");
}
publicstringnumN(stringstr)
{
stringnumber=null;
foreach(chariteminstr)
{
if(item>=48&&item<=58)
{
number+=item;
}
else
{
break;
}
}
returnnumber;
}
publicstringnowTime()
{
stringtime=DateTime.Now.Year.ToString();
if(DateTime.Now.Month<10)
{
time=time+"0"+DateTime.Now.Month.ToString().Trim();
}
else
{
time=time+DateTime.Now.Month.ToString().Trim();
}
time=time+DateTime.Now.Day.ToString().Trim();
returntime;
}
protectedvoidguanchenggong_RowDeleting(objectsender,GridViewDeleteEventArgse)
{
localhost1.Servicecd1=newlocalhost1.Service();
stringname1=TextBox6.Text.ToString();
DataTabletb1=cd1.Subtract(name1);
stringstr=guanchenggong.SelectedIndex.ToString();
stringstr11=tb1.Rows[Int32.Parse(str)][0].ToString();
Response.Write("");
}
protectedvoidButton6_Click(objectsender,EventArgse)
{
localhost1.Serviceup=newlocalhost1.Service();
stringid=TextBox4.Text.ToString();
stringsname=TextBox6.Text.ToString();
intage=int.Parse(TextBox5.Text);
stringsex;
if(RadioButton1.Checked)
{
sex=RadioButton1.Text.ToString();
}
else
{
sex=RadioButton2.Text.ToString();
}
stringdoctor=DropDownList1.SelectedIndex.ToString();
stringdct=numN(doctor);
stringtypea=DropDownList2.SelectedIndex.ToString();
stringty=numN(typea.ToString());
stringtype=TextBox7.Text.ToString();
Response.Write("");
(2)添加WEB服务引用:
运行:
点击查询
2、试验二:
编写一应用程序,实现对天气预报的Web服务的调用,要求界面能够按照地名查询当时的天气情况。
注:
天气预报Web服务接口为下网址:
应用程序界面:
添加引用:
代码如下:
usingSystem;
usingSystem.Configuration;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.HtmlControls;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Xml.Linq;
using.webxml.www;
publicpart