科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件把.Net整合进其他平台

把.Net整合进其他平台

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

整合分布式应用程序经常是一件非常困难并且错综复杂的任务,即使是最富有经验的开发者也可能会觉得头疼

作者:合肥小雨 来源:yesky 2007年11月5日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
整合PHP Web服务

  既然一个自定义代理类已经准备好被使用,那么把它整合进一个ASP.Net页面就变得很简单了。getHeaders ()和getMessage ()方法可以调用Web服务,存取返回的XmlDocument对象(见代码段6和7) 在XmlDocument内的子结点中的枚举可以显示这些数据。 虽然许多Web服务可以很容易地使用VS.Net或者WSDL.exe创建的代理类自动地生成,但是仍然会有你需要创建自定义SOAP包装来把.Net和其他的平台整合起来的情况。 本文中介绍的内容以及代码就提供了完成这个整合工作的一种方法。

  代码段1:

POST http://www.codecraze.com/soap/nntp.php HTTP/1.1
User-Agent: Mozilla/4.0 (compatible;
MSIE 6.0; MS Web Services Client Protocol 1.0.2914.16)
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://www.codecraze.com/soap/nntp.php"
Content-Length: 683
Expect: 100-continue
Connection: Keep-Alive
Host: www.codecraze.com

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap=
"http://schemas.xmlsoap.org/soap/envelope/"
xmlns:soapenc="http://schemas.xmlsoap.org/soap/
encoding/"
xmlns:tns="http://tempuri.org/"
xmlns:types="http://tempuri.org/encodedTypes"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body
soap:encodingStyle=
"http://schemas.xmlsoap.org/soap/encoding/">
<q1:getheaders xmlns:q1=
"http://www.codecraze.com/soap/nntp.xsd">
<newsgroup xsi:type="xsd:string">
microsoft.public.dotnet.xml
</newsgroup>
<numitems xsi:type="xsd:string">15</numitems>
</q1:getheaders>
</soap:Body>
</soap:Envelope>

  代码段2:

HTTP/1.1 200 OK
Date: Apr, 16 Dec 2002 15:57:47 GMT
Server: Apache/1.3.20 (Unix) ApacheJServ/1.1.2 PHP/
4.0.4pl1 FrontPage/5.0.2.2510 Rewrit/1.1a
X-Powered-By: PHP/4.0.4pl1
Content-Length: 419
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Content-Type: text/xml; charset="utf-8"

<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/
envelope/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/
soap/encoding/">
<SOAP-ENV:Body>
<SOAP-ENV:Fault>
<faultcode>SOAP-ENV:Client.SOAPMessageFormat</
faultcode>
<faultstring>
message does not have a correct SOAP root XML tag
</faultstring>
</SOAP-ENV:Fault>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

  代码段3 :

private string CreateSoapEnvelope(
string method,string[] param1,
string[] param2) {
StringBuilder sb = new StringBuilder();
sb.Append("<SOAP-ENV:Envelope");
sb.Append(" xmlns:SOAP-" +
"ENV=\"http://schemas.xmlsoap.org/soap/
envelope/\"");
sb.Append(" xmlns:xsi=
\"http://www.w3.org/1999/XMLSchema-" +
instance\"");
sb.Append(" xmlns:xsd=
\"http://www.w3.org/1999/XMLSchema\">");
sb.Append("<SOAP-ENV:Body>");
sb.Append("<m:" + method + " xmlns:m=\"nntp.xsd\">");
sb.Append("<" + param1[0] + ">" + param1[1] +
"</" + param1[0] + ">");
sb.Append("<" + param2[0] + ">" + param2[1] +
"</" + param2[0] + ">");
sb.Append("</m:" + method + ">");
sb.Append("</SOAP-ENV:Body>");
sb.Append("</SOAP-ENV:Envelope>");
return sb.ToString();
}

  代码段4:

private XmlDocument SendSoapEnvelope(string soapEnv) {
HttpWebRequest request =
(HttpWebRequest)WebRequest.Create(_uri);
XmlTextReader xmlReader = null;
XmlDocument xmlDoc = null;
request.Method = "POST";
request.ContentType = "text/xml";
request.Headers.Add("SOAPAction",_soapAction);
StreamWriter writer = new StreamWriter(
request.GetRequestStream());
writer.Write(soapEnv);
writer.Close();
HttpWebResponse response =
(HttpWebResponse)request.GetResponse();

if (response != null) {
xmlReader = new XmlTextReader(
response.GetResponseStream());
xmlDoc = new XmlDocument();
xmlDoc.Load(xmlReader);
return xmlDoc;
} else {
return xmlDoc;
}
}

  代码段5:

public XmlDocument getHeaders(string newsgroup,
string numitems) {
string soapEnv = CreateSoapEnvelope("getheaders",
new string[2]{"newsgroup",newsgroup},
new string[2]{"numitems",numitems});
return FilterSoapEnvelope(SendSoapEnvelope(soapEnv));
}

public XmlDocument getMessage(string newsgroup,
string article) {
string soapEnv = CreateSoapEnvelope("getmessage",
new string[2]{"newsgroup",newsgroup},
new string[2]{"article",article});
return FilterSoapEnvelope(SendSoapEnvelope(soapEnv));
}

  代码段6:

private void Page_Load(object sender, System.EventArgs e)
{
StringBuilder sb = new StringBuilder();
try {
MSNewsService.MSNewsServiceProxy news =
new MSNewsService.MSNewsServiceProxy();
XmlDocument newsHeaders =
news.getHeaders("microsoft.public.dotnet.xml",
"15");
sb.Append("<ul>");
foreach (XmlNode node in
newsHeaders.DocumentElement.ChildNodes) {
sb.Append("<li><a href=\"JavaScript:readArticle(
'" + node.FirstChild.InnerText + "','" +
node.ChildNodes.Item(1).InnerText +
"')\">" + node.ChildNodes.Item(1).InnerText +
"</a></li>");
}
sb.Append("<ul>");
lblMessages.Text = sb.ToString();
}
catch (Exception exp) {
lblMessages.Text =
"The Web Service appears to be" + " unavailable";
}
}

  代码MSNewsServiceProxy.cs:

using System.Net;
using System.Xml;
using System.IO;
using System.Text;
namespace MSNewsService {

public class MSNewsServiceProxy {
string _uri;
string _soapAction;

public MSNewsServiceProxy() {
_uri = "http://www.codecraze.com/soap/nntp.php";
_soapAction = "http://www.codecraze.com/soap/nntp.php";
}

private XmlDocument SendSoapEnvelope(string soapEnv) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_uri);
XmlTextReader xmlReader = null;
XmlDocument xmlDoc = null;
request.Method = "POST";
request.ContentType = "text/xml";
request.Headers.Add("SOAPAction",_soapAction);
StreamWriter writer = new StreamWriter(request.GetRequestStream());
writer.Write(soapEnv);
writer.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

if (response != null) {
xmlReader = new XmlTextReader(response.GetResponseStream());
xmlDoc = new XmlDocument();
xmlDoc.Load(xmlReader);
return xmlDoc;
} else {
return xmlDoc;
}
}
private string CreateSoapEnvelope(string method,string[] param1, string[] param2) {
StringBuilder sb = new StringBuilder();
sb.Append("<SOAP-ENV:Envelope");
sb.Append(" xmlns:SOAP-" +
"ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"");
sb.Append(" xmlns:xsi=\"http://www.w3.org/1999/XMLSchema-" +
"instance\"");
sb.Append(" xmlns:xsd=\"http://www.w3.org/1999/XMLSchema\">");
sb.Append("<SOAP-ENV:Body>");
sb.Append("<m:" + method + " xmlns:m=\"nntp.xsd\">");
sb.Append("<" + param1[0] + ">" + param1[1] + "</" + param1[0] + ">");
sb.Append("<" + param2[0] + ">" + param2[1] + "</" + param2[0] + ">");
sb.Append("</m:" + method + ">");
sb.Append("</SOAP-ENV:Body>");
sb.Append("</SOAP-ENV:Envelope>");
return sb.ToString();
}

public XmlDocument getHeaders(string newsgroup,string numitems) {
string soapEnv = CreateSoapEnvelope("getheaders",
new string[2]{"newsgroup",newsgroup},
new string[2]{"numitems",numitems});
return FilterSoapEnvelope(SendSoapEnvelope(soapEnv));
}
public XmlDocument getMessage(string newsgroup,string article) {
string soapEnv = CreateSoapEnvelope("getmessage",
new string[2]{"newsgroup",newsgroup},
new string[2]{"article",article});
return FilterSoapEnvelope(SendSoapEnvelope(soapEnv));
}

private XmlDocument FilterSoapEnvelope(XmlDocument doc) {
XmlDocument filterDoc = new XmlDocument();
XmlNode result = doc.SelectSingleNode("//results");
XmlNode resultImport = filterDoc.ImportNode(result,true);
filterDoc.AppendChild(resultImport);
return filterDoc;
}

}
}

  代码msNewsClient.aspx.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Text;

namespace MSNewsService
{

public class MSNewsServiceClient : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblMessages;

public MSNewsServiceClient()
{
Page.Init += new System.EventHandler(Page_Init);
}

private void Page_Load(object sender, System.EventArgs e) {
StringBuilder sb = new StringBuilder();
try {
MSNewsService.MSNewsServiceProxy news = new MSNewsService.MSNewsServiceProxy();
XmlDocument newsHeaders = news.getHeaders("microsoft.public.dotnet.xml","15");
sb.Append("<ul>");
foreach (XmlNode node in newsHeaders.DocumentElement.ChildNodes) {
sb.Append("<li><a href=\"JavaScript:readArticle('" + node.FirstChild.InnerText +
"','" + node.ChildNodes.Item(1).InnerText +
"')\">" + node.ChildNodes.Item(1).InnerText + "</a></li>");
}
sb.Append("<ul>");
lblMessages.Text = sb.ToString();
}
catch (Exception exp) {
lblMessages.Text = "The Web Service appears to be unavailable";
}
}

private void Page_Init(object sender, EventArgs e)
{

InitializeComponent();
}


private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}

}
}

  代码msNewsMessage.aspx

<%@ Page language="c#" src="msNewsMessage.aspx.cs" Codebehind="msNewsMessage.aspx.cs" AutoEventWireup="false" Inherits="MSNewsService.msNewsMessage" %>
<HTML>
<body bgcolor="#ffffff">
<h2>
<asp:Label ID="lblMessageTitle" Runat="server" />
</h2>
<p>
<asp:Label ID="lblMessageText" Runat="server" />
</p>
</body>
</HTML>
代码msNewsMessage.aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

namespace MSNewsService
{

public class msNewsMessage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblMessageTitle;
protected System.Web.UI.WebControls.Label lblMessageText;

public msNewsMessage()
{
Page.Init += new System.EventHandler(Page_Init);
}

private void Page_Load(object sender, System.EventArgs e) {
try {
MSNewsService.MSNewsServiceProxy news = new MSNewsService.MSNewsServiceProxy();
XmlDocument newsHeaders = news.getMessage("microsoft.public.dotnet.xml",
Request["articleID"]);
lblMessageTitle.Text = Request["title"];
lblMessageText.Text = newsHeaders.FirstChild.InnerText;
}
catch (Exception exp) {
lblMessageText.Text = "The Web Service appears to be unavailable";
}
}

private void Page_Init(object sender, EventArgs e)
{
InitializeComponent();
}


private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}

}
}

  代码msNewsClient.apsx

<%@ Page language="c#" src="msNewsClient.aspx.cs" Codebehind="msNewsClient.aspx.cs" AutoEventWireup="false" Inherits="MSNewsService.MSNewsServiceClient" %>
<HTML>
<head>
<script language="javascript">
function readArticle(id,title) {
openWindow("msNewsMessage.aspx?articleID=" + id +
"&title=" + title,500,400);
}
function openWindow(page,w,h) {
window.open(page,"","status=no,width=" + w +
",height=" + h + ",scrollbars=yes");
}
</script>
</head>
<body bgcolor="#ffffff">
<h2>Recent posts from: microsoft.public.dotnet.xml</h2>
<p />
<asp:Label ID="lblMessages" Runat="server" />
</body>
</HTML><0}

查看本文来源

    • 评论
    • 分享微博
    • 分享邮件
    邮件订阅

    如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章