Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Newbie: fetching XML over http
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 20th April 14:36
chris
External User
 
Posts: 1
Default Newbie: fetching XML over http



I have an external application that returns XML in response to http
requests. I need to get the XML and process it in a 1.2 .NET/ASP/IIS
environment. I'm just getting started with .NET and know very little about
it (I'm primarily a Java developer). What's the best approach here?
  Reply With Quote


  sponsored links


2 20th April 18:47
dickster
External User
 
Posts: 1
Default Newbie: fetching XML over http



Im not sure if you send XML down in your Request but in case you do
this function will help (if you dont the second half of the function is
all you will need)

Basically this example goes:
-------------------------------------------
Request XmlDo***ent -> Request Stream -> {Remote Server} -> Response
Stream -> Response XmlDo***ent

************************************************** ****************
using System.Xml
using System.Web
using System.Net
using System.IO

private XmlDo***ent GetRemoteResponse(XmlDo***ent p_RequestXML, string
p_TARGET_URL)
{
HttpWebRequest req;
HttpWebResponse resp;
StreamWriter sw;
StreamReader sr;
XmlDo***ent xDoc;

//Initialise the Request object
req = (HttpWebRequest)(WebRequest.Create(p_TARGET_URL));
req.KeepAlive = false;
req.Method = "POST";
req.ContentType = "text/xml";

//Try to pump the XML into the stream with myWriter
sw = new StreamWriter(req.GetRequestStream());
sw.Write(p_RequestXML.OuterXml);
sw.Flush();
sw.Close();

//Ping their site
resp = ((HttpWebResponse)(req.GetResponse));

// Set sr to read form response stream
sr = new StreamReader(resp.GetResponseStream());
xDoc = new XmlDo***ent();
xDoc.LoadXml(sr.ReadToEnd());
return xDoc;
}
  Reply With Quote
3 20th April 18:47
dickster
External User
 
Posts: 1
Default Newbie: fetching XML over http


Im not sure if you send XML down in your Request but in case you do
this function will help

Basically this example goes:
-------------------------------------------
Request XmlDo***ent -> Request Stream -> {Remote Server} -> Response
Stream -> Response XmlDo***ent


Using say a C# Console Application


using System;
using System.Xml;
using System.Web;
using System.Net;
using System.IO;


// In the main() method
static void Main(string[] args)
{

XmlDo***ent reqXdoc = new XmlDo***ent;
XmlDo***ent respXdoc;


reqXdoc.Load(<<request xml>>);
respXdoc = GetRemoteResponse(reqXdoc,"http://whereever.com");

Console.Out(respXdoc.OuterXml);
Console.Read();
}

private XmlDo***ent GetRemoteResponse(XmlDo***ent p_RequestXML, string
p_TARGET_URL)
{
HttpWebRequest req;
HttpWebResponse resp;
StreamWriter sw;
StreamReader sr;
XmlDo***ent xDoc;

//Initialise the Request object
req = (HttpWebRequest)(WebRequest.Create(p_TARGET_URL));
req.KeepAlive = false;
req.Method = "POST";
req.ContentType = "text/xml";

//Try to pump the XML into the stream with myWriter
sw = new StreamWriter(req.GetRequestStream());
sw.Write(p_RequestXML.OuterXml);
sw.Flush();
sw.Close();

//Ping their site
resp = ((HttpWebResponse)(req.GetResponse));

// Set sr to read form response stream
sr = new StreamReader(resp.GetResponseStream());
xDoc = new XmlDo***ent();
xDoc.LoadXml(sr.ReadToEnd());
return xDoc;
}

****ster
  Reply With Quote
4 20th April 22:12
martin honnen
External User
 
Posts: 1
Default Newbie: fetching XML over http


There is .NET 1.0, 1.1, and 2.0 but no 1.2 version.

The XML APIs like XmlDo***ent, XPathDo***ent, Xml(Text)Reader usually
have a Load method or a constructor taking a Url thus if you simply want
to make a HTTP GET request then pass the URL in

Besides that, to make HTTP requests in the .NET framework there is WebClient
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetWebClientClassTopic.asp>
and then there is WebRequest/HttpWebRequest and HttpWebResponse
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemNetHttpWebRequestClassTopic.asp>

All XML APIs like XmlDo***ent, XPathDo***ent, Xml(Text)Reader can read
from a stream so using either WebClient or HttpWebRequest if you e.g.
want to POST data and then using the response stream you get as an input
to the XML APIs should do.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  Reply With Quote
5 21st April 01:31
dickster
External User
 
Posts: 1
Default Newbie: fetching XML over http


Martin

I take it that means I could simplify the above function from:
sr = new StreamReader(resp.GetResponseStream());
xDoc.LoadXml(sr.ReadToEnd());
to
xDoc.LoadXml(resp.GetResponseStream());


Q.) Is there a way to directly write the XML contents of XmlDo***ent to
an HttpWebRequest objects RequestStream?

****ster
  Reply With Quote
6 21st April 01:31
martin honnen
External User
 
Posts: 1
Default Newbie: fetching XML over http


LoadXml takes a string value with the XML markup but the Load method has
an overload which takes a stream so you could do e.g. xDoc.Load(resp.GetResponseStream())

Sure, pass that stream to the Save method of the XmlDo***ent instance.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  Reply With Quote
7 21st April 01:31
dickster
External User
 
Posts: 1
Default Newbie: fetching XML over http


Martin,

Sorry meant to say XmlDo***ent.Load()
I see Stream,TextReader & XmlReader in the Overload List

Thanks for the insight on the .Save() method

I'm off now to rework some code :-)

****ster
  Reply With Quote


  sponsored links


Reply


Thread Tools
Display Modes




Copyright © 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666