Home All Groups Group Topic Archive Search About

n00b question re= WebClient class

Author
7 Sep 2006 3:15 PM
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();
        }

Author
7 Sep 2006 9:25 PM
Peter Bromberg [C# MVP]
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

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




Show quote
"Chris Dunaway" wrote:

> 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();
>         }
>
>
Author
8 Sep 2006 1:04 PM
Chris Dunaway
Peter wrote:
> 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());
}

AddThis Social Bookmark Button