.NETGURU
Util library ...
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngcodegiveawayswap' list.


Robert Chartier
Comments/feedback welcome

Im going to break down this utility library, into separate posts..one for
each section (method + overloads) that I have just so we can have separate
threads of conversation for each item.

Robert Chartier
Author and Developer
604-975-5590
Click here to reveal e-mail address
http://www.aspfree.com/devlinks
http://www.aspfree.com/authors/robert
http://www.aspalliance.com/nothingmn
http://www.15seconds.com/issue/wa.htm#chartier

Reply to this message...
 
    
Robert Chartier
#region GetHttpStream
/// <summary>
/// GetHttpStream: Converts a URL address to a System.IO.Stream
/// </summary>
/// <param name="sURL"></param>
/// <param name="proxyaddress"></param>
/// <param name="proxyport"></param>
/// <returns></returns>
public System.IO.Stream GetHttpStream(string sURL, string
proxyaddress, int proxyport) {

WebRequest wrGETURL;
wrGETURL = WebRequest.Create(sURL);

WebProxy myProxy = new WebProxy(proxyaddress,proxyport);
myProxy.BypassProxyOnLocal = true;

wrGETURL.Proxy = WebProxy.GetDefaultProxy();

Stream objStream;
objStream = wrGETURL.GetResponse().GetResponseStream();
return objStream;

}
/// <summary>
/// hit any url with GET/POST, with the given parameters (FOR POST),
with the username/password
/// </summary>
/// <param name="sURL"></param>
/// <param name="httpmethod"></param>
/// <param name="postparams"></param>
/// <param name="username"></param>
/// <param name="password"></param>
/// <returns></returns>
public System.IO.Stream GetHttpStream(string sURL, string httpmethod,
string postparams, string username, string password) {

byte [] bytes = null;
// Get the data that is being posted (or sent) to the server
bytes = System.Text.Encoding.ASCII.GetBytes (postparams);

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(sURL);

if(username!="") request.Credentials = new
NetworkCredential(username,password);

request.Method = httpmethod;
request.ContentLength = bytes.Length;
Stream sendStream = request.GetRequestStream();
sendStream.Write(bytes,0,bytes.Length);
sendStream.Close();
WebResponse response = request.GetResponse();
Stream objStream;
objStream = response.GetResponseStream();

return objStream;

}
#endregion

Robert Chartier
Author and Developer
604-975-5590
Click here to reveal e-mail address
http://www.aspfree.com/devlinks
http://www.aspfree.com/authors/robert
http://www.aspalliance.com/nothingmn
http://www.15seconds.com/issue/wa.htm#chartier

Reply to this message...
 
    
Robert Chartier
(used to simplfy the process)

#region ConvertStreamToString
/// <summary>
/// ConvertStreamToString: Converts your standard IO.Stream to a string
/// </summary>
/// <param name="theStream"></param>
/// <returns></returns>
public string ConvertStreamToString(System.IO.Stream theStream) {
string theString="";
using (StreamReader sr = new StreamReader(theStream) ) {
theString = sr.ReadToEnd();
// Close and clean up the StreamReader
sr.Close();
}
return theString;
}
#endregion

Robert Chartier
Author and Developer
604-975-5590
Click here to reveal e-mail address
http://www.aspfree.com/devlinks
http://www.aspfree.com/authors/robert
http://www.aspalliance.com/nothingmn
http://www.15seconds.com/issue/wa.htm#chartier

Reply to this message...
 
    
Robert Chartier
NOTE: I plan on changing/adding the second method to arrays, and not a
stupid string list. :) (It was done in this manner due to project
constraints).

#region Simple XML+XSL Transformation
/// <summary>
/// Transform: Simple Transformation of an XML + XSL
/// </summary>
/// <param name="xml"></param>
/// <param name="xsl"></param>
/// <returns></returns>
public string Transform(string xml, string xsl) {

System.IO.StringWriter sResult = new System.IO.StringWriter();
System.Xml.XmlDocument docXml = new System.Xml.XmlDocument();

System.Xml.XmlDocument docXsl = new System.Xml.XmlDocument();
System.Xml.Xsl.XslTransform xslTransform = new
System.Xml.Xsl.XslTransform();

//LOAD UP THE XML DOCUMENT
if (xml.IndexOf("<?xml") >= 0) {
//pass xml content in
try {
docXml.LoadXml(xml);
} catch( Exception exc) {
}

} else {
//try to load via URI
try {
docXml.Load(xml);
} catch( Exception exc) {
}
}

//LOAD UP THE XSL DOCUMENT
//load xsl to a xsltransform object
try {
xslTransform.Load(xsl);
} catch( Exception exc) {
}

//try to transform it
try {
// call transform - output streamed to console
xslTransform.Transform(docXml, null, sResult);
} catch( Exception exc) {
}
return sResult.ToString();

} //Transform

/// <summary>
/// Transform: transforms and xml+xsl with given xsl parameters
/// </summary>
/// <param name="xml"></param>
/// <param name="xsl"></param>
/// <param name="xslParamNames"></param>
/// <param name="xslParamValues"></param>
/// <returns></returns>
public string Transform(string xml, string xsl, string xslParamNames,
string xslParamValues) {

System.IO.StringWriter sResult = new System.IO.StringWriter();
XmlDocument docXml = new XmlDocument();
XmlDocument docXsl = new XmlDocument();
//DocumentNavigator navSource;
System.Xml.Xsl.XslTransform xslTransform = new
System.Xml.Xsl.XslTransform();

//LOAD UP THE XML DOCUMENT
if (xml.IndexOf("<?xml") >= 0) {
//pass xml content in
try {
docXml.LoadXml(xml);
} catch( Exception exc) {
}

} else {
//try to load via URI
try {
docXml.Load(xml);
} catch( Exception exc) {
}
}

//LOAD UP THE XSL DOCUMENT
//load xsl to a xsltransform object
try {
xslTransform.Load(xsl);
} catch( Exception exc) {
}

//load up the xsl parameters, if any
System.Xml.Xsl.XsltArgumentList xslArgs = new
System.Xml.Xsl.XsltArgumentList();
if (xslParamNames!=""){
char[] chSplit = {','};
string[] pNames;
string[] pValues;

pNames = xslParamNames.Split(chSplit);
pValues = xslParamValues.Split(chSplit);

for(int i=0;i<pNames.Length;i++) {
xslArgs.AddParam(pNames[i],"",pValues[i]);
}
}

//try to transform it
try {
// call transform - output streamed to console
if (xslParamNames!=""){
xslTransform.Transform(docXml, xslArgs, sResult);
} else {
xslTransform.Transform(docXml, null, sResult);
}
} catch( Exception exc) {
}
return sResult.ToString();

} //Transform

#endregion

Robert Chartier
Author and Developer
604-975-5590
Click here to reveal e-mail address
http://www.aspfree.com/devlinks
http://www.aspfree.com/authors/robert
http://www.aspalliance.com/nothingmn
http://www.15seconds.com/issue/wa.htm#chartier

Reply to this message...
 
 
System.IO.Stream
System.IO.StreamReader
System.IO.StringWriter
System.Net.HttpWebRequest
System.Net.NetworkCredential
System.Net.WebProxy
System.Net.WebRequest
System.Net.WebResponse
System.Text.Encoding
System.Xml.XmlDocument
System.Xml.Xsl.XsltArgumentList
System.Xml.Xsl.XslTransform




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification