将curl命令转换为Java / Android

时间:2011-03-13 05:38:55

标签: java android curl

有人可以帮我弄清楚如何将以下curl命令转换为Java语法,以便在Android应用程序中使用吗?

curl命令是:

curl -L -c cookie1 URL

curl -L -c cookie2 -b cookie1 -d "loginId=NAME&password=PASSWORD&_eventId_submit=Submit" URL2

curl -L -c cookie3 -b cookie2 -d "_eventId_submit=Submit" URL3

特别是存储和发送cookie。其余的我可以管理,但我不知道如何存储初始GET响应的cookie,然后用随后的POST发送它,然后用最终的POST发送该响应的cookie。

由于

1 个答案:

答案 0 :(得分:1)

这几乎正常......当我通过新意图开始活动时,会立即擦除cookie。一旦我找到了更完整的解决方案,我就会编辑这个答案,但以下内容确实生成了所需的身份验证cookie。

try {

        HttpGet httpget = new HttpGet("url1");
        client.getCookieStore().getCookies();
        HttpResponse response1 = client.execute(httpget);
        HttpEntity entity1 = response1.getEntity();
        if (entity1 != null){
            entity1.consumeContent();
        }
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
                cookie = cookies.get(i);
            }
        }


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

    try {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("loginId", username.toString()));
        nvps.add(new BasicNameValuePair("password", password.toString()));
        nvps.add(new BasicNameValuePair("_eventId_submit", "Submit"));

        HttpPost httppost = new HttpPost("url2");
        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        HttpParams params = httppost.getParams();
        HttpConnectionParams.setConnectionTimeout(params, 45000);
        HttpConnectionParams.setSoTimeout(params, 45000);

        // Perform the HTTP POST request
        HttpResponse response = client.execute(httppost);
        status = response.getStatusLine().toString();
        if (!status.contains("OK")) {
            throw new HttpException(status);
        }

        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
                cookie = cookies.get(i);
            }
        }


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

    try {
        List<NameValuePair> nvps2 = new ArrayList<NameValuePair>();

        nvps2.add(new BasicNameValuePair("_eventId_submit", "Submit"));

        HttpPost httppost2 = new HttpPost("url3");
        httppost2.setEntity(new UrlEncodedFormEntity(nvps2, HTTP.UTF_8));
        HttpParams params2 = httppost2.getParams();
        HttpConnectionParams.setConnectionTimeout(params2, 45000);
        HttpConnectionParams.setSoTimeout(params2, 45000);

        // Perform the HTTP POST request
        HttpResponse response2 = client.execute(httppost2);
        if (cookies.isEmpty()) {
            System.out.println("None");
        } else {
            for (int i = 0; i < cookies.size(); i++) {
                System.out.println("- " + cookies.get(i).toString());
                cookie = cookies.get(i);
                settings.setASCookie(cookie);
            }
        }

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

HttpGet httpget = new HttpGet("url1"); client.getCookieStore().getCookies(); HttpResponse response1 = client.execute(httpget); HttpEntity entity1 = response1.getEntity(); if (entity1 != null){ entity1.consumeContent(); } if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); cookie = cookies.get(i); } } } catch (ClientProtocolException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("loginId", username.toString())); nvps.add(new BasicNameValuePair("password", password.toString())); nvps.add(new BasicNameValuePair("_eventId_submit", "Submit")); HttpPost httppost = new HttpPost("url2"); httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); HttpParams params = httppost.getParams(); HttpConnectionParams.setConnectionTimeout(params, 45000); HttpConnectionParams.setSoTimeout(params, 45000); // Perform the HTTP POST request HttpResponse response = client.execute(httppost); status = response.getStatusLine().toString(); if (!status.contains("OK")) { throw new HttpException(status); } if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); cookie = cookies.get(i); } } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (HttpException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { List<NameValuePair> nvps2 = new ArrayList<NameValuePair>(); nvps2.add(new BasicNameValuePair("_eventId_submit", "Submit")); HttpPost httppost2 = new HttpPost("url3"); httppost2.setEntity(new UrlEncodedFormEntity(nvps2, HTTP.UTF_8)); HttpParams params2 = httppost2.getParams(); HttpConnectionParams.setConnectionTimeout(params2, 45000); HttpConnectionParams.setSoTimeout(params2, 45000); // Perform the HTTP POST request HttpResponse response2 = client.execute(httppost2); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); cookie = cookies.get(i); settings.setASCookie(cookie); } } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }

编辑:

我创建了一个扩展Application的应用程序类,并在那里定义了我的DefaultHttpClient,然后在我的所有其他活动中,我使用该客户端进行请求。这允许在我的应用程序中的所有活动之间共享cookie。