为什么我将getRequestProperty("Cookie")
作为空值?如果有可能帮助我。我希望从session
文件中获取php
ID,我认为我应该使用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";
?>
答案 0 :(得分:0)
如果您在服务器的响应中查找cookie,它将不会成为请求属性。您需要查看&#34; Set-Cookie&#34;的响应标头。领域。试试这个:
conn.getHeaderFields().get("Set-Cookie")