C#和json请求pinterest

时间:2013-06-28 17:20:36

标签: c# xml json http pinterest

嘿伙计们我是c#的新手并且正在学习它,我已经学习了webrequest和其他基础来模拟基本的帖子请求。为了让我的生活变得轻松,我正在使用chilkatsoft

现在我的问题是,如何发送强制服务器只请求xml或html或xhtml的标头。我不希望Json从服务器响应。

实际上我正在尝试自动化简单帐户创建过程,这样我就可以了解更多信息,而我选择的网站是最好的。第一次生活很简单,因为帖子请求以这种方式包含数据http://i.imgur.com/PNYdQ5h.png但在此之后我的意思是注册几个(2-3)帐户它开始抛出json http://i.imgur.com/A2N4qa8.png所以我想知道如何只接受一个响应“text / html,application / xhtml + xml,application / xml” 这是我的代码

private void button4_Click(object sender, EventArgs e)
{
            Chilkat.HttpRequest req = new Chilkat.HttpRequest();
            Chilkat.Http http = new Chilkat.Http();
            List<string> fmane = new List<string>();
            List<string> lmane = new List<string>();
            List<string> mail = new List<string>();
            fmane  = File.ReadAllLines("firstname.txt").ToList();
            lmane = File.ReadAllLines("lastname.txt").ToList();
            mail = File.ReadAllLines("mail.txt").ToList();
            Random r = new Random();
            bool success;
            success = http.UnlockComponent("Anything for 30-day trial");
            if (success != true)
            {
                MessageBox.Show(http.LastErrorText);
                return;
            }
            http.CookieDir = "memory";
            http.SendCookies = true;
            http.SaveCookies = true;
            string html;
            http.Accept = "text/html,application/xhtml+xml,application/xml;";

            html = http.QuickGetStr("https://pinterest.com/join/register_new/");
            if (html == null)
            {
                textBox1.Text += http.LastErrorText + "\r\n";
                textBox1.Refresh();
                return;
            }

            req.UsePost();
            req.Path = "/join/register_new/";
            string fnamee = fmane[r.Next(1, fmane.Count())].ToString() + "_" + r.Next(1, 100000).ToString();
            req.AddParam("first_name", fnamee);
            req.AddParam("last_name", lmane[r.Next(1, lmane.Count)]);
            req.AddParam("email", fnamee + "_" + r.Next(1, 100000).ToString() + "@" + mail[r.Next(1, mail.Count)]);
            req.AddParam("username", fnamee + r.Next(1, 1000000) + r.Next(22222, 44444444));
            req.AddParam("password", "aki123");
            req.AddParam("gender", "male");
            req.AddParam("country", "IN");
            req.AddParam("user_image_url", "https://s-passets-ec.pinimg.com/images/registration/default-profile.png");

                success = req.AddFileForUpload("img", "944624_3075815911741_951085019_n.jpg");
                if (success != true)
                {
                    MessageBox.Show(req.LastErrorText);
                    return;
                }
                else
                {
                    MessageBox.Show("image upload success");
                }

            req.AddParam("csrfmiddlewaretoken", Regex.Match(html, "name='csrfmiddlewaretoken' value='(.*?)'").Groups[1].Value);
            req.AddParam("_ch", Regex.Match(html, "name='_ch' value='(.*?)'").Groups[1].Value);
            req.AddParam("registration_type", "email");
            req.HttpVersion = "HTTP/1.1";
            req.AddHeader("Accept", "text/html,application/xhtml+xml,application/xml");
            req.AddHeader("Accept-Encoding", "gzip, deflate");
            req.AddHeader("Accept-Language", "en-US,en;q=0.5");
            req.AddHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0");
            req.AddHeader("Referer", "https://pinterest.com/join/signup");
            string domain;
            int port;
            bool ssl;
            domain = "https://pinterest.com";
            port = 443;
            ssl = true;
            Chilkat.HttpResponse resp;
            resp = http.SynchronousRequest(domain, port, ssl, req);
            if (resp.BodyStr.Contains("CSRF verification failed. Request aborted."))
            {
                textBox1.Text += http.LastErrorText + "\r\n";
                textBox1.Refresh();
                MessageBox.Show("error csrf verification failed");
                return;
            }

            if (resp == null)
            {
                textBox1.Text += http.LastErrorText + "\r\n";
                textBox1.Refresh();
                return;
            }

            //  Is this a 302 redirect?
            if (resp.StatusCode == 302)
            {


                string redirectUrl;
                redirectUrl = resp.GetHeaderField("Location");

                html = http.QuickGetStr(redirectUrl);
                if (html == null)
                {
                    textBox1.Text += http.LastErrorText + "\r\n";
                    textBox1.Refresh();
                }
                else
                {
                    textBox1.Text += html + "\r\n";
                    textBox1.Refresh();
                }

            }
            else
            {
                textBox1.Text += resp.BodyStr + "\r\n";
                textBox1.Refresh();
            }
}

另外,我也面临着一个奇怪的错误

  

CSRF验证失败。请求中止。

尝试学习新东西,我尝试使用谷歌搜索,但找不到这样的东西。 请建议我像chilkatsoft这样可以让它变得更容易的好dll。

谢谢你们:)

0 个答案:

没有答案