Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > .NET для новичков > Загрузка изображений vk


Автор: dj100500 26.6.2012, 02:13
Пытаюсь загрузить изображение в альбом,  пол ночи уже сижу со снифером, но ничего не выходит хотя все делаю правильно вроде, подскажите в чем проблема пожалуйста.
Код

using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using EazySharp.Net;
namespace ScreenShotMe
{
    class Vk
    {
        private string cookies;
        private const string useragent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.120 Safari/535.2";
        public bool IsAuhtorized { get; private set; }
        public bool Init(string login, string pass)
        {
            HttpWebRequest req = CreateHttpWebRequest("http://m.vk.com/login");
            string page = string.Empty;
            using(HttpWebResponse rsp = (HttpWebResponse)req.GetResponse())
            {
                page = ReadResponse(rsp);
                int index = page.IndexOf("https://login.vk.com/?act=login&to=&from_host=m.vk.com&from_protocol=http&ip_h=");
                if (index == -1)
                    return false;
                int end_index = page.IndexOf('\"', index);
                string uri = page.Substring(index, end_index - index);
                req = CreateHttpWebRequest(uri);
            }
            req.Referer = "http://m.vk.com/login";
            req.ContentType = "application/x-www-form-urlencoded";
            req.Method = "POST";
            PostData pd = new PostData();
            pd["email"] = login;
            pd["pass"] = pass;
            req.AllowAutoRedirect = false;
            using(StreamWriter writer = new StreamWriter(req.GetRequestStream()))
            {
                writer.Write(pd.QueryString);
            }
            using (HttpWebResponse rsp = (HttpWebResponse)req.GetResponse())
            {
                req = CreateHttpWebRequest(rsp.Headers[HttpResponseHeader.Location]);
                req.Headers.Add(HttpRequestHeader.Cookie, "remixlang=0; remixdt=0; audio_vol=100; remixmdevice=1366/768/1/!!-!; remixchk=5; remixseenads=0; remixflash=11.1.102");
                req.Referer = "http://m.vk.com/login";
                req.AllowAutoRedirect = false;
            }
            using (HttpWebResponse rsp = (HttpWebResponse)req.GetResponse())
            {
                cookies = rsp.Headers[HttpResponseHeader.SetCookie];
                cookies = cookies.Substring(0, cookies.IndexOf(';') + 1) + " remixchk=5;";
            }
            IsAuhtorized = true;
            return true;
        }
        
        private string ReadResponse(WebResponse rsp)
        {
            using (StreamReader reader = new StreamReader(rsp.GetResponseStream()))//, Encoding.GetEncoding(1251)))
            {
                return reader.ReadToEnd();
            }
        }
        private HttpWebRequest CreateHttpWebRequest(string uri)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
            if (IsAuhtorized)
                req.Headers.Add(HttpRequestHeader.Cookie, cookies);
            req.UserAgent = useragent;
            req.AllowAutoRedirect = false;
            req.ServicePoint.Expect100Continue = false;
            req.Headers.Add("Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.3");
            req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            return req;
        }
        public bool UploadImage()
        {
            if(!IsAuhtorized)
                return false;
            string url = string.Empty;
            HttpWebRequest req = CreateHttpWebRequest("http://m.vk.com/album162801392_157181421?act=add");
            string page = ReadResponse(req.GetResponse());
            int index = page.IndexOf("action=\"")+8;
            int end_index = page.IndexOf('\"', index);
            url = page.Substring(index, end_index - index);
            req = CreateHttpWebRequest(url);
            req.Method = "POST";
            req.Referer = "http://m.vk.com/album162801392_157181421?act=add";
            req.ContentType = "multipart/form-data; boundary=----WebKitFormBoundary2yzl1KCZbcLbBdCR";
            using(StreamWriter writer = new StreamWriter(req.GetRequestStream()))
            {
                writer.Write("----WebKitFormBoundary2yzl1KCZbcLbBdCR\r\n");
                writer.Write("Content-Disposition: form-data; name=\"file1\"; filename=\"a.jpeg\"\r\n");
                writer.Write("Content-Type: image/jpeg\r\n");
                writer.Write(File.ReadAllText("a.jpeg"));
                writer.Write("\r\n----WebKitFormBoundary2yzl1KCZbcLbBdCR\r\n");
                writer.Write("Content-Disposition: form-data; name=\"file2\"; filename=\"\"\r\n");
                writer.Write("----WebKitFormBoundary2yzl1KCZbcLbBdCR\r\n");
                writer.Write("Content-Disposition: form-data; name=\"file3\"; filename=\"\"\r\n");
                writer.Write("----WebKitFormBoundary2yzl1KCZbcLbBdCR\r\n");
            }
            using (HttpWebResponse rsp = (HttpWebResponse)req.GetResponse())
            {
                req = CreateHttpWebRequest(rsp.Headers[HttpResponseHeader.Location]);
            }
            req.Referer = "http://m.vk.com/МОЙ_АЛЬБОМ?act=add";
            req = CreateHttpWebRequest("http://m.vk.com" + req.GetResponse().Headers[HttpResponseHeader.Location]);
            req.GetResponse();
            return true;
        }
    }
}


Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)