将 bash 脚本移植到 java
我找到了以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Curl 是一个允许您(通过命令行)下载 http 和 https 请求的程序无需通过浏览器。它类似于 wget 命令。
同时,Android 的 HttpClient 不是一个命令,甚至不是一个函数。它是一个基于类的 API,允许您执行 http 请求。
我建议您浏览 Curl 手册页(我链接到的)并查看如果您可以看到传递给 Curl 的各种参数的作用。此命令会两次点击相同的 URL
“http://connect.garmin.com/signin”
。第一次它只是检查它是否可以访问。如果是,它将运行第二个curl
命令。以下是各个参数的含义:key=value
形式给出,这就是要使用的 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 secondcurl
command. Here are what the various parameters mean:key=value
, and that's the cookie to use./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 tohttps://connect.garmin.com/signin
to sign in. The data you send is in the--data
parameter.