Здравствуйте. Поднял apache в составе denwer, С msdn'a взял код:
| Код | using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets;
namespace listing1 { class Program { private static Socket ConnectSocket(string server, int port) { Socket s = null; IPHostEntry hostEntry = null;
hostEntry = Dns.GetHostEntry(server);
foreach(IPAddress address in hostEntry.AddressList) { IPEndPoint ipe = new IPEndPoint(address, port); Socket tempSocket = new Socket(ipe.AddressFamily, SocketType.Stream, ProtocolType.Tcp); tempSocket.Connect(ipe); if (tempSocket.Connected) { s = tempSocket; break; } else { continue; } } return s; }
private static string SocketSendReceive(string server, int port) { string request = "GET http://localhost HTTP/1.0\r\n" + "Host: localhost\r\n" + "Accept: text/html\r\n" + "Connection: Close\r\n\r\n"; Byte[] bytesSent = Encoding.ASCII.GetBytes(request); Byte[] bytesReceved = new Byte[256];
Socket s = ConnectSocket(server, port);
if(s == null) return ("Connection failed");
s.Send(bytesSent, bytesSent.Length, 0);
int bytes = 0; string page = "Default HTML page on " + server + ":\r\n"; do{ bytes = s.Receive(bytesReceved, bytesReceved.Length, 0); page = page + Encoding.ASCII.GetString(bytesReceved, 0, bytes); } while (bytes > 0); return page; }
public static string coding(string str) { Byte[] bytes;
UTF8Encoding utf8 = new UTF8Encoding();
bytes = utf8.GetBytes(str); Char[] chars = utf8.GetChars(bytes); string s=""; foreach (Char c in chars) { s = s + c; } return s; }
static void Main(string[] args) { string host; int port = 80; if (args.Length == 0) host = Dns.GetHostName(); else host = args[0]; host = "127.0.0.1"; string result = SocketSendReceive(host, port); Console.WriteLine(coding(result)); Console.ReadKey(); } } }
|
с сервером соединяется, посылает запрос и принимает ответ.
| Код | Default HTML page on 127.0.0.1: HTTP/1.1 200 OK Date: Wed, 23 Dec 2009 18:10:55 GMT Server: Apache/2.2.4 (Win32) mod_ssl/2.2.4 OpenSSL/0.9.8d PHP/5.2.4 X-Powered-By: PHP/5.2.4 Content-Length: 148 Connection: close Content-Type: text/html; charset=windows-1251
<html> <head> <title>??????????????? ?? ????????? ???? ???????...</title> <meta http-equiv=Refresh content="0; url=/denwer/"> </head> </html>
|
Кириллицу не отображает :(( Узал поиск! указанными на форуме способами не получается отобразить русскую кодировку. Прошу помощи, срочно надо! Заранее спасибо!
P.S. при запросе страницы ya.ru тоже самое. Значит проблема в клиентской программе. |