|
.net
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
n00b question re= WebClient classpresentation 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,
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 -- Show quoteCo-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(); > } > > Peter wrote:
> Chris, I ended up using HttpResponse after all. For anyone else wondering,> 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 > 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()); } |
|||||||||||||||||||||||