asp.net 模似http请求代码

作者:袖梨 2022-06-25

asp教程.net 模似http请求代码
/*
一般 模似http请求都用到sock来,设置一些相关请求,让服务器觉得是用户正常浏览。
*/
//返回 0 成功注册 1 帐户错误 2 网络不通 3 验证码错误 4登陆次数过多
        public int regpost(ref taccountinfo taccountinfo)
        {

            clog.writelog("regpost start");

            string url = "http://***";

            httpwebrequest request = (httpwebrequest)httpwebrequest.create(url);
            request.method = "post";
            request.contenttype = "application/x-www-form-urlencoded";
            request.headers.add("accept-language", "zh-cn");

            request.servicepoint.expect100continue = false;
            request.cookiecontainer = m_cookiecontainer;

            //填充要post的内容
            string strpostdata = "***";

            byte[] bytestopost = encoding.ascii.getbytes(strpostdata);
            request.contentlength = bytestopost.length;
            //request.allowautoredirect = false;

            stream requeststream = null;

            try
            {
                requeststream = request.getrequeststream();
                requeststream.write(bytestopost, 0, bytestopost.length);
            }
            catch
            {
                if (requeststream != null)
                {
                    requeststream.dispose();
                }
                request.abort();

                clog.writelog("regpost end 1");

                return 2;
            }

            requeststream.close();

            int nstatus = 0;

            try
            {
                httpwebresponse response = (httpwebresponse)request.getresponse();

                nstatus = response.statuscode.gethashcode();

                stream responsestream = response.getresponsestream();

                streamreader readstream = new streamreader(responsestream, system.text.encoding.utf8);
                string page = readstream.readtoend();
                response.close();

                clog.writelog("regpost end 2");

                //如果登陆失败,分析原因
                //分析结果
                if (isyzmwrong(ref  page))
                {
                    return 3;// 验证码错误     
                }
                else if (isloginsuccess(ref  page))//成功
                {
                    return 0;
                }
                else
                {
                    return 1;// 帐户密码错误
                }
            }
            catch
            {
                clog.writelog("regpost end 3");
             
                request.abort();
                return 2;
            }
        }

相关文章

精彩推荐