0

First, I want to connect to the site.
After, I want to get a data from different page with this connection

sample:

1- connect to http://site.com/Login with user pass
2- get a secret data from http://site.com/Secret

How do I do this, pleas help me...

OutputStreamWriter request = null;
    url = new URL("http://site.com/Login"); 
    String response = null; 
    EditText user = (EditText)this.findViewById(R.id.user);
    EditText pass = (EditText)this.findViewById(R.id.pass);
    String parameters ;

    try {
        System.setProperty("http.keepAlive", "true");

        url = new URL("http://site.com/Home/Login"); 
        httppost = (HttpURLConnection) url.openConnection();
        httppost.setDoInput(true);
        httppost.setDoOutput(true);
        httppost.setRequestMethod("POST");
        httppost.setRequestProperty("User-Agent", "Mozilla/5.0 Linux U Android 2.3.3 tr-tr HTC_DesireHD_A9191 Build/GRI40 AppleWebKit/533.1 KHTML, like Gecko Version/4.0 Mobile Safari/533.1");
        httppost.setRequestProperty("Accept_Language", "en-US");
        httppost.setRequestProperty("Connection", "Keep-Alive"); 
        httppost.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        request = new OutputStreamWriter(httppost.getOutputStream());

        parameters = "username="+user.getText()+"&password="+pass.getText();
        request.write(parameters);
        request.flush();
        request.close(); 

        String line = "";               
        InputStreamReader isr = new InputStreamReader(httppost.getInputStream());
        BufferedReader reader = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        response = sb.toString();

        Toast.makeText(this,response, Toast.LENGTH_LONG).show();             

        isr.close();
        reader.close();

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
    }
misima
  • 421
  • 5
  • 17

1 Answers1

0

You should use WebView and sorry but you can not get the webview content.

If you own the server-side code and you want to access from java then you should use an access token for more secure connection and get data by using JSON or XML. Or there is a much more secure connection type which is OAuth2.

If you don't own server, you should show the http://site.com/Login url in webview. When the user logins than you have cookie in your webview. You can use CookieManager http://developer.android.com/reference/android/webkit/CookieManager.html and send the data by using this cookie and get the result. This method is not easy and can differentiate according to server-side implementation.

Yekmer Simsek
  • 4,102
  • 3
  • 21
  • 19
  • öncelikle cevabın çin teşekkür ederim.. ya ben sadece bağlantıyı canlı tutup bir resim çekmeliyim onuda sdcard a kaydedicem zaten, fakat tekrar istek gönderdiğimde bağlantı sıfırlanıyor. nasıl bir yöntem izlemeliyim sence? yardımcı olursan çok sevineceğim şimdiden teşekkürler.... – misima Apr 19 '11 at 20:56
  • Merhaba, ingilizce devam edersek başka insanlarda faydalanır. By using cookie manager class you can get cookie by using getCookie(String url) method. Then you can set this cookie by using your httppost request. you can find a working example in this thread http://stackoverflow.com/questions/678630/how-do-i-make-an-http-request-using-cookies-on-android – Yekmer Simsek Apr 19 '11 at 21:07