locked
C# Youtube HttpWebRequest

    Question

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using Google.GData.Client;
    using Google.GData.Extensions;
    using Google.GData.YouTube;
    using Google.YouTube;
    using Google.GData.Extensions.MediaRss;
    using System.Threading;
    using System.Net;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form4 : Form
        {
            public Form4()
            {
                InitializeComponent();
            }
            public void http()
            {
                HttpWebRequest request = GetNewRequest("http://www.youtube.com/comment_voting?a=-1&id=ElmOl_qgg14TOf2QjsVeF92YakgUMR8CTnKEbN8Rmqg&video_id=XMb63nsvQMo&old_vote=0", cookies);
                Dictionary<string, string> parameters = new Dictionary<string, string>{{"a","1"},
                {"id","ElmOl_qgg157qG8oiwc3Z7uLywO1QxdPc_sZJNYgTWQ"},{"video_id","XMb63nsvQMo"},{"old_vote","0"}};
                HttpWebResponse response = MakeRequest(request, cookies, parameters);
                response.Close();
            }
            public static CookieContainer cookies = new CookieContainer();
    
            public static HttpWebRequest GetNewRequest(string targetUrl, CookieContainer SessionCookieContainer)
            {
                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUrl);
                request.CookieContainer = SessionCookieContainer;
                request.AllowAutoRedirect = false;
                return request;
            }
            public void login()
            {
                HttpWebRequest request = GetNewRequest("https://accounts.google.com/ServiceLoginAuth", cookies);
                request.Referer = "https://accounts.google.com/ServiceLogin?passive=true&continue=https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26feature%3Dsign_in_button%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252F&uilel=3&hl=en_US&service=youtube";
                request.Host = "accounts.google.com";
                Dictionary<string, string> parameters = new Dictionary<string, string>{{"continue","https%3A%2F%2Fwww.youtube.com%2Fsignin%3Faction_handle_signin%3Dtrue%26feature%3Dsign_in_button%26nomobiletemp%3D1%26hl%3Den_US%26next%3D%252F"},
                {"service","youtube"},{"uilel","3"},{"dsh","157212168103955870"},{"hl","en_US"},
                    {"GALX","PTqcwpZb2aE"},{"pstMsg","1"},{"dnConn",""}, {"checkConnection","youtube%3A248%3A1"}, 
                    {"checkedDomains","youtube"}, {"timeStmp",""}, {"secTok",""}, {"Email","username"}, {"Passwd","password"}, 
                    {"signIn","Sign+in"}, {"PersistentCookie","yes"}, {"rmShown","1"}};
                HttpWebResponse response = MakeRequest(request, cookies, parameters);
                response.Close();
            }
    
            public static HttpWebResponse MakeRequest(HttpWebRequest request, CookieContainer SessionCookieContainer, Dictionary<string, string> parameters = null)
            {
    
    
                request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.52 Safari/536.5Accept: */*";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.CookieContainer = SessionCookieContainer;
                request.AllowAutoRedirect = false;
    
    
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                string postData = "";
                foreach (KeyValuePair<String, String> parametro in parameters)
                {
                    if (postData.Length == 0)
                    {
                        postData += String.Format("{0}={1}", parametro.Key, parametro.Value);
                    }
                    else
                    {
                        postData += String.Format("&{0}={1}", parametro.Key, parametro.Value);
                    }
    
                }
                byte[] postBuffer = UTF8Encoding.UTF8.GetBytes(postData);
                using (Stream postStream = request.GetRequestStream())
                {
                    postStream.Write(postBuffer, 0, postBuffer.Length);
                }
    
                HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                SessionCookieContainer.Add(response.Cookies);
    
                while (response.StatusCode == HttpStatusCode.Found)
                {
                    response.Close();
                    request = GetNewRequest(response.Headers["Location"], SessionCookieContainer);
                    response = (HttpWebResponse)request.GetResponse();
                    SessionCookieContainer.Add(response.Cookies);
                }
                return response;
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                login();
                MessageBox.Show("Logged");
                http();
                MessageBox.Show("Sent");
            }
        }
    }
    

    I'm using this code to vote youtube comments. Youtube API is not supporting that voting comments, So i think i can just do it with HttpRequesting. But when i debug this application, it does not vote to specific comment. What is wrong in my code?
    Sunday, September 30, 2012 6:39 AM

All replies

  • Hi Oceanjack,

      Welcome to MSDN Forum Support.

      We're doing research on this issue. It might take some time before we get back to you.

      Sincerely,

      Jason Wang




    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, October 01, 2012 6:18 AM
  • Hi Oceanjack,

     

      Welcome to MSDN Forum Support.

      You are more likely to get more efficient responses to
    Network Class Library (System.Net) issues at http://social.msdn.microsoft.com/Forums/en/ncl/threads where you can contact Network Class Library (System.Net) experts,so I would like to redirect you to appropriate forum for better responses.

     

      Sincerely,

      Jason Wang



    Jason Wang [MSFT]
    MSDN Community Support | Feedback to us

    Monday, October 01, 2012 6:26 AM
  • Is the contents type correct for the cookie request.  You are using an application form.  I would use wireshark to help determine exactly where the failure is occuring.

              request.ContentType = "application/x-www-form-urlencoded";


    jdweng

    Monday, October 01, 2012 11:41 AM