本人新手,跟这网上的例子写了个SOCKET获取HTML的代码,但是不能获取HTML,求大神们指点代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("http://127.0.0.1/test.html");
Encoding e = Encoding.UTF8;
string requestheader = "GET " + uri.AbsolutePath + " HTTP/1.1" + System.Environment.NewLine + "Connection: Keep-Alive" + System.Environment.NewLine + "Host: " + uri.Host;
byte[] requestbyte = e.GetBytes(requestheader);
byte[] receivebyte = new byte[1024*100];
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
s.Connect(uri.Host,uri.Port);
s.Send(requestbyte);
int receivebytelength = s.Receive(receivebyte);
s.Shutdown(SocketShutdown.Both);
s.Close();
string html = string.Empty;
if (receivebytelength > 0)
{
html = e.GetString(receivebyte, 0, receivebytelength);
}
Console.Write(html);
//Console.Write(requestheader);
Console.ReadKey();
}
}
}
我是刚学SOCKET的,是在不知道问题在哪里,所以只好在这里发问,路过的大神麻烦帮下忙,感激不尽!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Uri uri = new Uri("http://127.0.0.1/test.html");
Encoding e = Encoding.UTF8;
string requestheader = "GET " + uri.AbsolutePath + " HTTP/1.1" + System.Environment.NewLine + "Connection: Keep-Alive" + System.Environment.NewLine + "Host: " + uri.Host;
byte[] requestbyte = e.GetBytes(requestheader);
byte[] receivebyte = new byte[1024*100];
Socket s = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
s.Connect(uri.Host,uri.Port);
s.Send(requestbyte);
int receivebytelength = s.Receive(receivebyte);
s.Shutdown(SocketShutdown.Both);
s.Close();
string html = string.Empty;
if (receivebytelength > 0)
{
html = e.GetString(receivebyte, 0, receivebytelength);
}
Console.Write(html);
//Console.Write(requestheader);
Console.ReadKey();
}
}
}
我是刚学SOCKET的,是在不知道问题在哪里,所以只好在这里发问,路过的大神麻烦帮下忙,感激不尽!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货