473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Sockets programming in C#

Hi,

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.

Thank you,

Fernando Ronci
E-mail: fe***********@hotmail.com
Nov 16 '05 #1
5 1470
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
Nov 16 '05 #2
This namespace might not work correct if you catch the raw html from server
side script.
"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

Nov 16 '05 #3

"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>

*/

Nov 16 '05 #4
On Sun, 26 Sep 2004 12:32:12 -0300, Fernando Ronci wrote:
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)


Is an HTTP server listening on 192.168.0.2:80, and able to serve
"test.html"?

I ran your code, specifiying "http://127.0.0.1/test.html", after setting an
HTTP server to listen on 127.0.0.1:80 and to be able to serve test.html,
and no exceptions were raised here. I doubt that it matters, but I'm using
..NET 1.1, not Whidbey.
Nov 16 '05 #5
[re-sent because last didn't get through]

On Sun, 26 Sep 2004 12:32:12 -0300, Fernando Ronci wrote:
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)


Is an HTTP server listening on 192.168.0.2:80, and able to serve
"test.html"?

I ran your code, specifiying "http://127.0.0.1/test.html", after setting an
HTTP server to listen on 127.0.0.1:80 and to be able to serve test.html,
and no exceptions were raised here. I doubt that it matters, but I'm using
..NET 1.1, not Whidbey.
Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: jubi | last post by:
I am trying to program for a PocketPC using C#. I know it is possible for sockets programming for a desktop using C#. I'd like to know if it's possible to: 1. Sockets programming for the...
4
by: 0to60 | last post by:
I have a question about socket programming in general. Exactly what happens behind the scenes when I one socket connects to a different socket in listen mode? Using the dotnet framework, I...
6
by: Laxmikant Rashinkar | last post by:
Is there any way to use a C# socket in promiscuous mode? Any sample code that shows how this is done? any assistance is much appreciated! thanks LK
1
by: Sougato Das | last post by:
Does anyone know any good places for information on sockets programming using VB.NET. I know VB.NET fairly well, but I know very little about sockets. Thanks.
4
by: BadOmen | last post by:
Hi, What is the different between 'System.Net.Sockets.Socket' and 'System.Net.Sockets.TcpClient'? When do I use System.Net.Sockets.TcpClient and System.Net.Sockets.Socket?? Yours, Jonas
3
by: Michael Maercker | last post by:
hi! i'm really not into networking at all and have now been asigned the task of porting a vb6-code into vb.net (compact framework, in this case) and the code uses the winsock-control. i quickly...
2
by: djc | last post by:
I am very new to this... I just started playing around with some network programming. I really only use vb.net. I say that so I don't get the 'use c/c++' answer. Programming is a hobby for me, not...
1
by: Farslayer | last post by:
I'm trying to find a good source for programming sockets with VB.NET. The examples in the MSDN help do not show how to accept incoming connections and add this connection to a local array or...
2
by: a_agaga | last post by:
Do you know are there some reasons why many do not make processes to communicate through memory? Why network connections (sockets) are used so commonly in IPC (inter process communication)...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.