Skip to content

Instantly share code, notes, and snippets.

@jz5
Created Jun 13, 2015
Embed
What would you like to do?
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment