0

Why I get the getRequestProperty("Cookie") as a null value? If it is possible help me. I want get the session id from php file and I think I should use the getRequestProperty("Cookie") method to do this. In the following code I can get the hello value but I can not get the session id from server and the getRequestProperty("Cookie") value is null.

public class MainActivity extends Activity {
    TextView content;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        content    =   (TextView)findViewById( R.id.content );




        Button saveme=(Button)findViewById(R.id.button);

        saveme.setOnClickListener(new Button.OnClickListener(){

            public void onClick(View v)
            {
                try{

                    // CALL GetText method to make post method call
                    GetText();
                }
                catch(Exception ex)
                {
                    content.setText(" url exeption! " );
                }
            }
        });
    }

    // Create GetText Metod
    public  void  GetText()  throws  UnsupportedEncodingException
    {

        String text = "";
        BufferedReader reader=null;
        String s = "";
        // Send data
        try
        {

            // Defined URL  where to send data
            URL url = new URL("http://127.0.0.1:8080/apps/a.php");

            // Send POST data request

            URLConnection conn = url.openConnection();



            // Get the server response

            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            s = conn.getRequestProperty("Cookie");
            StringBuilder sb = new StringBuilder();
            String line = null;

            // Read Server Response
            while((line = reader.readLine()) != null)
            {
                // Append server response in string
                sb.append(line);
            }


            text = sb.toString();
        }
        catch(Exception ex)
        {

        }
        finally
        {
            try
            {

                reader.close();
            }

            catch(Exception ex) {}
        }

        // Show response on activity
        content.setText( text + "\n" + s  );

    }
}

And the PHP file is as follows:

<?php 
session_start();

echo "hello";

 ?>
Naveen Kumar M
  • 7,497
  • 7
  • 60
  • 74
and
  • 341
  • 1
  • 3
  • 11

1 Answers1

0

If you're looking for the cookie in the response from the server, it's not going to be a request property. You'll need to look in the response headers for a "Set-Cookie" field. Try this:

conn.getHeaderFields().get("Set-Cookie")
Ajay
  • 763
  • 5
  • 17