如何通过代理C#查明Tumblr上的用户名是否免费

时间:2015-11-29 16:17:28

标签: c# proxy web-scraping httpwebrequest httpwebresponse

我将给出下面的代码,这里可以回复tumblr:

  • HTTP代码200 =可用
  • HTTP代码403 =可能意味着两件事:

如果响应中包含“登录时出现问题,请稍后重试。”这意味着错误/代理禁令

否则它将返回一些带有用户名建议的json等等==>用户名是

如果它既没有返回,则返回带有临时IP禁止消息的HTML页面。

我从这里获得了一些公共代理http://sslproxies24.blogspot.com。 我手动测试了一些(将它们输入到Mozilla设置中)并且它可以工作。

我目前的代码:

string username = ((ListBoxItem)lvUsernames.Items[i]).Content.ToString(); // current username
string page;
WebRequest requestToGetFormKey = WebRequest.Create("https://www.tumblr.com");
((HttpWebRequest)requestToGetFormKey).UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
WebResponse responseToGetFormKey = requestToGetFormKey.GetResponse();

using (StreamReader stream = new StreamReader(responseToGetFormKey.GetResponseStream()))
{
    page = stream.ReadToEnd();
}

string formKey = page.Substring(page.IndexOf("tumblr-form-key") + 26, page.IndexOf('\"', page.IndexOf("tumblr-form-key") + 26) - (page.IndexOf("tumblr-form-key") + 26));

WebRequest requestToGetUsername = WebRequest.Create("https://www.tumblr.com/svc/account/register");

string postData = "user%5Bemail%5D=user2%40gmail.com&user%5Bpassword%5D='.MyPasswordQWERTY12345.'&tumblelog%5Bname%5D=" + username + "&user%5Bage%5D=&context=no_referer&version=STANDARD&follow=&form_key=" + formKey + "&seen_suggestion=1&used_suggestion=0&used_auto_suggestion=0&about_tumblr_slide=&random_username_suggestions=%5B%22FancyDragonTrash%22%2C%22GhostlyCollectorAnchor%22%2C%22DistinguishedGentlemenCshopshop%22%2C%22ScrumptiouslyProfousndStarlight%22%2C%22UniquefStudentPuppy%22%5D&action=signup_account&tracking_url=%2F&tracking_version=modal";

var data = Encoding.ASCII.GetBytes(postData);
string proxy = ((ListBoxItem)lvProxies.Items[proxyCounter]).Content.ToString(); // current proxy

if (proxy != "noproxy") // "noproxy" is just for testing
{
    //first variant
    WebProxy wb = new WebProxy(proxy, true);
    requestToGetUsername.Proxy = wb;

    //second variant
    //requestToGetUsername.Proxy = new WebProxy(proxy.Substring(0, proxy.IndexOf(':')), Convert.ToInt32(proxy.Substring(proxy.IndexOf(':') + 1, proxy.Length - proxy.IndexOf(':') - 1)));
}

requestToGetUsername.Method = "POST";
requestToGetUsername.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
requestToGetUsername.ContentLength = data.Length;

try
{
    using (var stream = requestToGetUsername.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }
}
catch
{
//sometimes this variant appears, but in Mozilla it responds
    lvErrors.Items.Add(new ListBoxItem() { Content = "Proxy: " + proxy + " doesn't respond." });
    continue;
}

try
{
    WebResponse responseToGetUsername = requestToGetUsername.GetResponse();
    lvAvailable.Items.Add(new ListBoxItem() { Content = "Proxy: " + proxy + " | " + "Username: " + username });
}
catch (WebException ex)
{
    if (((int)((HttpWebResponse)ex.Response).StatusCode) == 403)
    {
        string responseString = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();

        if (responseString.IndexOf("There was a problem logging in, try again later") == -1)
            lvTaken.Items.Add(new ListBoxItem() { Content = "Proxy: " + proxy + " | " + "Username: " + username });
        else
            // sometimes this variant appears, but in Mozilla it isn't banned
            lvErrors.Items.Add(new ListBoxItem() { Content = "Proxy: " + proxy + " is banned." });
    }
    else
        lvErrors.Items.Add(new ListBoxItem() { Content = "Proxy: " + proxy + " is banned." });
}

我不明白为什么单个代理在浏览Mozilla和C#应用程序时以两种不同的方式工作。

谢谢!

0 个答案:

没有答案