我正在尝试将旧的 VB.NET 应用程序移植到 Android 应用程序,但由于我缺乏 Java 经验,我无法找到这个应用程序。我尝试了多种解决方案,但无济于事。
这个想法基本上是向“http://login.vk.com/”发出 POST 请求并获取响应 cookie。如果有人能给我关于如何在 Android 上使用 cookie 的提示,我将非常感激。
原始代码
Public Sub Login(ByVal Username As String, ByVal Password As String)
Try
' Make request
Dim cont As New CookieContainer
Dim request As HttpWebRequest
request = WebRequest.Create("http://login.vk.com/")
request.Method = "POST"
request.CookieContainer = cont
' Create POST content and send
Dim postdata As String = "act=login&success_url=&fail_url=&try_to_login=1&to=&vk=&al_test=3&email=" & HttpUtility.UrlEncode(Username) & "&pass=" & HttpUtility.UrlEncode(Password) & "&expire="
Dim postbytes() As Byte = System.Text.Encoding.UTF8.GetBytes(postdata)
request.ContentType = "application/x-www-form-urlencoded"
request.ContentLength = postbytes.Length
Dim requestStream As Stream = request.GetRequestStream
requestStream.Write(postbytes, 0, postbytes.Length)
requestStream.Close()
' Get response and login cookie
Dim response As HttpWebResponse = request.GetResponse
Dim cookies As CookieCollection = request.CookieContainer.GetCookies(New Uri("http://pirate.vk.com"))
For Each myCookie As Cookie In cookies
If myCookie.Name = "remixsid" Then
Me.Guid = myCookie.Value
End If
Next
response.Close()
' Throw error if cookie not found
If Not IsLoggedIn Then Throw New Exception("Invalid login guid")
Catch ex As Exception
Throw New Exception("Error at custom login", ex)
End Try
End Sub
目前写的代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://login.vk.com/");
try {
List<NameValuePair> postData = new ArrayList<NameValuePair>();
postData.add(new BasicNameValuePair("act", "login"));
postData.add(new BasicNameValuePair("success_url", ""));
postData.add(new BasicNameValuePair("fail_url", ""));
postData.add(new BasicNameValuePair("try_to_login", "1"));
postData.add(new BasicNameValuePair("to", ""));
postData.add(new BasicNameValuePair("vk", ""));
postData.add(new BasicNameValuePair("al_test", ""));
postData.add(new BasicNameValuePair("email", URLEncoder.encode(username, "UTF-8")));
postData.add(new BasicNameValuePair("pass", URLEncoder.encode(password, "UTF-8")));
postData.add(new BasicNameValuePair("expire", ""));
httppost.setEntity(new UrlEncodedFormEntity(postData));
HttpResponse response = httpclient.execute(httppost);
} catch(Exception e) {
}