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=Si开发者_开发百科gn+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
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.
精彩评论