0

为什么我得到getRequestProperty("Cookie")一个空值?如果可能的话帮助我。我想session从文件中获取 id php,我认为我应该使用该getRequestProperty("Cookie")方法来执行此操作。在下面的代码中,我可以获取hello值,但无法session从服务器获取 id 并且getRequestProperty("Cookie")值为 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  );

    }
}

文件如下PHP

<?php 
session_start();

echo "hello";

 ?>
4

1 回答 1

0

如果您在服务器的响应中查找 cookie,它不会是请求属性。您需要在响应标头中查找“Set-Cookie”字段。尝试这个:

conn.getHeaderFields().get("Set-Cookie")
于 2015-09-22T14:15:22.830 回答