Last active
August 29, 2015 14:23
-
-
Save jz5/e3e760c9e035090ece4f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void Main(string[] args) | |
{ | |
// ログイン(Cookie の取得) | |
var session = GetUserSession("メールアドレス", "パスワード"); | |
// マイページ取得(取得した Cookie を付けてアクセス) | |
var url = "http://www.nicovideo.jp/my/top"; | |
var req = WebRequest.CreateHttp(new Uri(url)); | |
req.CookieContainer = new CookieContainer(); | |
req.CookieContainer.Add(session); | |
string html = null; | |
using (var res = req.GetResponse()) | |
{ | |
using (var rs = res.GetResponseStream()) | |
{ | |
using (var sr = new StreamReader(rs)) | |
{ | |
html = sr.ReadToEnd(); | |
} | |
} | |
} | |
} | |
private static Cookie GetUserSession(string email, string password) | |
{ | |
const string path = "https://secure.nicovideo.jp/secure/login?site=niconico"; | |
var req = WebRequest.CreateHttp(new Uri(path)); | |
var param = string.Format("mail={0}&password={1}", Uri.EscapeDataString(email), Uri.EscapeDataString(password)); | |
var buf = System.Text.Encoding.UTF8.GetBytes(param); | |
req.Method = "POST"; | |
req.ContentType = "application/x-www-form-urlencoded"; | |
req.ContentLength = buf.Length; | |
req.Referer = "https://secure.nicovideo.jp/secure/login_form"; | |
req.CookieContainer = new CookieContainer(); | |
using (var rs = req.GetRequestStream()) | |
{ | |
rs.Write(buf, 0, buf.Length); | |
} | |
string html = null; | |
using (var res = req.GetResponse()) | |
{ | |
using (var rs = res.GetResponseStream()) | |
{ | |
using (var sr = new StreamReader(rs)) | |
{ | |
html = sr.ReadToEnd(); | |
} | |
} | |
var cookies = req.CookieContainer.GetCookies(new Uri(path)); | |
return cookies["user_session"]; // MEMO: 無い場合は null | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment