当前位置:编程学习 > C#/ASP.NET >>

http的GET方法

我给服务器发送一个这样的GET方法
返回400错误,有谁知道原因么?
char buf[DEFAULT_LEN+1] = "GET http://www.jsu.edu.cn/ HTTP/1.0\nAccept: */*\nAccept-Language: zh-cn\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; WPS)\nHost: www.jsu.edu.cn\nConnection: Keep-Alive\n\n";
我估计是上面这个GET方法错了。
唉,c++社区人少了只能来这里问了-_- --------------------编程问答-------------------- 不会吧

我这样就可以


            TcpClient tcpClient = new TcpClient("www.jsu.edu.cn", 8080);

            NetworkStream netStream = tcpClient.GetStream();

            if (netStream.CanWrite)
            {
                Byte[] sendBytes = Encoding.UTF8.GetBytes("GET http://www.jsu.edu.cn/ HTTP/1.0\nAccept: */*\nAccept-Language: zh-cn\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; WPS)\nHost: www.jsu.edu.cn\nConnection: Keep-Alive\n\n");
                netStream.Write(sendBytes, 0, sendBytes.Length);
            }
            else
            {
                Console.WriteLine("You cannot write data to this stream.");
                tcpClient.Close();
                netStream.Close();
                return;
            }

            if (netStream.CanRead)
            {
                byte[] bytes = new byte[tcpClient.ReceiveBufferSize];

                netStream.Read(bytes, 0, (int)tcpClient.ReceiveBufferSize);

                string returndata = Encoding.UTF8.GetString(bytes);

                Console.WriteLine("This is what the host returned to you: " + returndata);
            }
            else
            {
                Console.WriteLine("You cannot read data from this stream.");
                tcpClient.Close();
                netStream.Close();
                return;
            }
            netStream.Close(); --------------------编程问答-------------------- 我也说两句吧,如果你是已经连上去了,那么
GET / HTTP/1.0
直接这样就行了,还有HTTP1.0可以不需要HOST字段,对于1.0版本的,Connection也没有多大意义,1.0的好像总是一发送完成就关闭的。
估计可能是你的 GET 这里错了吧 --------------------编程问答-------------------- HTTP协议里面,换行符号是 \r\n

把你的\n前面都加个\r。。。。 --------------------编程问答--------------------

char buf[DEFAULT_LEN+1] = "GET / HTTP/1.0\r\n"
                          "Accept: */*\r\n"
                          "Accept-Language: zh-cn\r\n"
                          "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; QQDownload 1.7; WPS)\r\n"
                          "Host: www.jsu.edu.cn\r\n"
                          "Connection: Keep-Alive\r\n"
                          "\r\n"; 


注意 GET里面,是相对URI
不是绝对的URL 
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,