How do I log in and download a file from an https webpage with Java?

I need to go into the https page and upload the file using Java. I know all the URLs in advance:

baseURL = // a https URL;
urlMap = new HashMap<String, URL>();
urlMap.put("login", new URL(baseURL, "exec.asp?login=username&pass=XPTO"));
urlMap.put("logout", new URL(baseURL, "exec.asp?exec.asp?page=999"));
urlMap.put("file", new URL(baseURL, "exec.asp?file=111"));

If I try all these links in a web browser like firefox, they work.

Now when I do:

urlConnection = urlMap.get("login").openConnection();
urlConnection.connect();
BufferedReader in = new BufferedReader(
    new InputStreamReader(urlConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
    System.out.println(inputLine);
in.close();

I am returning the login HTML code again, and I cannot proceed to upload the file.

Thanks!

+3
source share
5 answers

I agree with Alnitak that the problem is probably the storage and return of cookies.

Another good option I used is the HttpClient from Jakarta Commons.

, , , , , querystrings ( HTTPS). HttpClient POST, .

+5

, cookie (. CookieHandler).

:

class MyCookieHandler extends CookieHandler {

    private Map<String, List<String>> cookies = new HashMap<String, List<String>>();

    @Override
    public Map<String, List<String>> get(URI uri,
            Map<String, List<String>> requestHeaders) throws IOException {
        String host = uri.getHost();
        Map<String, List<String>> ret = new HashMap<String, List<String>>();
        synchronized (cookies) {
            List<String> store = cookies.get(host);
            if (store != null) {
                store = Collections.unmodifiableList(store);
                ret.put("Cookie", store);
            }
        }

        return Collections.unmodifiableMap(ret);
    }

    @Override
    public void put(URI uri, Map<String, List<String>> responseHeaders)
            throws IOException {
        List<String> newCookies = responseHeaders.get("Set-Cookie");
        if (newCookies != null) {
            String host = uri.getHost();
            synchronized (cookies) {
                List<String> store = cookies.get(host);
                if (store == null) {
                    store = new ArrayList<String>();
                    cookies.put(host, store);
                }
                store.addAll(newCookies);
            }
        }
    }

}
+4

, - , , , , .

, HTTP , , , .

+3

, Java CURL http://sourceforge.net/projects/javacurl. , https , , .. .

eclipse , , .

wget java.

+2

You might want to try HttpUnit . Although it is written with website verification in mind, it may be useful for your problem.

On its website:

"... Written in Java, HttpUnit emulates the relevant parts of browser behavior, including form submission, JavaScript, basic HTTP authentication, cookies and automatic page redirection and allows the Java test code to check returned pages either as text, XML DOM or form containers, tables and links. "

+1
source

All Articles