具有特定网站的android httpClient和httpPost问题

时间:2012-11-21 23:21:30

标签: android cookies http-post httpclient http-get

我正在尝试帮助学生编写一个能够联系特定网络服务器的Android应用程序。从网站的外观来看,您可以使用Web浏览器发出GET请求,然后返回cookie会话和“真实性令牌”(请参阅​​页面来源作为隐形输入)

我们发出GET请求然后想要跟踪帖子,但我们在帖子上收到404的状态代码。另外,第一个GET请求返回的代码为200.

有没有人有任何想法?下面是执行的代码......

public void run()
    {
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("https://co22.herokuapp.com/login/");
        HttpPost httpPost = new HttpPost("https://co22.herokuapp.com/login/sessions");
        HttpResponse response;

        try
        {
            response = httpClient.execute(httpGet);


            Log.d("matt",response.getStatusLine().toString());


            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("session[network_id]", username));
            nameValuePairs.add(new BasicNameValuePair("session[password]", password));
            nameValuePairs.add(new BasicNameValuePair("authenticity_token","9yvxPOUpRFdsTeHAZtISEfBHpElDTHzvMjAbQnxOHDM="));
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


            response = httpClient.execute(httpPost);
            Log.d("matt",response.getStatusLine().toString());

        } 
        catch (ClientProtocolException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }

1 个答案:

答案 0 :(得分:0)

看来上面的内容会正常运行,但我们只是将第二个帖子请求发送到了错误的网站。