获取网页内容的控制台程序 望指点
我试着做一个能取得并输出网页内容的windows控制台应用程序,可是始终不能成功,我是个新手,大家帮帮忙.程序代码如下:
using System;
using System.Net;
using System.IO;
using System.Net.Sockets;
using System.Text;
namespace ss
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class socket
{
static void Main(string[] args)
{
string sip = args[0];//服务器IP地址
try
{
IPAddress hostadd =Dns.Resolve(sip).AddressList[0];
IPEndPoint ephost =new IPEndPoint(hostadd,80);
Socket cliensocket =new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
cliensocket.Connect(ephost);
//建立socket
StreamReader file =File.OpenText("d:\\c\\c#\\ss.txt");
string file1;
string file2=null;
while((file1=file.ReadLine())!=null)
{
file2=file2+file1;
}
Console.WriteLine(file2);
//读取将要发送到服务器的数据,数据以文本文件形式存放在d:\\c\\c#\\ss.txt中
byte[] senddata = Encoding.ASCII.GetBytes(file2.ToCharArray());
cliensocket.Send(senddata,0,senddata.Length,SocketFlags.None);
//发送数据
byte[] redata = new byte[1024];
int i = cliensocket.Receive(redata,0,cliensocket.Available,SocketFlags.None);
string y = Encoding.ASCII.GetString(redata,0,i);
Console.WriteLine("Receive {0} bytes",i);
Console.WriteLine("data:{0}",y);
//接收并输出数据
cliensocket.Shutdown(SocketShutdown.Both);
cliensocket.Close();
//关闭socket
}
catch{}
}
}
}