/// <summary>
/// Socket返回页面上的数据
/// </summary>
/// <param name="Url">访问路径 相对路径</param>
/// <param name="IP">IP地址</param>
/// <param name="Host">Host地址</param>
/// <param name="Encode">编码类型</param>
/// <returns>远程页面内容</returns>
public static string GetNews(string Url, string IP, string Host, string Encode,int port)
{
Encoding encode = System.Text.Encoding.GetEncoding(Encode);
StringBuilder sb = new StringBuilder(5000);
IPEndPoint EP = new IPEndPoint(IPAddress.Parse(IP), port);
string strToSend = "";
strToSend = "GET /" + Url + " HTTP/1.1/r/nHOST:" + Host + " /r/n/r/n";
byte[] bts = encode.GetBytes(strToSend);
byte[] RecvBuffer = new byte[1024];
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Connect(EP);
socket.ReceiveTimeout = 1000*60*10;
socket.Send(bts);
int nBytes = socket.Receive(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None);
sb.Append(encode.GetString(RecvBuffer, 0, nBytes));
while (socket.Available > 0)
{
Array.Clear(RecvBuffer, 0, RecvBuffer.Length);
nBytes = socket.Receive(RecvBuffer, 0, RecvBuffer.Length, SocketFlags.None);
sb.Append(encode.GetString(RecvBuffer, 0, nBytes));
}
}
catch
{
sb.Append("-1");
}
finally
{
socket.Close();
socket = null;
encode = null;
EP = null;
RecvBuffer = null;
bts = null;
}
return sb.ToString();
}