C# 使用TcpClient模拟HTTP请求的使用

一般做http模拟请求的一般使用HttpWebRequest这种方式,但这种方式也存在着一些bug,因为HttpWebRequest是使用的Windows系统定制的net通信,xp跟win7他们有的地方就不一样,HttpWebRequest可以说是给Windows定制的模拟操作,如果遇见某些服务器,可能根本不吃这一套,就像我最近遇到的问题,设置的Content-Length竟然可能视为无效,但正常访问抓包ok,程序跑起来有一半多时候会出错,还有其他的一些问题,比如设置了进制自动重定向,如果再设置Expect100Continue时候,前面的设置会发生无效,无奈,尝试使用TcpClient方式解决了。当下载文件时候,HttpWebRequest好像根据操作系统有最大文件的限制,所以这个时候使用TcpClient也是不错的选择

下面是我下好的一个类,我使用已经够了,可能中间有不足,希望大家指出

class HTTPClass
    {

        public Response HTTP(string _url, string _type, string _postdata, string _cookie, Encoding _responseEncode)
        {

            try
            {
                TcpClient clientSocket = new TcpClient();
                Uri URI = new Uri(_url);
                clientSocket.Connect(URI.Host, URI.Port);
                StringBuilder RequestHeaders = new StringBuilder();

                RequestHeaders.Append(_type + " " + URI.PathAndQuery + " HTTP/1.1\r\n");
                RequestHeaders.Append("Content-Type:application/x-www-form-urlencoded\r\n");
                RequestHeaders.Append("User-Agent:Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11\r\n");
                RequestHeaders.Append("Cookie:" + _cookie + "\r\n");
                RequestHeaders.Append("Accept:*/*\r\n");
                RequestHeaders.Append("Host:" + URI.Host + "\r\n");
                RequestHeaders.Append("Content-Length:" + _postdata.Length + "\r\n");
                RequestHeaders.Append("Connection:close\r\n\r\n");

                byte[] request = Encoding.UTF8.GetBytes(RequestHeaders.ToString() + _postdata);
                clientSocket.Client.Send(request);

                byte[] responseByte = new byte[1024000];
                int len = clientSocket.Client.Receive(responseByte);
                string result = Encoding.UTF8.GetString(responseByte, 0, len);
                clientSocket.Close();
                int headerIndex = result.IndexOf("\r\n\r\n");
                string[] headerStr = result.Substring(0, headerIndex).Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                Dictionary responseHeader = new Dictionary();
                for (int i = 0; i < headerStr.Length; i++)
                {
                    string[] temp = headerStr[i].Split(new string[] { ": " }, StringSplitOptions.RemoveEmptyEntries);
                    if (temp.Length == 2)
                    {
                        if (responseHeader.ContainsKey(temp[0]))
                        {
                            responseHeader[temp[0]] = temp[1];
                        }
                        else
                        {
                            responseHeader.Add(temp[0], temp[1]);
                        }
                    }
                }
                Response response = new Response();
                response.HTTPResponseHeader = responseHeader;
                string[] httpstatus = headerStr[0].Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
                if (httpstatus.Length > 2)
                {

                    response.HTTPStatusCode = httpstatus[1];
                }
                else
                {

                    response.HTTPStatusCode = "400";
                }
                response.HTTPResponseText = _responseEncode.GetString(Encoding.UTF8.GetBytes(result.Substring(headerIndex + 4)));
                return response;

            }
            catch
            {
                return null;
            }

        }


    }

    class Response
    {

        string hTTPStatusCode;
        /// 

        /// http状态代码
        /// 

        public string HTTPStatusCode
        {
            get { return hTTPStatusCode; }
            set { hTTPStatusCode = value; }
        }
        Dictionary hTTPResponseHeader;
        /// 

        /// Response的header
        /// 

        public Dictionary HTTPResponseHeader
        {
            get { return hTTPResponseHeader; }
            set { hTTPResponseHeader = value; }
        }
        string hTTPResponseText;
        /// 

        /// html代码
        /// 

        public string HTTPResponseText
        {
            get { return hTTPResponseText; }
            set { hTTPResponseText = value; }
        }


    }

只给大家做参考使用,具体情况具体分析

以上程序对对于来获取一个小网页完全可以,但如果你网页比较大的话,就会出现接收不全的问题,这个问题主要是因为服务器那边响应请求的时候,首先把要响应的内容写如缓冲区,然后根据本身的设定一次次发送内容,而我这只是接受了一次,所以当文本过大时候,就会只接收一部分,如果你的内容过大,可以循环接收下来解决这个问题,当然,可能你有更好的方法

转载:http://www.lyblog.info/archives/955

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值