使用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;
}
...全文
277 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
心情解码 2006-06-01
  • 打赏
  • 举报
回复
:)





-
strawren 2006-05-31
  • 打赏
  • 举报
回复
你调用 s.Send(buffOfSent, buffOfSent.Length, 0);
后调用
s.flush()(JAVA里是这个方法,看C#是个什么类似的方法)
再调用
s.Receive()
试试。
好象是跟I/O流的相关
lxhvc 2006-05-31
  • 打赏
  • 举报
回复
..........
lxhvc 2006-05-31
  • 打赏
  • 举报
回复
自己顶一下,期待牛人出现。

111,095

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧