2 liu122613153b liu122613153B 于 2013.08.23 10:33 提问

发送http请求返回{"retcode":103,"errmsg":""}

我模仿一个QQ登录的按钮功能 为什么返回的{"retcode":103,"errmsg":""}
代码如下:

            try
            { 
            //发送请求地址
            string url2 = ptsig;

            string strHtmlContent2 = "";//接收返回数据
            HttpWebRequest webRequest2 = WebRequest.Create(url2) as HttpWebRequest;//发送请求

            webRequest2.Method = "GET";//请求方法
            webRequest2.Referer = "http://d.web2.qq.com/proxy.html";//页面链接

            webRequest2.ContentType = "application/x-www-form-urlencoded";//内容类型
            //webRequest2.Timeout = 2000;//响应时间
            webRequest2.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";//客户端浏览器的原始用户代理信息

            webRequest2.CookieContainer = cookies;//获取cookie


            HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();//获得服务器响应对象 
            Stream resStream2 = response2.GetResponseStream();//转成流对象  
            StreamReader sr2 = new StreamReader(resStream2, Encoding.UTF8);//创建只读流对象
            strHtmlContent2 = sr2.ReadToEnd();
            response2.Close();//关闭流对象
            webRequest2.Abort();//关闭请求
        }
        catch (Exception e)
        {
            Msg = "qq:" + _qqno + "登陆" + e.Message;
            return false;

        }

        try
        {
            //发送请求地址
            string url2 = "http://d.web2.qq.com/channel/login2";

            string strHtmlContent2 = "";//接收返回数据
            HttpWebRequest webRequest2 = WebRequest.Create(url2) as HttpWebRequest;//发送请求

            webRequest2.Method = "POST";//请求方法

            //webRequest2.Referer = "http://d.web2.qq.com/proxy.html";//页面链接
            webRequest2.Referer = "http://d.web2.qq.com/proxy.html?v=20110331002&callback=1&id=2";

            webRequest2.ContentType = "application/x-www-form-urlencoded";//内容类型
            //webRequest2.Timeout = 2000;//响应时间
            webRequest2.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)";//客户端浏览器的原始用户代理信息

            webRequest2.CookieContainer = cookies;//获取cookie

            //上传数据
            string postData = "r={\"status\":\"online\",\"ptwebqq\":\"" + ptwebqq + "\",\"passwd_sig\":\"\",\"clientid\":\"" + clientid + "\",\"psessionid\":null}";

            // Encode the data
            byte[] encodedBytes = Encoding.UTF8.GetBytes(postData);//转成二进制数组
            webRequest2.ContentLength = encodedBytes.Length;//字节数

            // Write encoded data into request stream
            Stream requestStream = webRequest2.GetRequestStream();//转成流对象
            requestStream.Write(encodedBytes, 0, encodedBytes.Length);
            requestStream.Close();//关闭流并释放资源

            HttpWebResponse response2 = (HttpWebResponse)webRequest2.GetResponse();//获得服务器响应对象 
            Stream resStream2 = response2.GetResponseStream();//转成流对象  
            StreamReader sr2 = new StreamReader(resStream2, Encoding.UTF8);//创建只读流对象
            strHtmlContent2 = sr2.ReadToEnd();
            response2.Close();//关闭流
            webRequest2.Abort();//关闭请求
            if (strHtmlContent2.IndexOf("vfwebqq") > -1)
            {
                //获取第二次登录用户进行QQ号的操作
                vfwebqq = new Regex("vfwebqq\":\"[^\"]+").Match(strHtmlContent2).Value.Replace("vfwebqq\":\"", "");
                psessionid = new Regex("psessionid\":\"[^\"]+").Match(strHtmlContent2).Value.Replace("psessionid\":\"", "");
                Msg = "qq:" + _qqno + "登陆webqq成功";
                return true;
            }
            else
            {
                Msg = "qq:" + _qqno + "登陆受限";
                return false;
            }
        }
        catch (Exception e)
        {
            Msg = "qq:" + _qqno + "登陆" + e.Message;
            return false;

        }

1个回答

shiwenping11
shiwenping11   2015.12.25 19:11

没有把cookie带上

Csdn user default icon
上传中...
上传图片
插入图片