/**


     * 通过post方式发送请求

     * 

     * @param url

     * @param data

     *            userName=userName&passWord=passWord 不要有空格

     * @return


     */

    public static String request(String urlString, String data) {

        OutputStreamWriter wr = null;

        BufferedReader rd = null;

        String code = "";

        try {

            // Send data

            URL url = new URL(urlString);

            HttpURLConnection connection = (HttpURLConnection) url

                    .openConnection();

            connection.setDoOutput(true);

            // Read from the connection. Default is true.

            connection.setDoInput(true);

            // Set the post method. Default is GET

            connection.setRequestMethod("POST");

            // Post cannot use caches

            connection.setUseCaches(false);

            connection.setConnectTimeout(90000);

            wr = new OutputStreamWriter(connection.getOutputStream());

            wr.write(data);

            wr.flush();

            // Get the response

            rd = new BufferedReader(new InputStreamReader(connection

                    .getInputStream()));

            String line;

            while ((line = rd.readLine()) != null) {

                code = line;

                // Process line...

            }

        } catch (Exception e) {

            logger.log(Level.WARNING, "http post error ! URL:" + urlString, e);

        } finally {

            if (wr != null) {

                try {

                    wr.close();

                } catch (IOException e) {

                    // eat it

                }

            }

            if (rd != null) {

                try {

                    rd.close();

                } catch (IOException e) {

                    // eat it

                }

            }

        }

        return code;

    }
    ```
Comments
Write a Comment