扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:合肥小雨 来源:yesky 2007年11月5日
关键字:
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领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者