ASP.NET 与 ASP、PHP、RAILS 和 JAVA 通信技巧(第一部分)






4.56/5 (10投票s)
2007年10月16日
4分钟阅读

92920

918
集成 ASP.NET 应用与现有 ASP、PHP、RAILS、JAVA 应用的技巧
目录
引言

在设计应用程序时,最困难的部分是将其与现有应用程序集成。如果幸运的话,目标应用程序也基于 ASP.NET 构建,但大多数情况下,这些应用程序是用不同的语言编写的,并且使用不同的框架。本文介绍了一种解决此问题的设计。
高层设计
高层设计包含一个集成层,该层使 ASP.NET 能够与 RAILS、PHP、ASP 进行通信。此集成层还与 RAILS、PHP、ASP 和 Java 共享来自 ASP.NET 的数据。通过修改,此设计还可以扩展到其他用其他语言创建的应用程序。

详细技术设计
集成层的详细设计包含两个主要部分:
ASPNETIntegrationServer
- 它通过 ASP.NET 页面实现,该页面获取来自 ASP.NET 应用程序的数据,并通过调用相应的监听器将其传递给目标应用程序,如图所示。
监听器
ASPListener
(此块位于 ASP WebServer Box 中)- 步骤 1:读取 ASP.NET 页面传递的数据
- 步骤 2:在 ASP 中处理请求和自定义业务逻辑
- 步骤 3:将结果返回给 ASP.NET 页面
PHPListener
(此块位于 PHP WebServer Box 中)- 步骤 1:读取 ASP.NET 页面传递的数据
- 步骤 2:在 PHP 中处理请求和自定义业务逻辑
- 步骤 3:将结果返回给 ASP.NET 页面
RailsListener
(此块位于 RAILS WebServer Box 中)- 步骤 1:读取 ASP.NET 页面传递的数据
- 步骤 2:在 Ruby 中处理请求和自定义业务逻辑
- 步骤 3:将结果返回给 ASP.NET 页面
JavaListener
(此块位于 Java Tomcat WebServer Box 中)
[请注意,Java 监听器未在微型 EAI 代码中实现]- 步骤 1:读取 ASP.NET 页面传递的数据
- 步骤 2:在 Java 中处理请求和自定义业务逻辑
- 步骤 3:将结果返回给 ASP.NET 页面
流程图

Using the Code
ASPNETIntegrationServer.aspx.cs
通过 HTTPGet
传递值并重定向到页面的调用监听器的逻辑
string strRequest;
strRequest = "Value to Pass";
//code to call asplistener
Response.Redirect
(http://aspserver/PHPListener.php?aspNetPassedValueByGet= + strRequest);
//code to call phplistener
Response.Redirect
(http://phpserver/ASPListener.asp?aspNetPassedValueByGet= + strRequest);
//code to call railslistener
Response.Redirect
("http://railsserver/RailsListener/integrate?aspPassedValue=
" + strRequest);
调用监听器并在代码中读取值的逻辑
public string TinyEAIPostRequest(string strURL,string strRequest)
{
HttpWebResponse objHttpWebResponse = null;
UTF8Encoding encoding;
string strResponse = "";
HttpWebRequest objHttpWebRequest;
objHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL);
objHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
objHttpWebRequest.PreAuthenticate = true;
objHttpWebRequest.Method = "POST";
//Prepare the request stream
if (strRequest != null && strRequest != string.Empty)
{
encoding = new UTF8Encoding();
Stream objStream = objHttpWebRequest.GetRequestStream();
Byte[] Buffer = encoding.GetBytes(strRequest);
// Post the request
objStream.Write(Buffer, 0, Buffer.Length);
objStream.Close();
}
objHttpWebResponse = (HttpWebResponse)objHttpWebRequest.GetResponse();
encoding = new UTF8Encoding();
StreamReader objStreamReader =
new StreamReader(objHttpWebResponse.GetResponseStream(),encoding);
strResponse = objStreamReader.ReadToEnd();
objHttpWebResponse.Close();
objHttpWebRequest= null;
return strResponse;
}
PHPListener.php
<?php
#Receive ASP.NET passed values
#Data from ASP.NET can be passed as POST or GET Requests
echo 'php received :- ' . $_GET['aspNetPassedValueByGet' ]
echo 'php received :- ' . $_POST['aspNetPassedValueByPost' ]
#Your php logic goes here
?>
ASPListener.asp
<%
'Receive ASP.NET passed values
'Data from ASP.NET can be passed as POST or GET Requests
Response.Write "asp received :- " +
Request.QueryString("aspNetPassedValueByGet")
Response.Write "asp received :- " + Request.Form('aspNetPassedValueByPOST')
'Your asp logic goes here
%>
RailsListenerController.rb
class RailsListenerController < ApplicationController
def integrate
@aspValue = @params["aspPassedValue"]
end
end
integrate.rhtml
<html>
<head></head>
<body>
ASP.NET Passed value <%=@aspValue
%>
</body>
</html>
使用此设计测试 PHP 加法示例
这是一个简单的加法示例,使用 PHP 监听器,其中 PHP 中编写的业务逻辑将 ASP.NET 传递的值相加,然后将结果返回给 ASP.NET 应用程序。请参阅以下执行此操作的内部步骤:
- ASP.NET 应用程序获取输入
- 调用
TinyEAIPostRequest
函数,传递 URL 和输入 - 调用
PHPListener
,传递要相加的值 PHPListener
接收请求- 此处是我们的自定义业务逻辑 - 为简单起见,我们只进行数字相加
PHPListener
返回结果- ASP.NET 将结果显示给用户
PHPListener.php:将此文件放在 PHP 服务器上
<?php
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];
#Here goes our custom business logic
#Just add the two numbers and return
echo $value1 + $value2 ;
?>
TinyEAI:在 ASP.NET 服务器上托管 TinyEAI
在 ASPNETIntegrationServer.aspx.cs 文件中更改 URL,使其指向上面的 PHPListener
,如下所示:
protected void btnTestCallandReturn_Click(object sender, EventArgs e)
{
//As we get only one return value so we assign
//all the response back to txtResults
//For complex types we can make the Listener return XML
//which can be loaded to XMLDOM and use it.
txtResult.Text = TinyEAIPostRequest
("https://:8081/phpListener.php", txtRequest.Text);
}
测试我们的示例
在浏览器中打开以下链接:http://yourASPNETServer/tinyEAI/ASPNETIntegrationServer.aspx
应该会显示以下屏幕:

测试我们的示例
在 ServerRequest
文本框中,输入 Value1=2&Value2=100
。然后单击 **Test PHP Request** 按钮。
在 ServerResponse
文本框中,您应该会看到相加后的值。
应该会显示以下屏幕:

改进设计的建议
请提供您改进此设计的建议,以及使其更具可扩展性和易于实现的技巧,并分享您在集成设计方面的想法。
我想请问,如果你喜欢这篇文章,请投票并留下评论,这能让我知道文章是否达到了合适的水平,以及是否包含了人们需要知道的内容。
结论
一种非常简单的方法,可将 ASP.NET 与 PHP、RAILS、ASP 和 Java 平台/语言集成。
历史
- v1.0 2007/08/11:首次发布