美高梅网投网站-美高梅手机网投-美高梅官方网站
做最好的网站

您的位置:美高梅网投网址 > 美高梅网站 > 所以只要是浏览器里有的我们都要模拟,整个开

所以只要是浏览器里有的我们都要模拟,整个开

发布时间:2019-11-06 22:15编辑:美高梅网站浏览(68)

    在开发者界面用可以看到很多ptqrlogin开头的 name

    smart qqC#开发总结:

    美高梅官方网站 1

    整个开发下来其实一点都不是很难,从一开始二维码 获取到最终的收发消息,基本上都是模拟浏览器的操作。都是基于http通讯。一下就是 本次新手学习http协议的最关键的一个类

    我要做的就是  模拟浏览器的操作,把它转换成我们自己的客户端。所以只要是浏览器里有的我们都要模拟。接下来判断二维码失效,两秒一次吧,弄一个Timer

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

     

    public static CookieContainer cookies = new CookieContainer();
    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();

     

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

    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>
    /// 每两秒检查一次二维码状态
    /// </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协议,基本上网页版的程序,我们都可以自由的转换成图形桌面版,从而根据自己自身业务进行开发。

     

    其次本人接触过 最多的就是淘宝客,淘宝天猫京东店数据,当然这些都是官方api的。kaola jd tianmao 等购物网站的分类,商品数据爬取分析。

    第一次发新手教程很多可能看的不是很明白,不过有源码。可以根据源码的注释进行学习。其中最关键的是json的解析了。因为自己也是新手,所以很多都是要查一下资料才明白的。希望这几讲能给一些学习http协议的新手一些启示(我也是新手,说错勿怪。)

    接下来就是做个图形界面的smart qq机器人。

    本文由美高梅网投网址发布于美高梅网站,转载请注明出处:所以只要是浏览器里有的我们都要模拟,整个开

    关键词: