using System.Xml;
using System.Text;
using System.Net;
using System.Net.Sockets;
...
private string RetrieveXML(string server, string page, System.Web.HttpRequest request)
{
/**********************************************
* Send the request
**********************************************/
//create socket
IPHostEntry ipHostInfo = Dns.Resolve(server);
IPAddress ipAddress = ipHostInfo.AddressList[0];
IPEndPoint ipe = new IPEndPoint(ipAddress, 80);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
//connect socket to server
socket.Connect(ipe);
//Create the request to send to the server
string strRequest = "GET /" + page + " HTTP/1.1\r\n" +
"Host: " + server + "\r\n" +
"Connection: Close\r\n" +
"Cookie: " + request.Headers["Cookie"] + "\r\n" +
"User-Agent: " + request.Headers["User-Agent"] + "\r\n\r\n";
//Convert send data to bytes
Byte[] bytesSend = Encoding.ASCII.GetBytes(strRequest);
//Send the data to the server
socket.Send(bytesSend, bytesSend.Length, 0);
/**********************************************
* Receive the return data
**********************************************/
//Declare variables for data receipt
byte[] bytes = new byte[256];
int nBytes = 0;
string receive = "";
string xml = "";
// The following will block until the page is transmitted.
do
{
nBytes = socket.Receive(bytes, bytes.Length, 0);
receive += Encoding.ASCII.GetString(bytes, 0, nBytes);
}
while (nBytes > 0);
//We have the page data, but it includes the headers
// Retrieve XML data from page response
xml = receive.Substring(receive.IndexOf("<?xml"), receive.Length - receive.IndexOf("<?xml"));
//Cleanup the socket
socket.Shutdown(SocketShutdown.Both);
socket.Close();
//Return the data
return xml;
}
public void TransferSession(System.Web.HttpRequest request, System.Web.SessionState.HttpSessionState session)
{
//Clear the session contents to have a clean session - Optional
session.RemoveAll();
//Define the URL and page to load the Session XML from
string XMLServer = request.ServerVariables["SERVER_NAME"];
string XMLPage = "aspclassicsession.asp";
//Define an XMLDocument to allow easy XML tree navigation
XmlDocument doc = new XmlDocument();
//Load the document from the reader
doc.LoadXml(RetrieveXML(XMLServer, XMLPage, request));
//Loop through the Session element's child nodes and set
//each Session object
foreach(XmlNode node in doc.FirstChild.NextSibling.ChildNodes)
{
session[node.Name.ToString()] = node.InnerText.ToString();
}
}
|