Java JProgressBar excutePost(String targetURL, String urlParameters, JProgressBar progress)

Here you can find the source of excutePost(String targetURL, String urlParameters, JProgressBar progress)

Description

excute Post

License

Open Source License

Declaration

public static String excutePost(String targetURL, String urlParameters,
            JProgressBar progress) 

Method Source Code

//package com.java2s;
/*//  ww w  .j a va  2  s. co  m
 * This file is part of Spoutcraft Launcher (http://www.spout.org/).
 *
 * Spoutcraft Launcher is licensed under the SpoutDev License Version 1.
 *
 * Spoutcraft Launcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * In addition, 180 days after any changes are published, you can use the
 * software, incorporating those changes, under the terms of the MIT license,
 * as described in the SpoutDev License Version 1.
 *
 * Spoutcraft Launcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License,
 * the MIT license and the SpoutDev license version 1 along with this program.
 * If not, see <http://www.gnu.org/licenses/> for the GNU Lesser General Public
 * License and see <http://www.spout.org/SpoutDevLicenseV1.txt> for the full license,
 * including the MIT license.
 */

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import java.net.HttpURLConnection;
import javax.swing.JProgressBar;

public class Main {
    public static String excutePost(String targetURL, String urlParameters,
            JProgressBar progress) {
        HttpURLConnection connection = null;

        if (targetURL.startsWith("https://login.minecraft.net")) {
            targetURL = "http://session.minecraft.net/game/getversion.jsp";
        }

        try {
            URL url = new URL(targetURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");

            connection.setRequestProperty("Content-Length",
                    Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            connection.setConnectTimeout(10000);

            connection.connect();

            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(
                    new InputStreamReader(is));

            StringBuilder response = new StringBuilder();
            String line;
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();

            return response.toString();
        } catch (Exception e) {
            String message = "Login failed...";
            progress.setString(message);
        } finally {
            if (connection != null) {
                connection.disconnect();
            }
        }
        return null;
    }
}

Related

  1. createJProgressBar(String title)
  2. createPanel(String message, JProgressBar progressBar)
  3. createProgressBar()
  4. generateProgressBarFor(Object... objs)
  5. incProgressBar(JProgressBar jpb)
  6. printProgress(JProgressBar prBar, int progressValue)
  7. SetProgressBar(JProgressBar jpb)