我用c#做了个socket访问http服务器,但返回HTTP/1.1 400 Bad RequestContent-Type: text/htmlDate: Thu, 30 Nov 2006 15:16:33 GMTConnection: closeCon,已经困了我一个月了,哪位高手帮我解决我所有的分都给他我的代码是
  string hostName="www.eshuba.com";
IPHostEntry ipInfo=Dns.GetHostByName("www.eshuba.com"); 
IPAddress[] ipAddr=ipInfo.AddressList; 
IPAddress ip=ipAddr[0]; 
int port=Int32.Parse("80"); IPEndPoint hostEP=new IPEndPoint(ip,port); 
Socket socket=new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); 
try 

socket.Connect(hostEP); 
if (socket.Connected)Response.Write("ddddddddd");
} catch(Exception se) 

    Response.Write("adfsafsdfds");
}  string sendStr="GET /down.asp?id=6794&no=1 HTTP/1.1\r\n 
Accept: */*\r\n
 Accept-Language: zh-cn\r\n 
UA-CPU: x86\r\n
 User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; Maxthon; .NET CLR 1.1.4322; .NET CLR 2.0.50727)\r\n 
Host: www.eshuba.com\r\n
Connection: Keep-Alive\r\n\r\n ";
///此外内容我是根据httplook得到.应该不会错.
byte[] bytesSendStr=new byte[1024]; 
bytesSendStr=Encoding.ASCII.GetBytes(sendStr); bytesSendStr=Encoding.ASCII.GetBytes(sendStr);
try 

//向主机发送请求 
socket.Send(bytesSendStr,bytesSendStr.Length,0); 

catch(Exception ce) 
{ } string recvStr=""; byte[] recvBytes=new byte[1024]; 
int bytes=0; 
while(true) 

bytes=socket.Receive(recvBytes,recvBytes.Length,0); 
if(bytes<=0) 
break; 
recvStr+=Encoding.ASCII.GetString(recvBytes,0,bytes); recvStr+=Encoding.ASCII.GetString(recvBytes,0,bytes);

tb_class.Text=recvStr;////我最后得到的是tb_class.text = HTTP/1.1 400 Bad RequestContent-Type: text/htmlDate: Thu, 30 Nov 2006 15:16:33 GMTConnection: closeContent-Length: 39<h1>Bad Request (Invalid Hostname)</h1>
哪位大哥指点一下到底错在哪里,万分感激.

解决方案 »

  1.   

    我把GET /down.asp?id=6794&no=1 HTTP/1.1\r\n 改为HTTP/1.0居然可以接收到东西,虽然不是正确的返回信息,我真是奇怪了,改为1.0服务器返回
    HTTP/1.1 404 Not FoundContent-Length: 1308Content-Type: text/htmlServer: Microsoft-IIS/6.0Date: Fri, 01 Dec 2006 06:03:02 GMTConnection: close<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><HTML><HEAD><TITLE>无法找到该页</TITLE><META HTTP-EQUIV="Content-Type" Content="text/html; charset=GB2312"><STYLE type="text/css">  BODY { font: 9pt/12pt 宋体 }  H1 { font: 12pt/15pt 宋体 }  H2 { font: 9pt/12pt 宋体 }  A:link { color: red }  A:visited { color: maroon }</STYLE></HEAD><BODY><TABLE width=500 border=0 cellspacing=10><TR><TD><h1>无法找到该页</h1>您正在搜索的页面可能已经删除、更名或暂时不可用。<hr><p>请尝试以下操作:</p><ul><li>确保浏览器的地址栏中显示的网站地址的拼写和格式正确无误。</li><li>如果通过单击链接而到达了该网页,请与网站管理员联系,通知他们该链接的格式不正确。</li><li>单击<a href="javascript:history.back(1)">后退</a>按钮尝试另一个链接。</li></ul><h2>HTTP 错误 404 - 文件或目录未找到。<br>Internet 信息服?(IIS)</h2><hr><p>技术信息(为技术支持人员提供)</p><ul><li>转到 <a href="http://go.microsoft.com/fwlink/?linkid=8180">Microsoft 产品支持服务</a>并搜索包括&ldquo;HTTP&rdquo;和&ldquo;404&rdquo;的标题。</li><li>打开&ldquo;IIS 帮助&rdquo;(可在 IIS 管理器 (inetmgr) 中访问),然后搜索标题为&ldquo;网站设置&rdquo;、&ldquo;常规管理任务&rdquo;和&ldquo;关于自定义错误消息&rdquo;的主题。</li></ul></TD></TR><
      

  2.   

    唉,已经好几天在弄这个东西啦。学习是艰苦的。还是很感谢你,NET人生
      

  3.   

    为什么不用 HttpRequest ...
      

  4.   

    我也用了HttpRequest,但我试了一下,HttpRequest好像只返回了最终的服务器信息,浏览器跳转前的内容取不到,不知JUSTACODER兄有什么办法。
      

  5.   

    顺便说明,我刚转学NET三个月,以前我一直从事数据库开发。所以很多NET的东西还不懂。以前因为不想学习,用PB简单,现在要重新开始学习。
      

  6.   

    HBXTLHX兄能否贴出一个方案,感激不尽。