n00b question re= WebClient class

C

Chris Dunaway

I am using the following code to test an .aspx page which has no
presentation and only a handler for the Page_Load event. I am using
this code to POST the contents of an .xml file to the .aspx page.

The page received the file just find and uses Response.Write to send
back a response to indicate it received the file.

I am at a loss to understand how to get this response back from the
WebClient.

I also looked at the HttpRequest class and I can get a response stream
from that, but how do I use that to POST to the .aspx page?

Thanks,

Chris



static void Main(string[] args)
{
WebClient wc = new WebClient();
Stream w =
wc.OpenWrite("http://localhost:2062/TestPage.aspx");
StreamWriter sw = new StreamWriter(w);

using (StreamReader sr = new StreamReader("test.xml"))
{
sw.Write(sr.ReadToEnd());
sw.Flush();
}

sw.Close();

wc.Dispose();
}
 
G

Guest

Chris,
You need to use one of the WebClient methods that returns the response.
UploadString can be used for uploading your Xml, and has a string return
value.
Peter
 
C

Chris Dunaway

Peter said:
Chris,
You need to use one of the WebClient methods that returns the response.
UploadString can be used for uploading your Xml, and has a string return
value.
Peter

I ended up using HttpResponse after all. For anyone else wondering,
here is the code I used:

static void Main(string[] args)
{

Uri uri = new Uri("http://server/page.aspx");
HttpWebRequest wrq = WebRequest.CreateDefault(uri) as
HttpWebRequest;

wrq.Method = WebRequestMethods.Http.Post;

using (StreamWriter sw = new StreamWriter(wrq.GetRequestStream()))
{
using (StreamReader sr = new StreamReader("test.xml"))
{
sw.Write(sr.ReadToEnd());
sw.Flush();
}
}

HttpWebResponse rsp = wrq.GetResponse() as HttpWebResponse;

StringBuilder sb = new StringBuilder();
using (StreamReader sr = new StreamReader(rsp.GetResponseStream()))
{
sb.Append(sr.ReadToEnd());
}

Console.Write(sb.ToString());
}
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top