using System;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
public class Sample
{
public static string DoSocketGet(string server)
{
//Set up variables and String to write to the server.
Encoding ASCII = Encoding.ASCII;
string Get = "GET / HTTP/1.1\r\nHost: " + server +
"\r\nConnection: Close\r\n\r\n";
Byte[] ByteGet = ASCII.GetBytes(Get);
Byte[] RecvBytes = new Byte[256];
String strRetPage = null;
// IPAddress and IPEndPoint represent the endpoint that will
// receive the request.
// Get first IPAddress in list return by DNS.
try
{
// Define those variables to be evaluated in the next for loop and
// then used to connect to the server. These variables are defined
// outside the for loop to make them accessible there after.
Socket s = null;
IPEndPoint hostEndPoint;
IPAddress hostAddress = null;
int conPort = 80;
// Get DNS host information.
IPHostEntry hostInfo = Dns.Resolve(server);
// Get the DNS IP addresses associated with the host.
IPAddress[] IPaddresses = hostInfo.AddressList;
// Evaluate the socket and receiving host IPAddress and IPEndPoint.
for (int index=0; index
1<ipaddresses.length; ":\r\n";="" (!s.connected)="" (bytes="" );="" +="" 0);="" 0,="" ;="" a="" all="" and="" ascii.getstring(recvbytes,="" byteget.length,="" bytes="s.Receive(RecvBytes," bytes);="" connect="" connection="" connection.="" conport);="" content="" continue;="" creates="" data="" end="" failed,="" for="" get="" home="" host="" host.="" hostaddress="IPaddresses[index];" hostendpoint="new" if="" index++)="" int32="" ipaddress.="" ipendpoint(hostaddress,="" ipendpoint.="" is="" its="" loop="" loop.="" next="" of="" over="" page="" protocoltype.tcp="" receive="" received.="" recvbytes.length,="" request="" s="null;" s.connect(hostendpoint);="" s.send(byteget,="" send="" sent="" server="" socket="" socket(addressfamily.internetwork,="" sockettype.stream,="" strretpage="strRetPage" tcp="" the="" to="" try="" until="" using="" while="" {="" }=""> 0)
2{
3bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
4strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0, bytes);
5}
6
7
8} // End of the try block.
9
10catch(SocketException e)
11{
12Console.WriteLine("SocketException caught!!!");
13Console.WriteLine("Source : " + e.Source);
14Console.WriteLine("Message : " + e.Message);
15}
16catch(ArgumentNullException e)
17{
18Console.WriteLine("ArgumentNullException caught!!!");
19Console.WriteLine("Source : " + e.Source);
20Console.WriteLine("Message : " + e.Message);
21}
22catch(NullReferenceException e)
23{
24Console.WriteLine("NullReferenceException caught!!!");
25Console.WriteLine("Source : " + e.Source);
26Console.WriteLine("Message : " + e.Message);
27}
28catch(Exception e)
29{
30Console.WriteLine("Exception caught!!!");
31Console.WriteLine("Source : " + e.Source);
32Console.WriteLine("Message : " + e.Message);
33}
34
35return strRetPage;
36
37}
38public static void Main()
39{
40Console.WriteLine("请需入请求地址:");
41String server =Console.ReadLine();
42Console.WriteLine(DoSocketGet(server));
43
44//存一下档
45string path = @"D:\Socket.html";
46if(File.Exists(path))
47{
48File.Delete(path);
49}
50using(FileStream fs =File.Create(path))
51{
52Byte[] bs =new UTF8Encoding(true).GetBytes(DoSocketGet(server));
53fs.Write(bs,0,bs.Length);
54}
55}
56}</ipaddresses.length;>