Requesting a XML document programatically

  • Thread starter Thread starter K Viltersten
  • Start date Start date
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
 
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.
 
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
 
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.
 
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
 
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.



Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
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
 
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
 
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
 
Back
Top