httpurlconnection 模拟登录并下载文件到本地

 1 package com.download;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.DataOutputStream;
 5 import java.io.InputStreamReader;
 6 import java.net.HttpURLConnection;
 7 import java.net.MalformedURLException;
 8 import java.net.URL;
 9 import java.util.List;
10 import java.util.Map;
11 
12 public class HttpRequestUtil {
13     private final static String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36";
14 
15     public static String sendPost(String urlParams, String params) {
16         BufferedReader in = null;
17         DataOutputStream wr = null;
18         String cookie = null;
19 
20         try {
21             URL obj = new URL(urlParams);
22             HttpURLConnection con = (HttpURLConnection) obj.openConnection();
23             // add reuqest header
24             con.setRequestMethod("POST");
25             con.setRequestProperty("User-Agent", USER_AGENT);
26             con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
27             con.setRequestProperty("Content-Type",
28                     "application/x-www-form-urlencoded; charset=UTF-8");
29             // Send post request
30             con.setDoOutput(true);
31             wr = new DataOutputStream(con.getOutputStream());
32             wr.write(params.getBytes("utf-8"));
33             wr.flush();
34 
35             // get response headers
36             /*
37              * Map<String, List<String>> responseHeaders =
38              * con.getHeaderFields(); for (Map.Entry<String, List<String>> entry
39              * : responseHeaders .entrySet()) { System.out.println("Key : " +
40              * entry.getKey() + " ,Value : " + entry.getValue()); }
41              */
42 
43             // get response content
44             int responseCode = con.getResponseCode();
45             if (responseCode == HttpURLConnection.HTTP_OK) {
46                 cookie = con.getHeaderField("Set-Cookie");
47                 in = new BufferedReader(new InputStreamReader(
48                         con.getInputStream()));
49                 String inputLine;
50                 StringBuffer response = new StringBuffer();
51                 while ((inputLine = in.readLine()) != null) {
52                     response.append(inputLine);
53                 }
54             }
55         } catch (Exception e) {
56             e.printStackTrace();
57         } finally {
58             try {
59                 if (wr != null) {
60                     wr.close();
61                 }
62                 if (in != null) {
63                     in.close();
64                 }
65             } catch (Exception e) {
66                 System.out.println("something  is wrong");
67             }
68 
69         }
70         return cookie;
71     }
72 79 }
package com.download;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class JsonFileDownload {
    private static final String LOGIN_URL = "";
    private static final String LOGIN_PARAMS = "";
    private static final String SQL_JSON_FILE = "";

    public static void readRemoteJsonFile() {
        String cookie = HttpRequestUtil.sendPost(LOGIN_URL, LOGIN_PARAMS);

        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            URL url = new URL(SQL_JSON_FILE);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Cookie:", cookie);
            con.setRequestProperty(
                    "User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36");

            reader = new BufferedReader(new InputStreamReader(
                    con.getInputStream()));
            writer = new BufferedWriter(new FileWriter("sql.txt"));

            String line = null;
            while ((line = reader.readLine()) != null) {
                writer.write(line);
                System.out.println(line);
                writer.newLine();
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
                if (writer != null) {
                    writer.close();
                }
            } catch (Exception e) {
                // TODO: handle exception
            }

        }
    }

    public static void main(String[] args) {
        JsonFileDownload.readRemoteJsonFile();

    }
}

 

posted @ 2015-11-06 14:38  程序猿进化之路  阅读(504)  评论(0)    收藏  举报