C#http(s)GET请求问题

时间:2012-11-16 12:11:25

标签: c# http https get request

不知怎的,我相信我没有正确地处理我的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(&quot;ctl00$main$Login1$LoginButton&quot;, &quot;&quot;, true, &quot;Login1&quot;, &quot;&quot;, false, false))" id="ctl00_main_Login1_LoginButton" />

奇怪的是,它不包含在表单标签中,不幸的是onclick的东西对我来说是一个黑盒子。

因此,为了获得GET请求的正确参数,我必须使用Fiddler,因为我不知道如何生成GET URL参数(时间戳和哈希)。它在fiddler中看起来像这样:

http://support.bintube.com/access/remote/?name=myname&email=myemail%40gmail.com&external_id=myexternal_id&organization=BinTube.com&timestamp=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 cookies,没什么重要的(谷歌搜索)。所以我不相信我没有得到正确的cookie来进一步推进。

也许某人对我为什么失败如此严重有一些暗示?

0 个答案:

没有答案