VB.NetでMixiボイス 投稿するクラス

ということで、投稿の機能しかないし、元はいろんなとこからかき集めたコードなので公開しますね。
コメントが入ってないのと、そもそも使い方間違ってるかもしれませんので、ご利用は自己責任でw




Imports System.Text
Imports System.Net
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Web

Public Class MixiVoice
Private Class CustomWebClient
Inherits WebClient

Private cookieContainer As CookieContainer = New CookieContainer()

Protected Overrides Function GetWebRequest(ByVal address As Uri) As WebRequest
Dim request = MyBase.GetWebRequest(address)
If (TypeName(request) = "HttpWebRequest") Then
CType(request, HttpWebRequest).CookieContainer = cookieContainer
End If
Return request
End Function
End Class

Private _mixi As CustomWebClient
Private _proxy As System.Net.WebProxy
Private _encoding As System.Text.Encoding
Private _iconurl As String
Private _NickName As String
Private _login As Boolean
ReadOnly Property iconurl() As String
Get
Return _iconurl
End Get
End Property
ReadOnly Property NickName() As String
Get
Return _NickName
End Get
End Property
Sub New()
_encoding = System.Text.Encoding.GetEncoding("euc-jp")
_mixi = New CustomWebClient With {.Encoding = _encoding}
_login = False
End Sub

Function login(ByVal mail As String, ByVal pass As String, Optional ByVal Proxy As String = "", Optional ByVal Proxy_user As String = "", Optional ByVal Proxy_password As String = "") As Boolean
If _login Then
Debug.Print("すでにログインしている")
Return True
End If
'Proxyの設定がある場合

If Proxy <> "" Then
_proxy = New System.Net.WebProxy(Proxy)
If Proxy_user <> "" Then
_proxy.Credentials = New System.Net.NetworkCredential(Proxy_user, Proxy_password)
End If
End If
_mixi.Proxy = _proxy
Dim para As New System.Collections.Specialized.NameValueCollection
para.Add("next_url", "/home.pl")
para.Add("email", mail)
para.Add("password", pass)
_mixi.UploadValues("https://mixi.jp/login.pl", para)

Dim echo = _mixi.DownloadString("https://mixi.jp/recent_voice.pl")
If InStr(echo, "logout loginForm") > 0 Then
Return False
End If
Dim AccountInfo As System.Text.RegularExpressions.Match = Regex.Match(echo, "src=""(http://profile.img.mixi.jp/photo/member/.+?)"" />\s+(.+?)")
If AccountInfo.Success Then
_iconurl = AccountInfo.Groups(1).Value
_NickName = AccountInfo.Groups(2).Value
Else
Return False
End If
_login = True
Return True
End Function
Function SendMessage(ByVal msg As String) As Boolean
If Not _login Then
Debug.Print("ログインされてないお")
Return False
End If

Dim echo = _mixi.DownloadString("https://mixi.jp/recent_voice.pl")
If InStr(echo, "logout loginForm") > 0 Then
Return False
End If


Dim postkey = Regex.Match(echo, "id=""post_key"" value=""(.+?)""").Groups(1).Value

_mixi.Headers(HttpRequestHeader.ContentType) = "application/x-www-form-urlencoded"

_mixi.UploadString("http://mixi.jp/add_voice.pl", String.Join("&", New String() _
{ _
"body=" & HttpUtility.UrlEncode(msg, _encoding), _
"post_key=" & postkey, _
"redirect=recent_echo" _
}))
Return True
End Function
End Class