简体   繁体   English

将curl命令转换为Java / Android

[英]Translating curl commands into Java/Android

Can someone help me figure out how to translate the following curl commands into Java syntax for use in an Android application? 有人可以帮我弄清楚如何将以下curl命令转换为Java语法以用于Android应用程序吗?

The curl commands are: 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

Specifically the storing and sending of cookies. 特别是cookie的存储和发送。 The rest I can manage, but I'm not sure how to store the initial GET response's cookie, then send it with the subsequent POST, then send that response's cookie with the final POST. 其余的我可以管理,但是我不确定如何存储初始GET响应的cookie,然后在后续的POST中发送它,然后在最终的POST中发送该响应的cookie。

Thanks 谢谢

This is almost working...when I start an activity through a new intent, the cookies are immediately wiped. 这几乎可以正常工作...当我通过新的意图开始活动时,cookie会立即被擦除。 I'll edit this answer once I have that figured out for a more complete solution, but the following does generate the desired authentication 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(); } 

Edit: 编辑:

I created an application class that extends Application and defined my DefaultHttpClient there, then in all my other activities, I use that client for requests. 我创建了一个扩展应用程序的应用程序类,并在其中定义了我的DefaultHttpClient,然后在所有其他活动中,我都使用该客户端进行请求。 This allows cookies to be shared across all activities in my application. 这允许cookie在我的应用程序中的所有活动之间共享。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM