不知何故,我相信我没有正确执行我的 HTTP 请求。
本站:
https://www.bintube.com/login/
有 2 个带有名称的 html 输入字段:
ctl00$main$Login1$UserName and ctl00$main$Login1$Password
提交按钮的 HTML 代码是:
<input type="submit" name="ctl00$main$Login1$LoginButton" value="Login" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$main$Login1$LoginButton", "", true, "Login1", "", false, false))" id="ctl00_main_Login1_LoginButton" />
奇怪的是,它不包含在表单标签中,不幸的是 onclick 对我来说是一个黑盒子。
所以为了得到正确的 GET 请求参数,我必须使用 Fiddler,因为我不知道 GET URL 参数(时间戳和哈希)是如何生成的。在提琴手中看起来像这样:
http://support.bintube.com/access/remote/?name=myname&email=myemail%40gmail.com&external_id=myexternal_id&organization=BinTube.com×tamp=1353061816&hash=2372492438924389243
但是,此 URL 与前面提到的 URL https://www.bintube.com/login/ (SSL)完全不同。所以我不知道我是否错过了任何东西,直到它跳转到http://support.bintube.com/(这是 Fiddler 向我展示的),而且,我总是必须使用 Fiddler 给我 C# 代码的 URL ,以便使用我的代码登录。理想情况下,我的代码会自行解决所有问题,因此我不必使用 Fiddler。
我的代码是:
var cc = new CookieContainer();
List<string> paras = new List<string>();
string url;
string html;
// data copied from Fiddler, (changed some of it of course):
paras.Add(String.Format("{0}={1}", "name", HttpUtility.UrlEncode("myname")));
paras.Add(String.Format("{0}={1}", "email", HttpUtility.UrlEncode("myemail")));
paras.Add(String.Format("{0}={1}", "external_id", HttpUtility.UrlEncode("myexternal_id")));
paras.Add(String.Format("{0}={1}", "organization", HttpUtility.UrlEncode("BinTube.com")));
paras.Add(String.Format("{0}={1}", "timestamp", HttpUtility.UrlEncode("1353061816")));
paras.Add(String.Format("{0}={1}", "hash", HttpUtility.UrlEncode("2372492438924389243")));
url = "http://support.bintube.com/access/remote/" + "?" + paras.Aggregate((x, y) => x + "&" + y);
var req = (HttpWebRequest)WebRequest.Create(url);
Console.WriteLine(url);
req.CookieContainer = cc;
req.Method = "GET";
req.AllowAutoRedirect = true;
// also copied from Fiddler
req.Host = "support.bintube.com";
req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11";
req.KeepAlive = true;
req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
//req.Headers["Accept-Encoding"] = "gzip,deflate,sdch"; //<- this give gibberish response, why?
req.Headers["Accept-Language"] = "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4";
req.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.3";
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
cc.Add(resp.Cookies);
//rewrite cookies for further requests (found that somewhere, but did not help)
string ckheader = cc.GetCookieHeader(req.RequestUri);
cc.SetCookies(new Uri("http://www.bintube.com/"), ckheader);
StreamReader r = new StreamReader(resp.GetResponseStream());
html = r.ReadToEnd();
该代码有效。有时它停止工作,然后我必须从 Fiddler 获取新的 GET 参数,这是丑陋的设计,我对此一点也不满意。第二个更大的问题是我不能使用
https://www.bintube.com/search/ (requires login for results)
和我收到的饼干。收到的 cookie(来自第一个请求)是 _zendesk_session 和 _zendesk_shared_session cookie,没什么重要的(谷歌搜索)。所以不知何故,我相信我没有得到正确的饼干来进一步发展。
也许有人对我为什么会失败这么严重有一些提示?