您的位置:首页 > 编程语言 > ASP

ASP调用c#编写的websevice方法

2014-07-14 22:32 363 查看
最近因为需要,维护一个比较古老的ASP网站,并且需要将OpenLDAP集成到ASP网站中。在网上查找了比较久,没有找到有效的直接asp操作LDAP的方式,也可能是我查找的方式不对,反正没有找到方法,如果各位有这方面的经验,欢迎讨论。

后来我采用的是,使用c#编写了一个WebService,因为C#有System.DirectoryServices.Protocols命名空间下提供了很多LDAP操作的接口跟类,实现LDAP查询操作十分方便。完成Web Services的编写之后,然后使用asp访问Web Services即可。

1.首先编写webservice,涉及到项目,我这里仅给出简单的示例代码,webservice文件名为MyFirstWebService.asmx(这里也不涉及到详细的WebService编写方法,大家可直接Google学习之),诸位可根据自己实际需要进行更改。

[WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class MyFirstWebService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public string Test(string tt)
        {
            return "your input is: " + tt + ".";
        }
    }

2.完成webservice代码编写之后,将它发布到本地IIS中,这里我发布到localhost/yui目录下,直接访问http://localhost/yui/MyFirstWebService.asmx,如果可以看到我们编写的webservice中的HelloWorld以及Test方法,证明发布成功。





3.编写asp代码,并保存为test.asp,代码中没有什么特别高深的东东,因此我也就懒得写注释了。
<%
Const cXMLHTTP_TYPENAME = "MSXML2.ServerXMLHTTP" 
Const cWEB_SERVICE_BASE_URL = "http://localhost/yui/MyFirstWebService.asmx"
Const cWEB_SERVICE_HELLOWORD = "HelloWorld" 
Const cWEB_SERVICE_TEST = "Test" 

Dim sData 

    sData = "tt=ss"

    testresult = GetHttpResponse(cWEB_SERVICE_BASE_URL & "/" & cWEB_SERVICE_TEST, sData)
    response.write testresult
    response.write "<br />"
    response.write "result length is : "&Len(testresult)

Function GetHttpResponse(ByVal url, ByVal dataToSend) 

	Dim oXmlHttp: Set oXmlHttp = Server.CreateObject(cXMLHTTP_TYPENAME) 
	Dim bSendData 

		oXmlHttp.Open "POST", url, False 

		bSendData = False 
		If IsObject(dataToSend) Then 
			If Not dataToSend Is Nothing Then 
				bSendData = True 
			End If 
		ElseIf Len(Trim(dataToSend)) > 0 Then 
			bSendData = True 
		End If 

		If bSendData Then 
			If bSendData And IsObject(dataToSend) = False Then 
				'set the content type 
				oXmlHttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
			End If 

			oXmlHttp.Send dataToSend 
		Else 
			oXmlHttp.Send 
		End If 

		GetHttpResponse = oXmlHttp.ResponseText 

	Set oXmlHttp = Nothing 

End Function
%>

4.将步骤3中的test.asp复制到IIS中的默认网站下,直接浏览localhost/test.asp,即可在页面中看到如下输出 your input is: ss. 输出该语句,表明asp已经成功调用到了c#编写的WebService。





5.至此,我们已经完成了使用经典的ASP调用c#编写的web服务的功能。



本人对WebService以及ASP认识还比较浅薄,欢迎大家一起讨论,一起进步。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: