将 bash 脚本移植到 java

发布于 2024-10-21 09:57:34 字数 752 浏览 0 评论 0原文

我找到了以下 bash 脚本,我想在 Android 应用程序中使用它。尽管花了一段时间开发应用程序,但我几乎没有 bash/http 经验,并且不知道从哪里开始。

例如,登录功能如下所示:

curl \
    --location \
    --cookie "$COOKIES" \
    --cookie-jar "$COOKIES" \
    --output /dev/null \
    "http://connect.garmin.com/signin" && \
  curl \
    --location \
    --cookie "$COOKIES" \
    --cookie-jar "$COOKIES" \
    --data "login=login&login%3AloginUsernameField=$USER&login%3Apassword=$PASSWORD&login%3AsignInButton=Sign+In&javax.faces.ViewState=j_id1" \
    --output - \
    "https://connect.garmin.com/signin" |\

curl 如何对应 Android 的 HttpClient 命令等?

代码来自这里: http://braiden.org/?p=62#more-62

I have found the following bash script that I would like to use in an Android app. Despite spending a while developing apps I've got little to no bash/http experience and don't know where to start.

e.g. The login function looks like:

curl \
    --location \
    --cookie "$COOKIES" \
    --cookie-jar "$COOKIES" \
    --output /dev/null \
    "http://connect.garmin.com/signin" && \
  curl \
    --location \
    --cookie "$COOKIES" \
    --cookie-jar "$COOKIES" \
    --data "login=login&login%3AloginUsernameField=$USER&login%3Apassword=$PASSWORD&login%3AsignInButton=Sign+In&javax.faces.ViewState=j_id1" \
    --output - \
    "https://connect.garmin.com/signin" |\

how does curl correspond to Android's HttpClient commands etc?

The code is from here: http://braiden.org/?p=62#more-62

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫瑾少女 2024-10-28 09:57:34

Curl 是一个允许您(通过命令行)下载 http 和 https 请求的程序无需通过浏览器。它类似于 wget 命令。

同时,Android 的 HttpClient 不是一个命令,甚至不是一个函数。它是一个基于类的 API,允许您执行 http 请求。

我建议您浏览 Curl 手册页(我链接到的)并查看如果您可以看到传递给 Curl 的各种参数的作用。此命令会两次点击相同的 URL “http://connect.garmin.com/signin”。第一次它只是检查它是否可以访问。如果是,它将运行第二个 curl 命令。以下是各个参数的含义:

  • --位置:如果网页已移动到不同的 URL,则会尝试使用新 URL 再次运行curl 命令。
  • --cookie:cookie用于跟踪用户请求。它允许服务器知道同一个人正在再次访问。每个 URL 请求通常都是单独且独立的,因此 http 使用所谓的会话 cookie 来跟踪用户访问网站的情况。通常,参数以 key=value 形式给出,这就是要使用的 cookie。
  • --cookie-jar:这是服务器可以在本地系统上存储会话 cookie 的文件。这允许服务器获取和检索会话信息。
  • --输出:这是输出从服务器检索到的网页的位置。 /dev/null 是一个你可以扔掉你不想要的东西的地方。 - 表示将网页打印到屏幕上。

基本上,第一个 curl 命令是点击 Garmin 登录页面 (http://connect.garmin.com/signin)。它正在丢弃数据。这样做只是为了查看网页是否真正启动。

第二个 curl 命令正在登录。--data 行是发送到网页的数据。它假装您已填写登录页面上的登录表单,并向 Garmin 发送您的用户名和密码。该页面的输出作为 http 网页输出发送到终端(我打赌正在解析您想要的信息)。

您说您已经成为 Android 开发人员一段时间了,所以我假设您了解 Android 编程并了解 Android API 的工作原理。我不是 Android 开发人员,但我查看了 AndroidHttpClient API 文档 而且它看起来并没有那么复杂。基本上,您需要创建一个可以向 http://connect.garmin.com/signin 发送请求的对象来验证它是否已启动并正在运行,如果是,则向https://connect.garmin.com/signin 登录。您发送的数据位于 --data 参数中。

Curl is a program that allows you (via the command line) to download http and https requests without going through a browser. It is similar to the wget command.

Meanwhile, Android's HttpClient isn't a command or even a function. It is a class-based API that allows you to do http requests.

I would recommend that you go through the Curl manual page (which I linked to) and see if you can see what the various parameters passed to Curl do. This command is hitting the same URL "http://connect.garmin.com/signin" twice. The first time it merely is checking to see if it is accessible. If it is, it'll run the second curl command. Here are what the various parameters mean:

  • --location: If the webpage has moved to a different URL, this will try to run the curl command again using the new URL.
  • --cookie: A cookie is used to track user requests. It allows the server to know that the same person is visiting once more. Each URL request is normally separate and independent, so http uses what are called session cookies to track the user as they visit the website. Normally, the parameter is given as key=value, and that's the cookie to use.
  • --cookie-jar: This is the file where the server can store session cookies on your local system. This allows the server to get and retrieve session information.
  • --output: This is where to output the webpage retrieved from the server. /dev/null is a place where you can toss things you don't want out. The - means to print the webpage to the screen.

Basically, the first curl command is hitting the Garmin login page (http://connect.garmin.com/signin). It is discarding the data. This is done just to see if the webpage is actually up.

The second curl command is logging in. The --data line is the data being sent to the webpage. It is pretending that you've filled out the login form on the login page, and is sending Garmin your username and password. The output from that page goes to the terminal as http webpage output (which I bet is being parsed for the information you want).

You say you've been an Android developer for a while, so I assume you know about Android programming and understanding how the Android API works. I am not an Android developer, but I've looked at the AndroidHttpClient API documentation and it doesn't look all that complex. Basically, you need to create an object that can send a request to http://connect.garmin.com/signin to verify it is up and running, and if it is, you send another request to https://connect.garmin.com/signin to sign in. The data you send is in the --data parameter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文