"C# Learner" <cs****@learner.here> wrote in message
news:qc**************@csharp.learner...
On Sat, 25 Sep 2004 00:19:38 -0300, Fernando Ronci wrote:
What I need to do is very simple.
I want to programmaticaly issue an HTTP query to a URL and catch the
returned (raw) HTML code and save it to a file. I don't need to parse or
render the returned markup, just issue the query and catch the response.
I'll highly appreciate guidelines or hints on how this is achieved in C#
as I don't have any experience in sockets programming.
Take a look at HttpWebRequest and HttpWebResponse - the required
functionality is implemented in these two classes.
Example of usage here: http://tinyurl.com/54zc5
What could have gone wrong with the following code extracted from Whidbey's
online help that crashes with an unhandled error ?
The exact error message from the Visual Studio Just-In-Time debugger window
is:
"An unhandled exception ('System.Net.WebException') ocurred in httptest.exe
[2344]"
The error shows up when 'httptest http://192.168.0.2/test.html' is run, for
example. As I said above, I got the example that uses HttpWebRequest and
HttpWebResponse from Whidbey's help. (BTW,
http://192.168.0.2/test.html is
available)
Thank you,
Fernando Ronci
E-mail:
fe***********@hotmail.com
---------------
#region Using directives
using System;
using System.Net;
using System.Collections.Generic;
using System.Text;
using System.IO;
#endregion
namespace httptest
{
public class Program
{
public static void Main(string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create
(args[0]);
// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse
();
Console.WriteLine ("Content length is {0}",
response.ContentLength);
Console.WriteLine ("Content type is {0}", response.ContentType);
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream ();
// Pipes the stream to a higher level stream reader with the
required encoding format.
StreamReader readStream = new StreamReader (receiveStream,
Encoding.UTF8);
Console.WriteLine ("Response stream received.");
Console.WriteLine (readStream.ReadToEnd ());
response.Close ();
readStream.Close ();
}
}
}
/*
The output from this example will vary depending on the value passed into
Main
but will be similar to the following:
Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
....
</html>
*/