Java URLConnection Create createConnection(String method, String destinationUrl)

Here you can find the source of createConnection(String method, String destinationUrl)

Description

create Connection

License

Open Source License

Declaration

private static HttpURLConnection createConnection(String method, String destinationUrl) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import java.util.HashMap;

import java.util.Map;
import java.util.Set;

public class Main {
    public static String METHOD_POST = "POST";
    public static String METHOD_POST_MULTIDATA = "PostMultidata";
    private static Map queryStringVars;
    private static Map postVars;
    private static Map uploadFiles;
    private static String boundary = "";
    private static String outputContent = "";
    private static OutputStream output;

    private static HttpURLConnection createConnection(String method, String destinationUrl) {
        URL url = null;//from   w  w w.java 2  s  .c o m
        HttpURLConnection httpConn = null;
        int code = -1;
        try {
            try {
                String queryString = getQueryString();
                if (!queryString.equals("")) {
                    destinationUrl = destinationUrl + "?" + queryString;
                }
                url = new URL(destinationUrl);
                httpConn = (HttpURLConnection) url.openConnection();
                if (uploadFiles.size() > 0) {
                    method = METHOD_POST_MULTIDATA;
                }
                if (method.equals(METHOD_POST_MULTIDATA)) {
                    httpConn.setRequestMethod("POST");
                } else {
                    httpConn.setRequestMethod(method);
                }
                httpConn.setUseCaches(false);
                if ((uploadFiles.size() > 0) && (postVars.size() > 0)) {
                    httpConn.setDoInput(true);
                }
                httpConn.setDoOutput(true);
                httpConn.setRequestProperty("Connection", "Keep-Alive");
                // use multipart if has to upload files
                if ((method.equals(METHOD_POST_MULTIDATA)) || (method.equals(METHOD_POST))) {
                    boundary = Long.toHexString(System.currentTimeMillis());
                    httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
                }

                // TODO calcular
                // httpConn.addRequestProperty("Content-length",
                // contentStrPck.size() + "");
                String CHARSET = "UTF8";
                httpConn.connect();
                PrintWriter writer = null;
                output = httpConn.getOutputStream(); // exception throws here
                writer = new PrintWriter(new OutputStreamWriter(output, CHARSET), true); // true = autoFlush,  important!

                sendPostVariables(writer, CHARSET);
                uploadFiles(writer);
                InputStreamReader in = new InputStreamReader((InputStream) httpConn.getContent());
                BufferedReader buff = new BufferedReader(in);
                String line;
                String text = "";
                do {
                    line = buff.readLine();
                    text += line + "\n";
                } while (line != null);
                outputContent = text;
                return httpConn;
            } catch (IOException io) { // throw new Exception("Server not found.");
                System.err.println(
                        "usp/ime/line/ivprog/model/utils/HttpUtil.java: createConnection(String,String): IOException "
                                + io.toString());
                io.printStackTrace();
            }
        } catch (Exception e) {
            System.err.println("usp/ime/line/ivprog/model/utils/HttpUtil.java: createConnection(String,String): "
                    + e.toString());
            e.printStackTrace();
        }
        return null;
    }

    private static String getQueryString() {
        String retorno = "";
        Set keySet = queryStringVars.keySet();
        // java.util.Set extends Collection providing java.util.Iterator iterator();
        //leo for (Object objKS : keySet) { retorno += "&" + objKS + "=" + queryStringVars.get(objKS);   } // this version do not compile on Java 4
        java.util.Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object objKS = iterator.next();
            retorno += "&" + objKS + "=" + queryStringVars.get(objKS);
        }
        if (!retorno.equals("")) {
            retorno = retorno.substring(1);
        }
        return retorno;
    }

    private static void sendPostVariables(PrintWriter writer, String charset) {
        Set keySet = postVars.keySet();
        //leo for (Object objKS : keySet) // this version do not compile on Java 4
        java.util.Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object objKS = iterator.next();
            writer.println("--" + boundary);
            writer.println("Content-Disposition: form-data; name=\"" + objKS + "\"");
            writer.println("Content-Type: text/plain; charset=" + charset);
            writer.println();
            writer.println(postVars.get(objKS));
        }
    }

    private static void uploadFiles(PrintWriter writer) {
        Set keySet = uploadFiles.keySet();
        //leo for (Object objKS : keySet)
        java.util.Iterator iterator = keySet.iterator();
        while (iterator.hasNext()) {
            Object objKS = iterator.next();

            Map fileData = (HashMap) uploadFiles.get(objKS);
            writer.println("--" + boundary);
            writer.println("Content-Disposition: form-data; name=\"" + objKS + "\"; filename=\""
                    + fileData.get("filename") + "\"");
            writer.println("Content-Transfer-Encoding: binary");
            writer.println();
            InputStream input = null;
            try {
                if (fileData.get("file") instanceof File) {
                    input = new FileInputStream((File) fileData.get("file"));
                } else if (fileData.get("file") instanceof byte[]) {
                    input = new ByteArrayInputStream((byte[]) fileData.get("file"));
                }
                byte[] buffer = new byte[1024];
                for (int length = 0; (length = input.read(buffer)) > 0;) {
                    output.write(buffer, 0, length);
                }
                output.flush(); // Important! Output cannot be closed. Close of
                // writer will close output as well.
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (input != null) {
                    try {
                        input.close();
                    } catch (IOException logOrIgnore) {
                    }
                }
            }
            writer.println(); // Important! Indicates end of binary boundary.
            // End of multipart/form-data.
            writer.println("--" + boundary + "--");
        }
    }
}

Related

  1. createConnection(final URL url, final String authValue)
  2. createConnection(String processorName, String baseUrl)
  3. createConnection(URL url)
  4. createConnection(URL url)
  5. createConnection(URL url)