webservice+xmlhttp+remoting

WEBSERVICE
简单的说,它是一个供应商提供的服务,该供应商提供的服务器端提供了一些方法,这些方法可以在internet上被其化客户在任何操作平台上直接调用,而且不需要考虑
这些方法是用哪种语言编写的。

XML
是一种结构化的文本,也就是说,可以将数据表示为具有层次结构的结构化文本形式。由于任何平台都支持文本,所以把数据转换为文本就可以达到跨平台传递数据的功能,
实现不同平台之间的数据共享。

xml常用类
xmlreader,xmlwriter,xmltextwriter,xmldocument,xmldatadocument,xmlnode,xmlnodelist,xmlelement,xmlattribute.

SOAP
简单对像访问协议,用来与web service交换数据,是一个开放的基于XML的internet标准。简单地说,它是传送HTTP请求和应答数据的一种标准。

.net remoting

using System;
namespace RemoteObject
{
    public class MyObject : MarshalByRefObject
    {
        public int Add(int a, int b)
        {
            return a + b;
        }
    }
}

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
namespace TestRemotingConsoleServer
{
    class Program
    {
        static void Main(string[] args)
        {
            // 新建一个TCP信道
            TcpChannel tc = new TcpChannel(9999);
            // 注册TCP信道
            ChannelServices.RegisterChannel(tc, false);
            // 注册知名对象
            RemotingConfiguration.RegisterWellKnownServiceType(typeof(RemoteObject.
            MyObject), "myObject", WellKnownObjectMode.SingleCall);
            // 让控制台不会自动关闭
            Console.ReadLine();
        }
    }
}

protected void Page_Load(object sender, EventArgs e)
{
    RemoteObject.MyObject mo = (RemoteObject.MyObject)Activator.GetObject
    (typeof(RemoteObject.MyObject), "tcp://localhost:9999/myObject");
    Response.Write(mo.Add(1, 2));
}


wsdl
web服务描述语言,用于描述web service提供的方法以及调用这些方法的各种方式。它是另一种与XML完全兼容的语法。


web service文件的扩展名为asmx,其后端代码扩展名为.asmx.cs。在后端代码中,可以定义public形式的方法,而且定义的方法前还要用webMethod特性表明。
如果没有webmethod特性描述,web service中定义的方法就不能对外提供服务。
[WebMethod]
public string UpString(string getString)
{

    string backString=getString.ToUpper();
    return backString;
}
这里WebMethod是对UpString()方示的声明。


对于每个web service来说都需要一个惟一的命名空间,以便于客户端应用程序能够将它和web上有其它服务区分开。而asp.net创建的工程默认的命名空间都是相同的,
使用WebServiceAttribute类中的Namespace特性可以更改系统默认的命名空间。如:
[WebService(Namespace=http://www.myweb.com/webservice/)]
public class myWebservice
{
................
}
表明客户端应用程序调用是的http://www.myweb.com/webservice/上的服务。
它的位置在public class myWebservice声明的上一行,表示该Web Service使用的名称空间。


//----------------------------------

新建一个项目,在“模板”中选择ASP.NET Web服务,命名为MyWebService
如果需要,输入一个能用开发Web Service的Web服务器地址,单击确定。


...........
namespace MyWebService
{

[WebServie(Namespace="http://localhost/webservices")]
public class Service:System.Web.Servies.WebService
{
..............
   [WebMethod]
   public string HelloWorld()
   {
       return "hello world";
   }

   [WebMethod]
   public string ConvertString(string str,bool toLower)
   {
      if(toLower)
      {
         str=str.ToLower();
      }
      else
      {
         str=str.ToUpper();
      }
      return str;
   }
}
}


web service的调用方法
选择“解决方案资源管理器”,右击引用,选择添加web引用,输入URL"http://localhost(或网址)/Mywebservie/service1.asmx"


localhost.service1 cc=new Testwebservie.localhost.service1();


this.texttab.tex=cc.ConvertString(this.textboxinput.text,false)

 

///
=====================================================
 var  xmlhttp  =  new  ActiveXObject("Microsoft.XMLHTTP"); 
       xmlhttp.open("GET","xml_yz.aspx?act=UserManager_insert&czydm="+UserForm.czyDm.value,false);  //相对路径
       xmlhttp.send(); 
       retInfo=xmlhttp.responseText;
       //alert(retInfo);
       if(retInfo=="pass")
       {
       }
       else
       {
            alert("操作员代码 "+retInfo+" 已存在!");
            return false;
       }
  
   }
   return true;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WebService是一种基于Web的远程调用技术,它使用XML格式进行数据交互。而JSON是一种轻量级的数据交换格式,比XML更加简洁和易于解析。因此,WebService和JSON的结合可以实现跨平台、跨语言的数据交互。 下面是一个使用Java中的net.sf.json库实现WebService+JSON的例子: ```java // 定义一个WebService接口 @WebService public interface HelloWorld { @WebMethod String sayHello(String name); } // 实现WebService接口 @WebService(endpointInterface = "com.example.HelloWorld") public class HelloWorldImpl implements HelloWorld { public String sayHello(String name) { return "Hello " + name + "!"; } } // 发布WebService服务 public class WebServicePublisher { public static void main(String[] args) { String url = "http://localhost:8080/HelloWorld"; Endpoint.publish(url, new HelloWorldImpl()); System.out.println("WebService服务已发布:" + url); } } // 使用JSON格式调用WebService服务 public class WebServiceClient { public static void main(String[] args) { String url = "http://localhost:8080/HelloWorld"; String name = "Alice"; JSONObject request = new JSONObject(); request.put("name", name); String response = HttpClientUtil.doPost(url, request.toString()); JSONObject result = JSONObject.fromObject(response); String message = result.getString("message"); System.out.println(message); // 输出:Hello Alice! } } ``` 上述代码中,我们定义了一个简单的HelloWorld接口和实现类,并使用Endpoint.publish()方法将其发布为WebService服务。然后,我们使用net.sf.json库将请求数据和响应数据转换为JSON格式,并使用HttpClientUtil.doPost()方法发送POST请求调用WebService服务。最后,我们将响应数据解析为JSON格式,并获取其中的message字段作为调用结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值