Requesting a XML document programatically

K

K Viltersten

I have a site that i can request directly
from my browser's URL field by typing:

http://localhost/Search.aspx?qwert

and i wonder how i could obtain the very
same text that is rendered into the area
of my browser but assign it to a string
variable.

The pseudocode for it as follows.
string url = http://localhost/Search.aspx;
Connection c = new Connection (url);
c.Request();
string xml = c.GetXmlContentsString();

I've been googling but haven't had luck
so i guess it's something awfully basic
and simple.

Konrad Viltersten
 
N

Nicholas Paldino [.NET/C# MVP]

K Viltersten,

Have you looked at the WebClient class in the System.Net namespace? It
will do exactly what you want. If you need more control over the request
you are making, you can use the HttpWebRequest/HttpWebResponse classes in
the same namespace.
 
K

K Viltersten

Have you looked at the WebClient class in the System.Net namespace? It
will do exactly what you want. If you need more control over the request
you are making, you can use the HttpWebRequest/HttpWebResponse classes in
the same namespace.

I've been trying out some things with the
namespace you mentioned. Perhaps i'm not far
off the right code but this far, it's not
reading the stream as i want it to. This is the
code that i run.

string url = "http://localhost/Search.aspx?qwert";

WebRequest request = WebRequest.Create(url);

request.Method = "POST";

request.ContentType = "text/xml";

StreamReader reader = new StreamReader(request.GetRequestStream());

string contents = reader.ReadToEnd();

reader.Close();



I get errors when creating the stream. Please note,

not when i READ the stream but when i CREATE the reader...

Please advise.



Konrad Viltersten
 
C

Ciaran O''Donnell

The GetRequestStream function is the one that causes the request to be sent.
Try changing the Method to GET instead of POST.
If you still get errors, post the error message. They could be due to
firewalls/proxys needing authentication or a header missing from the request
so the message is key to helping with them. They the Method change first
though and let us know.
 
J

Jon Skeet [C# MVP]

I've been trying out some things with the
namespace you mentioned. Perhaps i'm not far
off the right code but this far, it's not
reading the stream as i want it to. This is the
code that i run.

The Stream you're fetching is the *request* stream - i.e. a stream for
the data which needs to get *sent* to the web server. That's not
readable - you're meant to write into it (if you want to send up any
data).

You want the *response* stream to read from.

Jon
 
J

Juan T. Llibre

re:
!> The Stream you're fetching is the *request* stream - i.e. a stream for
!> the data which needs to get *sent* to the web server. That's not
!> readable - you're meant to write into it (if you want to send up any data).

Exactly.

The link to the MSDN docs I just posted has correct sample code.




I've been trying out some things with the
namespace you mentioned. Perhaps i'm not far
off the right code but this far, it's not
reading the stream as i want it to. This is the
code that i run.

The Stream you're fetching is the *request* stream - i.e. a stream for
the data which needs to get *sent* to the web server. That's not
readable - you're meant to write into it (if you want to send up any
data).

You want the *response* stream to read from.

Jon
 
K

K Viltersten

This is almost the exact thing i need. Only
one problem remains. Just as you, guys,
suspected, there's an issue with login.
The instance of WebClient i've created
isn't logged-in yet and that's why i get
nada from the server.

So, the question is now as follows. How can
i retrieve the current credentials from
"this" so i can copy them into my instance
of WebClient)?

The link that Juan so kindly provided, led
only to DefaultNetworkCredentials and that
can't be the currently used ones...

Regards
Konrad Viltersten
 
K

K Viltersten

Small amendment. I know i can set the
credentials to the current ones by the
UseDefaultCredentials of my WebClient
object.

However, allthough it solves the issue
for this time, i feel like it's only a
lucky coincidence that i succeeded. I
prefer to be in full control.

Thank you this far and please advise.

Regards
Konrad Viltersten
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top