1

古い 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) {

            }
4

2 に答える 2

1

応答としてCookieを取得するには、次の方法を試してください。

private static HttpParams params;
params = new BasicHttpParams();
HttpClientParams.setRedirecting(params, false);
HttpClientParams.setCookiePolicy(params, CookiePolicy.BROWSER_COMPATIBILITY);
HttpClient httpclient = new DefaultHttpClient(params);

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);
                List<Cookie> cookies = ((DefaultHttpClient)httpclient).getCookieStore().getCookies();
                for(Cookie cookie : cookies){
                Log.i("Cookie", cookie.toString());
             }

            } catch(Exception e) {

            }
于 2012-05-24T02:31:28.083 に答える
0

これがあなたに役立つことを願っています:

    HttpURLConnection urlConn = null;
//edit String data as what you want
    String data = "act=login&success_url=&fail_url=&try_to_login=1&to=&vk=&al_test=3&email=" & URLEncoder.encode(username, "UTF-8")) & "&pass=" & URLEncoder.encode(password, "UTF-8")) & "&expire="
        URL mUrl;
        StringBuffer response = new StringBuffer(); 
        try {               
            mUrl = new URL("http://login.vk.com/");
            urlConn = (HttpURLConnection) mUrl.openConnection();
            urlConn.setRequestMethod("POST");
            //query is your body
            urlConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");            
            urlConn.setRequestProperty("Content-Length", Integer.toString(data.length()));
            urlConn.setUseCaches (false);
            urlConn.setDoInput(true);
            urlConn.setDoOutput(true);

            //send request
            DataOutputStream wr = new DataOutputStream (
                    urlConn.getOutputStream ());
            wr.write(data.getBytes("UTF8"));
            wr.flush ();
            wr.close ();

            //Get Response  
            InputStream is = urlConn.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;            
            while((line = rd.readLine()) != null) {
              response.append(line);
              response.append('\r');
            }
            rd.close();
            String result = response.toString();
            return result;
        } catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {

              if(urlConn != null) {
                  urlConn.disconnect(); 
              }
        }
于 2012-05-24T02:01:04.310 に答える