[VB/C#] niconico にログイン
コードから niconico にメールアドレスとパスワードでログインします。
下記コードの GetUserSession でログインし、必要な Cookie を取得します。ログイン後、例としてマイページの HTML を取得しています。
System.Net, System.IO を import してください。
Visual Basic (VB.NET)
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
Sub Main() | |
' ログイン(Cookie の取得) | |
Dim session = GetUserSession("メールアドレス", "パスワード") | |
' マイページ取得(取得した Cookie を付けてアクセス) | |
Dim url = "http://www.nicovideo.jp/my/top" | |
Dim req = WebRequest.CreateHttp(New Uri(url)) | |
req.CookieContainer = New CookieContainer | |
req.CookieContainer.Add(session) | |
Dim html As String | |
Using res = req.GetResponse | |
Using rs = res.GetResponseStream | |
Using sr = New StreamReader(rs) | |
html = sr.ReadToEnd | |
End Using | |
End Using | |
End Using | |
End Sub | |
Private Function GetUserSession(ByVal email As String, ByVal password As String) As Cookie | |
Const path As String = "https://secure.nicovideo.jp/secure/login?site=niconico" | |
Dim req = WebRequest.CreateHttp(New Uri(path)) | |
Dim param = String.Format("mail={0}&password={1}", Uri.EscapeDataString(email), Uri.EscapeDataString(password)) | |
Dim 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 rs = req.GetRequestStream() | |
rs.Write(buf, 0, buf.Length) | |
End Using | |
Dim html As String | |
Using res = req.GetResponse | |
Using rs = res.GetResponseStream | |
Using sr = New StreamReader(rs) | |
html = sr.ReadToEnd | |
End Using | |
End Using | |
Dim cookies = req.CookieContainer.GetCookies(New Uri(path)) | |
Return cookies("user_session") ' MEMO: 無い場合は Nothing | |
End Using | |
End Function |
C#
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 | |
} | |
} |