PDA

View Full Version : سوال: مشکل با خواند html از طریق سوکت پروگرمینگ



abasfar
جمعه 01 اردیبهشت 1391, 16:28 عصر
سلام
دوستان شما برای خواند صفحات وب از طریق سوکت پروگرمینگ از چه کدی استفاده میکنید؟

Uri uri = new Uri(@"http://barnamenevis.org");

// Get the IPAddress of the website we are going to and create the EndPoint
IPAddress ipAddress = Dns.GetHostEntry(uri.Host).AddressList[0];
IPEndPoint endPoint = new IPEndPoint(ipAddress, 80);

// Create a new Socket instance and open the socket for communication
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
socket.Connect(endPoint);

// Attempt to send the request
int byteCount = 0;
try
{
string requestString =
"POST " + uri.PathAndQuery + " HTTP/1.1\r\n" +
"Host: " + uri.Host + "\r\n" +
"Content-Type: application/x-www-form-urlencoded\r\n" +
"Content-Length: 11\r\n" +
"\r\n" +
"user=bob";

byte[] bytesToSend = Encoding.ASCII.GetBytes(requestString);
byteCount = socket.Send(bytesToSend, SocketFlags.None);
}
catch (SocketException se)
{
Console.WriteLine(se.Message);
}

// Attempt to receive the response
if (byteCount > 0)
{
byte[] bytesReceived = new byte[256];
try
{
byteCount = socket.Receive(bytesReceived, SocketFlags.None);
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine("HELP!! --> " + e.Message);
}

// Out the html we received
string html = Encoding.ASCII.GetString(bytesReceived);
Console.WriteLine(html);
}
else
{
Console.WriteLine("byteCount is zero!");
}

Console.Read();