V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
xxhjkl
V2EX  ›  分享创造

分享一个优酷提速脚本,请教 c#实现问题。

  •  
  •   xxhjkl · 2016-05-14 09:05:09 +08:00 · 5814 次点击
    这是一个创建于 2916 天前的主题,其中的信息可能已经有所发展或是发生改变。
    #!/usr/bin/env python
    # coding=utf-8
    
    import requests
    import json
    import time
    import sys
    
    
    session = requests.session()
    
    def login(passport, password):
    	payload = {
    		'passport': passport,
    		'password': password,
    		'captcha': '',
    		'remember': '1',
    		'callback': 'logincallback_%d' % (time.time() * 1000),
    		'from': 'http://login.youku.com/@@@@',
    		'wintype': 'page',
    	}
    	response = session.post(url = 'https://login.youku.com/user/login_submit/', data = payload)
    	return response
    
    def main():
    	reload(sys)
    	sys.setdefaultencoding('utf-8')
    
    	response = login('passport', 'password')
    	print(response.text)
    
    	response = session.post(url = 'http://vip.youku.com/?c=ajax&a=ajax_do_speed_up')
    	print(json.dumps(response.json(), ensure_ascii=False, indent=2))
    
    if __name__ == '__main__':
    	main()
    

    用法:需要 pip install requests ,需要优酷黄金会员,电信可提速线路。 30M 以下提到 30M , 50M 可以提到 100M 。 每次提速可以维持若干小时,不像快鸟需要心跳维持。不保证每个人可用 我这里可以用的,见图:

    http://ww2.sinaimg.cn/large/93dbe2cagw1f3umvzbipyj205l04edga.jpg

    http://ww2.sinaimg.cn/large/93dbe2cagw1f3umwi2aouj20dm0fxq5l.jpg

    这个脚本最开始用 c#写的。后来遇到若干奇葩 bug ,就找基友搞成了派僧。我写的 c#版 第一个 post 包的内容都对上的,就是它返回包的 cookie 处理不好(我自己推断的原因),导致登陆不上。有木有人能转化成 C#版给我看下如何实现啊,非程序员+新手,大家尽量讲的不要那么专业性吧,不然我也看不懂啊

    32 条回复    2017-04-26 14:10:21 +08:00
    xxhjkl
        1
    xxhjkl  
    OP
       2016-05-14 09:13:17 +08:00
    沙发我来占,验证码没考虑到。目测有时候登录需要验证码的
    hahasong
        2
    hahasong  
       2016-05-14 09:29:26 +08:00 via Android
    @cellur kpw3 什么情况
    tscat
        3
    tscat  
       2016-05-14 09:51:17 +08:00 via iPhone
    c#调用 Python 好了。。。
    l0wkey
        4
    l0wkey  
       2016-05-14 10:48:17 +08:00
    Tink
        5
    Tink  
       2016-05-14 10:53:36 +08:00 via iPhone
    我家是可以,能到 100mb
    l0wkey
        6
    l0wkey  
       2016-05-14 10:54:11 +08:00
    联通, 50Mbps ,加速完变成 30Mbps 了 😂
    然后看了下说明,不支持联通 50/100Mbps 的宽带
    bdbai
        7
    bdbai  
       2016-05-14 11:36:35 +08:00 via Android
    C# 有个东西叫 CookieContainer ,每次请求都带上它就可以了。
    xxhjkl
        8
    xxhjkl  
    OP
       2016-05-14 12:00:10 +08:00
    @bdbai 我对比过 python 发的包和我 c#发的包 post 出去的都一样的,应该就是 cookie 没处理好了 据说 python 是自己会处理 cookie 的。
    loading
        9
    loading  
       2016-05-14 12:02:13 +08:00 via Android
    建议抓包
    xxhjkl
        10
    xxhjkl  
    OP
       2016-05-14 12:06:18 +08:00
    @loading 抓包对比过了。我等下把包发上来。 post 出去的和官方的登录包都一样的, c#版的死活登不上
    loading
        11
    loading  
       2016-05-14 12:08:35 +08:00 via Android
    http 是无状态的,发的数据包必然有不同。
    xxhjkl
        12
    xxhjkl  
    OP
       2016-05-14 12:12:30 +08:00
    @loading 你等我晚上发包出来。 昨晚搞到很迟也搞不好 c#
    bdbai
        13
    bdbai  
       2016-05-14 12:15:15 +08:00 via Android
    @xxhjkl Python 这个用了 requests 的 Session ,它会自动处理 Cookie 。你还是直接把 C# 代码发上来吧,用 Gist 或者 Paste Bin 。
    takwai
        14
    takwai  
       2016-05-14 14:19:40 +08:00 via iPhone   ❤️ 1
    用 System.Net.Http 库里面的 HttpClient ,如果服务端响应有返回 Cookies ,下次请求就会自动带上,很方便。
    xxhjkl
        15
    xxhjkl  
    OP
       2016-05-14 22:03:16 +08:00
    ···
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net.Security;
    using System.Security.Cryptography.X509Certificates;
    using System.Net;
    using System.IO;
    using System.IO.Compression;
    using System.Text.RegularExpressions;

    namespace HttpWebRequestDemo
    {
    class Program
    {
    private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)";

    private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
    {
    return true; //总是接受
    }

    public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, Encoding charset)
    {
    HttpWebRequest request = null;
    //HTTPSQ 请求
    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
    request = WebRequest.Create(url) as HttpWebRequest;
    request.ProtocolVersion = HttpVersion.Version10;
    request.Method = "POST";
    request.ContentType = "application/x-www-form-urlencoded";
    request.UserAgent = DefaultUserAgent;
    //如果需要 POST 数据
    if (!(parameters == null || parameters.Count == 0))
    {
    StringBuilder buffer = new StringBuilder();
    int i = 0;
    foreach (string key in parameters.Keys)
    {
    if (i > 0)
    {
    buffer.AppendFormat("&{0}={1}", key, parameters[key]);
    }
    else
    {
    buffer.AppendFormat("{0}={1}", key, parameters[key]);
    }
    i++;
    }
    byte[] data = charset.GetBytes(buffer.ToString());
    using (Stream stream = request.GetRequestStream())
    {
    stream.Write(data, 0, data.Length);
    }
    }
    return request.GetResponse() as HttpWebResponse;
    }

    static void Main(string[] args)
    {
    string url = "https://login.youku.com/user/login_submit";
    string userName = "[email protected]";
    string password = "82925272";
    long epoch = (DateTime.Now.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
    string callback = "userNamelogincallback_" + epoch.ToString();
    Encoding encoding = Encoding.GetEncoding("utf-8");

    IDictionary<string, string> parameters = new Dictionary<string, string>();
    parameters.Add("passport", userName);
    parameters.Add("password", password);
    parameters.Add("captcha", "");
    parameters.Add("remember", "1");
    parameters.Add("callback", callback);
    parameters.Add("from", "http://login.youku.com/@@@@");
    parameters.Add("wintype", "page");
    HttpWebResponse response = Program.CreatePostHttpResponse(url, parameters, encoding);
    //打印返回值
    Stream stream = response.GetResponseStream(); //获取响应的字符串流
    StreamReader sr = new StreamReader(stream); //创建一个 stream 读取流
    string html = sr.ReadToEnd(); //从头读到尾,放到字符串 html
    string cookieString = response.Headers["Set-Cookie"];
    Console.WriteLine(html);
    Console.WriteLine(cookieString);
    }
    }
    }
    ···

    @bdbai
    @takwai
    @loading
    xxhjkl
        16
    xxhjkl  
    OP
       2016-05-14 22:04:17 +08:00
    上面的是测试账号是不是登录成功的。不是会员号 所以没有加速效果的
    luckylion
        18
    luckylion  
       2016-05-25 13:52:27 +08:00
    如果中途换 IP,必须要在优酷页面将"加速服务"关闭后重开,加速才能生效
    xxhjkl
        19
    xxhjkl  
    OP
       2016-05-25 13:56:29 +08:00
    @luckylion 不换 IP 有时候也会失效的,也要那个开关重新开一次
    meteor
        20
    meteor  
       2016-08-08 20:07:16 +08:00
    xxhjkl
        21
    xxhjkl  
    OP
       2016-08-08 20:14:32 +08:00
    @meteor 他是我基友 有问题么
    meteor
        22
    meteor  
       2016-08-08 21:30:19 +08:00
    @xxhjkl 没有。谢谢这个脚本
    meteor
        23
    meteor  
       2016-08-08 21:42:03 +08:00
    正好想要找个优酷加速脚本。
    准备改写成 cURL 或者 node.js 命令行的方式。
    xxhjkl
        24
    xxhjkl  
    OP
       2016-08-08 21:47:56 +08:00
    @meteor curl 的写了记得告诉我一声 这样路由器就能跑了
    meteor
        25
    meteor  
       2016-08-09 12:01:59 +08:00
    @xxhjkl Node.js 版模仿好了。 https://gist.github.com/meteormatt/f2430aebf089523770006362be6b795b
    好多重定向不知道 cURL 怎么弄
    meteor
        26
    meteor  
       2016-08-09 14:49:44 +08:00   ❤️ 1
    Helen
        27
    Helen  
       2016-09-06 21:42:37 +08:00
    @meteor 加速生效只有几个小时,但是失效后一段时间优酷还是显示提速中,得先关闭提速开关再打开再提速才有效
    meteor
        28
    meteor  
       2016-09-06 22:32:56 +08:00
    @Helen 可能是优酷这边的 bug
    phpiis
        29
    phpiis  
       2016-09-28 06:22:53 +08:00
    @meteor 用 php 的 curl 可以写吗
    meteor
        30
    meteor  
       2016-09-28 09:39:29 +08:00
    @phpiis 应该可以。不过我不会 PHP 。。
    xiangbing74
        31
    xiangbing74  
       2017-04-26 14:04:49 +08:00
    更新吗。。不能使用了
    xxhjkl
        32
    xxhjkl  
    OP
       2017-04-26 14:10:21 +08:00
    @xiangbing74 #31 优酷登陆的接口改了,有验证码了。不知道怎么实现
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   6321 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 06:13 · PVG 14:13 · LAX 23:13 · JFK 02:13
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.