n00b question re= WebClient class

Discussion in 'ASP .Net' started by Chris Dunaway, Sep 7, 2006.

  1. 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();
    }
     
    Chris Dunaway, Sep 7, 2006
    #1
    1. Advertising

  2. 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




    "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();
    > }
    >
    >
     
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=, Sep 7, 2006
    #2
    1. Advertising

  3. 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());
    }
     
    Chris Dunaway, Sep 8, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Paul J. Lay

    WebClient Class Method=Post

    Paul J. Lay, May 13, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    5,926
    Joerg Jooss
    May 15, 2004
  2. Grant Harmeyer

    WebClient Class / Upload File / IIS 405 Error

    Grant Harmeyer, Sep 7, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    4,590
    Sayed Hashimi
    Sep 8, 2004
  3. =?Utf-8?B?QXJuZQ==?=

    can Webclient class be used with Asp.net Session?

    =?Utf-8?B?QXJuZQ==?=, Oct 5, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    6,275
    Scott Allen
    Oct 8, 2004
  4. Replies:
    10
    Views:
    510
    Howard
    Nov 10, 2006
  5. Wells
    Replies:
    2
    Views:
    219
    Rhodri James
    May 13, 2009
Loading...

Share This Page