加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

客户端访问WebService(3)

发布时间:2020-12-17 02:36:32 所属栏目:安全 来源:网络整理
导读:1,使用Htttp Get 访问方法 简单例子: WebService代码: ? using ?System; using ?System.Collections; using ?System.ComponentModel; using ?System.Data; using ?System.Web; using ?System.Web.Services; using ?System.Web.Services.Protocols; namespace
1,使用Htttp Get 访问方法

简单例子:

WebService代码: ?
  1. using?System;
  2. using?System.Collections;
  3. using?System.ComponentModel;
  4. using?System.Data;
  5. using?System.Web;
  6. using?System.Web.Services;
  7. using?System.Web.Services.Protocols;
  8. namespace?AJAXEnabledWebApplication4
  9. {
  10. ????///?<summary>
  11. ????///?WebService2?的摘要说明
  12. ????///?</summary>
  13. ????[WebService(Namespace?=?"http://tempuri.org/")]
  14. ????[WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]
  15. ????[ToolboxItem(false)]
  16. ????[System.Web.Script.Services.ScriptService]
  17. ????public?class?WebService2?:?System.Web.Services.WebService
  18. ????{
  19. ????????[WebMethod]
  20. ????????public?int?GetRandom()
  21. ????????{
  22. ????????????return?new?Random(DateTime.Now.Millisecond).Next();
  23. ????????}
  24. ????????[WebMethod]
  25. ????????//ScriptMethod属性标记
  26. ????????//设置UserHttpGet为true
  27. ????????//通过上面的设置,就可以设置请求方式为Get
  28. ????????[System.Web.Script.Services.ScriptMethod(UseHttpGet=true)]
  29. ????????public?int?GetRangeRandom(int?minValue,?int?maxValue)
  30. ????????{
  31. ????????????return?new?Random(DateTime.Now.Millisecond).Next(minValue,?maxValue);
  32. ????????}
  33. ????}
  34. }
页面代码: ?

  1. <%@?Page?Language="C#"?AutoEventWireup="true"?CodeBehind="WebForm1.aspx.cs"?Inherits="AJAXEnabledWebApplication4.WebForm1"?%>
  2. <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html?xmlns="http://www.w3.org/1999/xhtml"?>
  4. <head?runat="server">
  5. ????<title>无标题页</title>
  6. </head>
  7. <body>
  8. ????<form?id="form1"?runat="server">
  9. ????<asp:ScriptManager?runat="server"?ID="scriptManager1"?>
  10. ????<Services>
  11. ????<asp:ServiceReference?Path="WebService2.asmx"?InlineScript="true"?/>
  12. ????</Services>
  13. ????</asp:ScriptManager>
  14. ????<input?type="button"?value="GetRandom?(Post)"?onclick="getRandom()"?/>
  15. ????<input?type="button"?value="Get?Range?Random?(Get)"?onclick="getRangeRandom(50,100)"?/>
  16. ????<script?type="text/javascript"?language="javascript"?>
  17. ????function?getRandom(){
  18. ??????????AJAXEnabledWebApplication4.WebService2.GetRandom(onsucceed);
  19. ????}
  20. ????function?getRangeRandom(minValue,maxValue){
  21. ????????AJAXEnabledWebApplication4.WebService2.GetRangeRandom(minValue,maxValue,onsucceed);
  22. ????}
  23. ????function?onsucceed(result){
  24. ????????alert(result);
  25. ????}
  26. ????</script>
  27. ????</form>
  28. </body>
  29. </html>
在页面生成的代理中,可以看到一些区别,还可以通过httpWatch观察post和get 的一些小的区别.?

2,服务器方法重载的调用和客户端重载的调用

简单例子:
webservice代码: ?

  1. using?System;
  2. using?System.Collections;
  3. using?System.ComponentModel;
  4. using?System.Data;
  5. using?System.Web;
  6. using?System.Web.Services;
  7. using?System.Web.Services.Protocols;
  8. namespace?AJAXEnabledWebApplication4
  9. {
  10. ????///?<summary>
  11. ????///?WebService1?的摘要说明
  12. ????///?</summary>
  13. ????[WebService(Namespace?=?"http://tempuri.org/")]
  14. ????[WebServiceBinding(ConformsTo?=?WsiProfiles.BasicProfile1_1)]
  15. ????[ToolboxItem(false)]
  16. ????[System.Web.Script.Services.ScriptService]
  17. ????public?class?WebService1?:?System.Web.Services.WebService
  18. ????{
  19. ????????[WebMethod]
  20. ????????public?int?GetRandom()
  21. ????????{
  22. ????????????return?new?Random(DateTime.Now.Millisecond).Next();
  23. ????????}
  24. ????????[WebMethod(MessageName="GetRangeRandom")]
  25. ????????//可以通过?MessageName=""对方法进行重新命名
  26. ????????public?int?GetRandom(int?min,?int?max)
  27. ????????{
  28. ????????????return?new?Random(DateTime.Now.Millisecond).Next(min,?max);
  29. ????????}
  30. ????}
  31. }

aspx页面代码:
  1. <%@?Page?Language="C#"?AutoEventWireup="true"?CodeBehind="Default.aspx.cs"?Inherits="AJAXEnabledWebApplication4._Default"?%>
  2. <!DOCTYPE?html?PUBLIC?"-//W3C//DTD?XHTML?1.0?Transitional//EN"?"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html?xmlns="http://www.w3.org/1999/xhtml"?>
  4. <head?runat="server">
  5. ????<title>Untitled?Page</title>
  6. </head>
  7. <body>
  8. ????<form?id="form1"?runat="server">
  9. ????????<asp:ScriptManager?ID="ScriptManager1"?runat="server">
  10. ????????<Services>
  11. ????????<asp:ServiceReference?Path="WebService1.asmx"?InlineScript="true"?/>
  12. ????????</Services>
  13. ????????</asp:ScriptManager>
  14. ????????<input?type="button"?value="Get?Random"?onclick="GetRandom()"?/>
  15. ????????<input?type="button"?value="Get?Range?Random"?onclick="GetRandom(50,100)"?/>
  16. ????????<script?type="text/javascript"?language="javascript">
  17. ????????function?GetRandom(minValue,maxValue){
  18. ??????????//js?函数默认的?arguments可以判断传入参数的数量,从而模仿了客户端方法重载
  19. ???????????if(arguments.length!=2){
  20. ????????????AJAXEnabledWebApplication4.WebService1.GetRandom(onsucceed);
  21. ????????????}else{
  22. ????????????AJAXEnabledWebApplication4.WebService1.GetRangeRandom(minValue,onsucceed);
  23. ???????????
  24. ????????????}
  25. ????????}
  26. ????????
  27. ????????function?onsucceed(result){
  28. ????????????alert(result);
  29. ????????}
  30. ????????</script>
  31. ????</form>
  32. </body>
  33. </html>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读