使用Socket请求Http连接的问题。
lxhvc 2006-05-31 02:47:07 调用下面这个方法请求iis服务器返回页面信息 request = "GET /atm/application/Login.aspx HTTP/1.1\r\n" + "Host: atm\r\n" + "Connection: Keep_Alive\r\n" + "Cookie: ATMuserId=6000000816\r\n\r\n";
只有当request中的Connection段为Close时才能返回应答信息,而Keep_Alive时无法返回应答信息,我的程序需要Keep_Alive,请问如何获得服务端的应答信息。
private static string SocketSendReceive(Socket socket, string request)
{
if (socket == null) throw new ArgumentNullException("socket", "Socket对象不可为空!");
if (string.IsNullOrEmpty(request)) throw new ArgumentNullException("request","请求消息不可为空!");
Socket s = socket;
string msg = request;
Byte[] buffOfSent = Encoding.ASCII.GetBytes(msg);
Byte[] buffOfReceived = new Byte[1024]; // 返回的消息缓冲。
string retMsg = string.Empty; // 返回的消息。
int count = 0; // 服务器回发内容计数。
// 发送请求。
s.Send(buffOfSent, buffOfSent.Length, 0);
s.BeginReceive(
// 该阻塞直到数据传输完成。
do
{
count = s.Receive(buffOfReceived, buffOfReceived.Length, 0);
retMsg = retMsg + Encoding.ASCII.GetString(buffOfReceived, 0, count);
}
while (count > 0);
return retMsg;
}