By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

Requesting a XML document programatically

P: n/a
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
Jul 10 '08 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"K Viltersten" <tm**@viltersten.comwrote in message
news:6d************@mid.individual.net...
>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


Jul 10 '08 #2

P: n/a
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
Jul 10 '08 #3

P: n/a
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.
--
Ciaran O''Donnell
http://wannabedeveloper.spaces.live.com
"K Viltersten" wrote:
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
Jul 10 '08 #4

P: n/a
On Jul 10, 3:53*pm, "K Viltersten" <t...@viltersten.comwrote:
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.
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
Jul 10 '08 #5

P: n/a
See this example for the proper syntax :
http://msdn.microsoft.com/en-us/libr...ebrequest.aspx

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/
======================================
"K Viltersten" <tm**@viltersten.comwrote in message news:6d************@mid.individual.net...
>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


Jul 10 '08 #6

P: n/a
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/
======================================
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:80**********************************@r66g2000 hsg.googlegroups.com...
On Jul 10, 3:53 pm, "K Viltersten" <t...@viltersten.comwrote:
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.
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
Jul 10 '08 #7

P: n/a
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


"Juan T. Llibre" <no***********@nowhere.comskrev i meddelandet
news:eY**************@TK2MSFTNGP02.phx.gbl...
See this example for the proper syntax :
http://msdn.microsoft.com/en-us/libr...ebrequest.aspx

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/
======================================
"K Viltersten" <tm**@viltersten.comwrote in message
news:6d************@mid.individual.net...
>>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



Jul 10 '08 #8

P: n/a
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
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


"Juan T. Llibre" <no***********@nowhere.comskrev i meddelandet
news:eY**************@TK2MSFTNGP02.phx.gbl...
>See this example for the proper syntax :
http://msdn.microsoft.com/en-us/libr...ebrequest.aspx

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/
======================================
"K Viltersten" <tm**@viltersten.comwrote in message
news:6d************@mid.individual.net...
>>>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




Jul 10 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.