上行下效登录总计,检查二维码的失灵状态

By admin in 4858.com on 2019年3月26日

在开发者界面用能够见到不少ptqrlogin起首的 name

smart qqC#支出总计:

4858.com 14858.com 2/**//// <summary>  登录
4858.com 3        /// </summary>
4858.com 4        /// <param name=”url”></param>
4858.com 5        /// <param name=”paramList”></param>
4858.com 6        /// <returns></returns>
4858.com 7        public static string Login(String url, String paramList)
4858.com 84858.com 9        4858.com 10{
4858.com 11            HttpWebResponse res = null;
4858.com 12            string strResult = “”;
4858.com 13            try
4858.com 144858.com 15            4858.com 16{
4858.com 17                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
4858.com 18                req.Method = “POST”;
4858.com 19                req.ContentType = “application/x-www-form-urlencoded”;
4858.com 20                req.AllowAutoRedirect = false;
4858.com 21                CookieContainer cookieCon = new CookieContainer();
4858.com 22                req.CookieContainer = cookieCon;
4858.com 23                StringBuilder UrlEncoded = new StringBuilder();
4858.com 244858.com 25                Char[] reserved = 4858.com 26{ ‘?’, ‘=’, ‘&’ };
4858.com 27                byte[] SomeBytes = null;
4858.com 28                if (paramList != null)
4858.com 294858.com 30                4858.com 31{
4858.com 32                    int i = 0, j;
4858.com 33                    while (i < paramList.Length)
4858.com 344858.com 35                    4858.com 36{
4858.com 37                        j = paramList.IndexOfAny(reserved, i);
4858.com 38                        if (j == -1)
4858.com 394858.com 40                        4858.com 41{
4858.com 42                            UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length – i)));
4858.com 43                            break;
4858.com 44                        }
4858.com 45                        UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j – i)));
4858.com 46                        UrlEncoded.Append(paramList.Substring(j, 1));
4858.com 47                        i = j + 1;
4858.com 48                    }
4858.com 49                    SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
4858.com 50                    req.ContentLength = SomeBytes.Length;
4858.com 51                    Stream newStream = req.GetRequestStream();
4858.com 52                    newStream.Write(SomeBytes, 0, SomeBytes.Length);
4858.com 53                    newStream.Close();
4858.com 54                }
4858.com 55                else
4858.com 564858.com 57                4858.com 58{
4858.com 59                    req.ContentLength = 0;
4858.com 60                }
4858.com 61
4858.com 62                res = (HttpWebResponse)req.GetResponse();
4858.com 63                cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url));
4858.com 64                Stream ReceiveStream = res.GetResponseStream();
4858.com 65                Encoding encode = System.Text.Encoding.GetEncoding(“GBK”);
4858.com 66                StreamReader sr = new StreamReader(ReceiveStream, encode);
4858.com 67                Char[] read = new Char[256];
4858.com 68                int count = sr.Read(read, 0, 256);
4858.com 69                while (count > 0)
4858.com 704858.com 71                4858.com 72{
4858.com 73                    String str = new String(read, 0, count);
4858.com 74                    strResult += str;
4858.com 75                    count = sr.Read(read, 0, 256);
4858.com 76                }
4858.com 77            }
4858.com 78            catch (Exception e)
4858.com 794858.com 80            4858.com 81{
4858.com 82                strResult = e.ToString();
4858.com 83            }
4858.com 84            finally
4858.com 854858.com 86            4858.com 87{
4858.com 88                if (res != null)
4858.com 894858.com 90                4858.com 91{
4858.com 92                    res.Close();
4858.com 93                }
4858.com 94            }
4858.com 95            return strResult;
4858.com 96        }
4858.com 974858.com 98        /**//// <summary>  获取页面HTML
4858.com 99        /// </summary>
4858.com 100        /// <param name=”url”></param>
4858.com 101上行下效登录总计,检查二维码的失灵状态。        /// <param name=”paramList”></param>
4858.com 102        /// <returns></returns>
4858.com 103        public static string getPage(String url, String paramList)
4858.com 1044858.com 105        4858.com 106{
4858.com 107            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
4858.com 108            req.Headers[“If-None-Match”] = “36d0ed736e88c71:d9f”;
4858.com 109            req.Referer = “”;
4858.com 110            CookieContainer cookieCon = new CookieContainer();
4858.com 111            req.CookieContainer = cookieCon;
4858.com 112            req.CookieContainer.SetCookies(new Uri(url), cookieheader);
4858.com 113            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
4858.com 114            StreamReader sr = new StreamReader(res.GetResponseStream(),Encoding.Default);
4858.com 115            string strResult = sr.ReadToEnd();
4858.com 116            sr.Close();
4858.com 117            return strResult;
4858.com 118        }

4858.com 119

全总开发下来其实有个别都不是很难,从一发轫二维码
获取到最终的收发消息,基本上都以仿照浏览器的操作。都是基于http通信。一下就是这次新手学习http协议的最器重的二个类

调用:

4858.com 120/// <summary>  登录
4858.com 121        /// </summary>
4858.com 122        /// <param name=”url”></param>
4858.com 123        /// <param name=”paramList”></param>
4858.com 124        /// <returns></returns>
4858.com 125        public static string Login(String url, String paramList)
4858.com 126        {
4858.com 127            HttpWebResponse res = null;
4858.com 128            string strResult = “”;
4858.com 129            try
4858.com 130            {
4858.com 131                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
4858.com 132                req.Method = “POST”;
4858.com 133                req.ContentType = “application/x-www-form-urlencoded”;
4858.com 134                req.AllowAutoRedirect = false;
4858.com 135                CookieContainer cookieCon = new CookieContainer();
4858.com 136                req.CookieContainer = cookieCon;
4858.com 137                StringBuilder UrlEncoded = new StringBuilder();
4858.com 138                Char[] reserved = { ‘?’, ‘=’, ‘&’ };
4858.com 139                byte[] SomeBytes = null;
4858.com 140                if (paramList != null)
4858.com 141                {
4858.com 142                    int i = 0, j;
4858.com 143                    while (i < paramList.Length)
4858.com 144                    {
4858.com 145                        j = paramList.IndexOfAny(reserved, i);
4858.com 146                        if (j == -1)
4858.com 147                        {
4858.com 148                            UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, paramList.Length – i)));
4858.com 149                            break;
4858.com 150                        }
4858.com 151                        UrlEncoded.Append(HttpUtility.UrlEncode(paramList.Substring(i, j – i)));
4858.com 152                        UrlEncoded.Append(paramList.Substring(j, 1));
4858.com 153                        i = j + 1;
4858.com 154                    }
4858.com 155                    SomeBytes = Encoding.UTF8.GetBytes(UrlEncoded.ToString());
4858.com 156                    req.ContentLength = SomeBytes.Length;
4858.com 157                    Stream newStream = req.GetRequestStream();
4858.com 158                    newStream.Write(SomeBytes, 0, SomeBytes.Length);
4858.com 159                    newStream.Close();
4858.com 160                }
4858.com 161                else
4858.com 162                {
4858.com 163                    req.ContentLength = 0;
4858.com 164                }
4858.com 165
4858.com 166                res = (HttpWebResponse)req.GetResponse();
4858.com 167                cookieheader = req.CookieContainer.GetCookieHeader(new Uri(url));
4858.com 168                Stream ReceiveStream = res.GetResponseStream();
4858.com 169                Encoding encode = System.Text.Encoding.GetEncoding(“GBK”);
4858.com 170                StreamReader sr = new StreamReader(ReceiveStream, encode);
4858.com 171                Char[] read = new Char[256];
4858.com 172                int count = sr.Read(read, 0, 256);
4858.com 173                while (count > 0)
4858.com 174                {
4858.com 175                    String str = new String(read, 0, count);
4858.com 176                    strResult += str;
4858.com 177                    count = sr.Read(read, 0, 256);
4858.com 178                }
4858.com 179            }
4858.com 180            catch (Exception e)
4858.com 181            {
4858.com 182                strResult = e.ToString();
4858.com 183            }
4858.com 184            finally
4858.com 185            {
4858.com 186                if (res != null)
4858.com 187                {
4858.com 188                    res.Close();
4858.com 189                }
4858.com 190            }
4858.com 191            return strResult;
4858.com 192        }
4858.com 193        /// <summary>  获取页面HTML
4858.com 194        /// </summary>
4858.com 195        /// <param name=”url”></param>
4858.com 196        /// <param name=”paramList”></param>
4858.com 197        /// <returns></returns>
4858.com 198        public static string getPage(String url, String paramList)
4858.com 199        {
4858.com 200            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
4858.com 201            req.Headers[“If-None-Match”] = “36d0ed736e88c71:d9f”;
4858.com 202            req.Referer = “”;
4858.com 203            CookieContainer cookieCon = new CookieContainer();
4858.com 204            req.CookieContainer = cookieCon;
4858.com 205            req.CookieContainer.SetCookies(new Uri(url), cookieheader);
4858.com 206            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
4858.com 207            StreamReader sr = new StreamReader(res.GetResponseStream(),Encoding.Default);
4858.com 208            string strResult = sr.ReadToEnd();
4858.com 209            sr.Close();
4858.com 210            return strResult;
4858.com 211        }

自身要做的就是 
模拟浏览器的操作,把它转换到大家友好的客户端。所以即便是浏览器里有些我们都要效仿。接下来判断二维码失效,两秒三遍啊,弄3个Timer

/// <summary>
/// HTTP网络通讯类
/// </summary>
public static class HTTP
{

        string postData =
“userName=admin&password=pass&area=2006&Submit=%B5%C7+%C2%BC”;            
        string strLogin, strResult;
        strLogin = Login(“”, postData);

调用:

 

public static CookieContainer cookies = new CookieContainer();
4858.com,static CookieCollection CookieCollection = new CookieCollection();
static CookieContainer CookieContainer = new CookieContainer();
/// <summary>
/// HTTP请求
/// </summary>
/// <param name=”url”>连接</param>
/// <param name=”enc”>编码</param>
/// <returns></returns>
public static string HttpGet(string url, Encoding enc)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create;
request.Timeout = 10000;//设置10秒超时
request.Proxy = null;
request.Method = “GET”;
request.ContentType = “application/x-www-from-urlencoded”;
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream;
StringBuilder sb = new StringBuilder();
sb.Append(reader.ReadToEnd;
reader.Close();
reader.Dispose();
response.Close();
return sb.ToString();
}
/// <summary>
/// 带referer的http请求连接
/// </summary>
/// <param name=”url”></param>
/// <param name=”referer”></param>
/// <param name=”timeout”></param>
/// <param name=”encode”></param>
/// <param name=”NoProxy”></param>
/// <returns></returns>
public static string Get(string url, string referer =
“”, int
timeout = 100000, Encoding encode = null, bool NoProxy = false)
{
string dat;
HttpWebResponse res = null;
HttpWebRequest req;
try
{
req = (HttpWebRequest)WebRequest.Create;
req.CookieContainer = cookies;
req.AllowAutoRedirect = false;
req.Timeout = timeout;
req.Referer = referer;
if
req.Proxy = null;
req.UserAgent = “Mozilla/5.0 (Windows NT 10.0;%20WOW64; rv:47.0)
Gecko/20100101 Firefox/47.0”;
res = (HttpWebResponse)req.GetResponse();

         strResult = getPage(“”, “”);
//输出
         this.webBrowser1.Document.Write(strResult);

        string postData =
“userName=admin&password=pass&area=2006&Submit=%B5%C7+%C2%BC”;            
        string strLogin, strResult;
        strLogin = Login(“”, postData);

 

cookies.Add(res.Cookies);
}
catch (HttpException)
{
return “”;
}
catch (WebException)
{
return “”;
}
StreamReader reader;

         strResult = getPage(“”, “”);
//输出
         this.webBrowser1.Document.Write(strResult);

smart 类

reader = new StreamReader(res.GetResponseStream(), encode == null ?
Encoding.UTF8 : encode);
dat = reader.ReadToEnd();

#region 第壹讲检查二维码状态
private static System.Timers.Timer StatuTimer = new
System.Timers.Timer();
public static void Login()
{
StatuTimer.AutoReset = true;
StatuTimer.Elapsed +=StatuTimer_Elapsed;
StatuTimer.Interval = 2000;
StatuTimer.Start();
}

res.Close();
req.Abort();

public static string Login_err = “其他”;
/// <summary>
/// 检查二维码状态
/// </summary>
public static void Login_GetStatu()
{
string dat;
dat =
HTTP.Get(string.Format(“”,
ptqrtoken));
string[] temp = dat.Split(‘\”);
switch (temp[1])
{
case (“65”): //二维码失效
Login_err = “二维码失效”;
break;
case (“66”): //等待扫描
Login_err = “等待扫描”;
break;
case (“67”): //等待确认
Login_err = “等待确认”;
break;
case (“0”): //已经确认
Login_err = “已经承认”;
StatuTimer.Stop();
break;

return dat;
}
/// <summary>
/// 带referer data的http post
/// </summary>
/// <param name=”url”></param>
/// <param name=”data”></param>
/// <param name=”Referer”></param>
/// <param name=”timeout”></param>
/// <param name=”encode”></param>
/// <returns></returns>
public static string Post(string url, string data, string Referer =

method= “POST”, int timeout = 100000, Encoding encode = null)
{
string dat = “”;
HttpWebRequest req;
try
{
req = WebRequest.Create as HttpWebRequest;
req.CookieContainer = cookies;
req.ContentType = “application/x-www-form-urlencoded”;
req.Method = method;
req.Proxy = null;
req.Timeout = timeout;
req.UserAgent = “Mozilla/5.0 (Windows NT 10.0;%20WOW64; rv:47.0)
Gecko/20100101 Firefox/47.0”;
req.ProtocolVersion = HttpVersion.Version10;
req.Referer = Referer;

default: break;
}

byte[] mybyte = Encoding.Default.GetBytes;
req.ContentLength = mybyte.Length;

}

Stream stream = req.GetRequestStream();
stream.Write(mybyte, 0, mybyte.Length);

/// <summary>
/// 每两秒检查3遍二维码状态
/// </summary>
private static void StatuTimer_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
Login_GetStatu();
}
#endregion

HttpWebResponse res = req.GetResponse() as HttpWebResponse;

 

cookies.Add(res.Cookies);
stream.Close();

 

StreamReader SR = new StreamReader(res.GetResponseStream(), encode ==
null ? Encoding.UTF8 : encode);
dat = SR.ReadToEnd();
res.Close();
req.Abort();
}
catch (HttpException)
{
return “”;
}
catch (WebException)
{
return “”;
}

HTTP 类

return dat;
}
/// <summary>
/// Hash 转换
/// </summary>
/// <param name=”s”></param>
/// <returns></returns>
public static int Hash33
{
var e = 0;
foreach (var t in s)
e += (e << 5) + t;
return int.MaxValue & e;
}
/// <summary>
/// 获取CookieCollection下 所有cookie
/// </summary>
/// <param name=”cc”></param>
/// <returns></returns>
public static string GetAllCookies(CookieCollection cc)
{
//List<Cookie> lstCookies = new List<Cookie>();
string lstCookies = “”;
ArrayList table = (ArrayList)cc.GetType().InvokeMember(“m_list”,
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.GetField |
System.Reflection.BindingFlags.Instance, null, cc, new object[] { });

 

if (table != null)
{
for (int i = 0; i < table.Count; i++)
{
string[] tacookie = table[i].ToString().Split;
lstCookies = tacookie[1].ToString;
}
}

/// <summary>
/// 带referer的http请求连接
/// </summary>
/// <param name=”url”></param>
/// <param name=”referer”></param>
/// <param name=”timeout”></param>
/// <param name=”encode”></param>
/// <param name=”NoProxy”></param>
/// <returns></returns>
public static string Get(string url, string referer =
“”, int
timeout = 100000, Encoding encode = null, bool NoProxy = false)
{
string dat;
HttpWebResponse res = null;
HttpWebRequest req;
try
{
req = (HttpWebRequest)WebRequest.Create(url);
req.CookieContainer = cookies;
req.AllowAutoRedirect = false;
req.Timeout = timeout;
req.Referer = referer;
if (NoProxy)
req.Proxy = null;
req.UserAgent = “Mozilla/5.0 (Windows NT 10.0;%20WOW64; rv:47.0)
Gecko/20100101 Firefox/47.0”;
res = (HttpWebResponse)req.GetResponse();

return lstCookies;
}
public delegate void Post_Async_Action(string data);
private class Post_Async_Data
{
public HttpWebRequest req;
public Post_Async_Action post_Async_Action;
}
public static void Post_Async(string url, string PostData,
Post_Async_Action action, string Referer =
“”, int
timeout = 100000)
{
HttpWebRequest req = WebRequest.Create as HttpWebRequest;
req.CookieContainer = cookies;
req.ContentType = “application/x-www-form-urlencoded”;
req.Method = “POST”;
req.Referer = Referer;
req.UserAgent = “Mozilla/5.0 (Windows NT 10.0;%20WOW64; rv:47.0)
Gecko/20100101 Firefox/47.0”;
req.Proxy = null;
req.ProtocolVersion = HttpVersion.Version10;
req.ContinueTimeout = timeout;

cookies.Add(res.Cookies);
}
catch (HttpException)
{
return “”;
}
catch (WebException)
{
return “”;
}
StreamReader reader;

ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes;
Stream stream = req.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();

reader = new StreamReader(res.GetResponseStream(), encode == null ?
Encoding.UTF8 : encode);
dat = reader.ReadToEnd();

Post_Async_Data dat = new Post_Async_Data();
dat.req = req;
dat.post_Async_Action = action;
req.BeginGetResponse(new AsyncCallback(Post_Async_ResponesProceed),
dat);
}

res.Close();
req.Abort();

private static void Post_Async_ResponesProceed(IAsyncResult ar)
{
StreamReader reader = null;
Post_Async_Data dat = ar.AsyncState as Post_Async_Data;
HttpWebRequest req = dat.req;
HttpWebResponse res = req.GetResponse() as HttpWebResponse;
reader = new StreamReader(res.GetResponseStream;
string temp = reader.ReadToEnd();
res.Close();
req.Abort();
dat.post_Async_Action;
}
}

return dat;
}

据此总结下来差不四只假如知情一点http协议,基本上网页版的程序,我们都能够任意的转换到图形桌面版,从而依据本身本身工作进行付出。

 

协助本身接触过
最多的正是天猫商城客,天猫商城Taobao京东店数据,当然这么些都以官方api的。kaola jd
tianmao 等购物网站的分类,商品数量爬取分析。

首先次发新手教程很多恐怕看的不是很理解,不过有源码。能够依照源码的证明实行学习。其中最重庆大学的是json的解析了。因为本身也是新手,所以众多都以要查一下材质才清楚的。希望这几讲能给部分上学http协议的新手一些启发(我也是新手,说错勿怪。)

接下去便是做个图形界面包车型地铁smart qq机器人。

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 美高梅手机版4858 版权所有