Java 访问网页

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author GaoFeng Chen
 */
public class AllResult {

    /**
     * @param args
     * @throws Exception
     */
    public static void main(String[] args) throws Exception {

        // 要访问的网页
        String urlStr = "http://xxxxxx";
        URL url;

        // 读文件,访问网页的引数放在一个文件中
        List<String> seqList = readFile("D:\\", "seq.txt");
        if (seqList.size() > 0) {

//  每次运行时将前一次的结果清空
            write("D:\\seqResult.txt", "OVERWRITE", new StringBuffer(
                    "the name of the sequence").append("                ")
                    .append("the result").toString());
            int i = 1;
            for (String seq : seqList) {
                System.out.println("The current row number is: " + i++);
                try {

            // 准备引数
                    Map parameters = setParameter(seq);

          // 获取引数
                    String parameter = getParameter(parameters);
                    byte[] b = parameter.getBytes();
                    url = new URL(urlStr);
                    URLConnection URLconnection = url.openConnection();
                    HttpURLConnection httpConnection = (HttpURLConnection) URLconnection;
                    httpConnection.setDoOutput(true);
                    // Read from the connection. Default is true.
                    httpConnection.setDoInput(true);
                    // Set the post method. Default is GET
                    httpConnection.setRequestMethod("POST");
                    // Post cannot use caches
                    httpConnection.setUseCaches(false);
                    OutputStream reqOut = null;
                    reqOut = httpConnection.getOutputStream();
                    reqOut.write(b, 0, b.length);
                    int responseCode = httpConnection.getResponseCode();
                    if (responseCode == HttpURLConnection.HTTP_OK) {
                        InputStream urlStream = httpConnection.getInputStream();

                        BufferedReader bufferedReader = new BufferedReader(
                                new InputStreamReader(urlStream));
                        String sCurrentLine = "";
                        while ((sCurrentLine = bufferedReader.readLine()) != null) {

               // 只把自己感兴趣的返回信息取出来
                            if (sCurrentLine.contains("XXXXX")) {

             // 将获取的数据写入到指定的文件

                             write("D:\\seqResult.txt", "APPEND",
                                     new StringBuffer(seq).append(":")
                                             .append(sCurrentLine)
                                             .toString());
                            }
                        }

                    } else {
                        System.err.println("FAIL");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("All of the sequence is over");
    }

    /**
     * 
     * @param sequence
     * @return
     */
    public static Map<String, String> setParameter(String sequence) {
        Map<String, String> parameters = new HashMap<String, String>();
        parameters.put("xxxx", "xx");
        parameters.put("xxxx", sequence);
        parameters.put("xxxx", "xx");
        parameters.put("xxxx", "xx");
        parameters.put("xxxx", "xx");
        parameters.put("xxxx", "xx");
        return parameters;
    }

    /**
     * 
     * @param parameters
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getParameter(Map parameters)
            throws UnsupportedEncodingException {

        StringBuffer parameter = new StringBuffer();
        for (java.util.Iterator iter = parameters.entrySet().iterator(); iter
                .hasNext();) {
            Entry element = (Entry) iter.next();
            parameter.append(element.getKey().toString());
            parameter.append("=");
            parameter.append(URLEncoder.encode(element.getValue().toString(),
                    "UTF-8"));
            parameter.append("&");
        }
        if (parameter.length() > 0) {
            parameter = parameter.deleteCharAt(parameter.length() - 1);
        }
        return parameter.toString();

    }

    /**
     * reader the file
     * 
     * @param dataPath
     *            path
     * @param fileName
     *            fileName
     * @return data file
     */
    public static List<String> readFile(String dataPath, String fileName) {
        File file = new File(dataPath + fileName);
        BufferedReader reader = null;
        List<String> value = new ArrayList<String>();
        try {
            reader = new BufferedReader(new FileReader(file));
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                value.add(tempString);
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }
        return value;
    }

    /**
     * 
     * @param strFileName
     * @param strWriteMode
     * @param lstOutputData
     * @return
     * @throws Exception
     */
    public static int write(String strFileName, String strWriteMode,
            String outputData) throws Exception {
        FileOutputStream fileOutputStream;
        OutputStreamWriter outputStreamWriter;
        BufferedWriter bufferedWriter;
        int countOutput;
        fileOutputStream = null;
        outputStreamWriter = null;
        bufferedWriter = null;
        countOutput = 0;
        try {
            if (strFileName == null)
                throw new IllegalArgumentException();
            if (strWriteMode == null)
                throw new IllegalArgumentException();
            if (!"OVERWRITE".equals(strWriteMode)
                    && !"APPEND".equals(strWriteMode))
                throw new IllegalArgumentException();
            if (outputData == null)
                throw new IllegalArgumentException();
            File file = new File(strFileName);
            if ("OVERWRITE".equals(strWriteMode))
                fileOutputStream = new FileOutputStream(file);
            else
                fileOutputStream = new FileOutputStream(file, true);
            outputStreamWriter = new OutputStreamWriter(fileOutputStream,
                    "MS932");
            bufferedWriter = new BufferedWriter(outputStreamWriter);
            // for (int i = 0; i < lstOutputData.size(); i++) {
            // String strLine = (String) lstOutputData.get(i);

            bufferedWriter.write(outputData);
            bufferedWriter.write("\n");
            countOutput++;
            // }

        } catch (Exception ex) {

        }

        closeOutputStream(fileOutputStream, outputStreamWriter, bufferedWriter);
        return countOutput;
    }

    /**
     * 
     * @param fileOutputStream
     * @param outputStreamWriter
     * @param bufferedWriter
     */
    private static void closeOutputStream(FileOutputStream fileOutputStream,
            OutputStreamWriter outputStreamWriter, BufferedWriter bufferedWriter) {
        if (bufferedWriter != null)
            try {
                bufferedWriter.close();
            } catch (IOException ex) {
            }
        if (outputStreamWriter != null)
            try {
                outputStreamWriter.close();
            } catch (IOException ex) {
            }
        if (fileOutputStream != null)
            try {
                fileOutputStream.close();
            } catch (IOException ex) {
            }
    }
}

猜你喜欢

转载自fengyilin.iteye.com/blog/2341384