Example usage for java.net ProtocolException printStackTrace

List of usage examples for java.net ProtocolException printStackTrace

Introduction

In this page you can find the example usage for java.net ProtocolException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:org.wso2.charon3.samples.group.sample01.CreateGroupSample.java

public static void main(String[] args) {
    try {/* ww  w .j a  v a2s . c o  m*/
        String url = "http://localhost:8080/scim/v2/Groups";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // Setting basic post request
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/scim+json");

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(createRequestBody);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();

        BufferedReader in;
        if (responseCode == HttpURLConnection.HTTP_CREATED) { // success
            in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        } else {
            in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
        }
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //printing result from response
        System.out.println("Response Code : " + responseCode);
        System.out.println("Response Message : " + con.getResponseMessage());
        System.out.println("Response Content : " + response.toString());

    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.wso2.charon3.samples.user.sample01.CreateUserSample.java

public static void main(String[] args) {
    try {//from  w  w  w  .j a v  a 2  s  . c  om
        String url = "http://localhost:8080/scim/v2/Users";
        URL obj = new URL(url);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();

        // Setting basic post request
        con.setRequestMethod("POST");
        con.setRequestProperty("Content-Type", "application/scim+json");

        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(createRequestBody);
        wr.flush();
        wr.close();

        int responseCode = con.getResponseCode();

        BufferedReader in;
        if (responseCode == HttpURLConnection.HTTP_CREATED) { // success
            in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        } else {
            in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
        }
        String inputLine;
        StringBuffer response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

        //printing result from response
        System.out.println("Response Code : " + responseCode);
        System.out.println("Response Message : " + con.getResponseMessage());
        System.out.println("Response Content : " + response.toString());

    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void setUpConnection(HttpURLConnection connection, String method) {
    try {/*from   w w  w.jav a 2s.c o m*/
        connection.setRequestMethod(method);
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");
    } catch (ProtocolException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void DownloadFile(String u) {
    try {//from  w ww .j a  v  a 2  s.  c  om
        Logd(TAG, "Starting download of: " + u);
        URL url = new URL(u);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();
        //File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        checkStorageDir();
        File storageDir = new File(
                Environment.getExternalStorageDirectory() + "/Android/data/com.nowsci.odm/.storage");
        File file = new File(storageDir, getFileName(u));
        Logd(TAG, "Storage directory: " + storageDir.toString());
        Logd(TAG, "File name: " + file.toString());
        FileOutputStream fileOutput = new FileOutputStream(file);
        InputStream inputStream = urlConnection.getInputStream();
        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        while ((bufferLength = inputStream.read(buffer)) > 0) {
            fileOutput.write(buffer, 0, bufferLength);
        }
        fileOutput.close();
        Logd(TAG, "File written");
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:oneDrive.OneDriveAPI.java

public static void uploadFile(String filePath) {
    URLConnection urlconnection = null;
    try {/*from  w w  w  .  ja v  a2s .  co m*/
        File file = new File(UPLOAD_PATH + filePath);
        URL url = new URL(DRIVE_PATH + file.getName() + ":/content");
        urlconnection = url.openConnection();
        urlconnection.setDoOutput(true);
        urlconnection.setDoInput(true);

        if (urlconnection instanceof HttpURLConnection) {
            try {
                ((HttpURLConnection) urlconnection).setRequestMethod("PUT");
                ((HttpURLConnection) urlconnection).setRequestProperty("Content-type",
                        "application/octet-stream");
                ((HttpURLConnection) urlconnection).addRequestProperty("Authorization",
                        "Bearer " + ACCESS_TOKEN);
                ((HttpURLConnection) urlconnection).connect();

            } catch (ProtocolException e) {
                e.printStackTrace();
            }
        }

        BufferedOutputStream bos = new BufferedOutputStream(urlconnection.getOutputStream());
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        int i;
        // read byte by byte until end of stream
        while ((i = bis.read()) >= 0) {
            bos.write(i);
        }
        bos.flush();
        bos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:com.keepsafe.switchboard.SwitchBoard.java

/**
 * Returns a String containing the server response from a GET request
 * @param address Valid http addess./*w w w.  j  a v a  2  s.  c  o  m*/
 * @param params String of params. Multiple params seperated with &. No leading ? in string
 * @return Returns String from server or null when failed.
 */
private static String readFromUrlGET(String address, String params) {
    if (address == null || params == null)
        return null;

    String completeUrl = address + "?" + params;
    if (DEBUG)
        Log.d(TAG, "readFromUrl(): " + completeUrl);

    try {
        URL url = new URL(completeUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setUseCaches(false);
        connection.setDoOutput(true);

        // get response
        InputStream is = connection.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(is);
        BufferedReader bufferReader = new BufferedReader(inputStreamReader, 8192);
        String line = "";
        StringBuffer resultContent = new StringBuffer();
        while ((line = bufferReader.readLine()) != null) {
            if (DEBUG)
                Log.d(TAG, line);
            resultContent.append(line);
        }
        bufferReader.close();

        if (DEBUG)
            Log.d(TAG, "readFromUrl() result: " + resultContent.toString());

        return resultContent.toString();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return null;
}

From source file:hack.ddakev.roadrant.MainActivity.java

public static String recognize(String url, File file) {
    OkHttpClient client = new OkHttpClient();
    MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
    OutputStream out = null;//  w w  w.  j  a v a2s.  c o m
    Response response = null;
    try {
        URL sendLoc = new URL(url);

        RequestBody requestBody = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("image", "license.png", RequestBody.create(MEDIA_TYPE_PNG, file)).build();
        Request request = new Request.Builder().header("Content-type", "application/json").url(sendLoc)
                .post(requestBody).build();
        response = client.newCall(request).execute();
        if (response.code() != 200)
            throw new IOException("Unexpected code " + response);
        else
            System.out.println(response.toString());
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (response != null) {
        try {
            String res = response.body().string();
            System.out.println(res);
            return res;
        } catch (IOException e) {
            return null;
        }
    } else
        return null;
}

From source file:hack.ddakev.roadrant.MainActivity.java

public static String post(String url, JSONObject data, int status) {
    OkHttpClient client = new OkHttpClient();
    OutputStream out = null;/*from ww w  . ja  v a2 s . c o m*/
    Response response = null;
    try {
        System.out.println("1");
        URL sendLoc = new URL(url);

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), data.toString());
        System.out.println("2");
        Request request = new Request.Builder().header("Content-type", "application/json").url(sendLoc)
                .post(requestBody).build();
        System.out.println(data.toString());
        System.out.println(request.toString());
        System.out.println(request.body().contentType());
        response = client.newCall(request).execute();
        System.out.println(response.toString());
        if (response.code() != 200 && response.code() != 201)
            throw new IOException("Unexpected code " + response);
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (response != null) {
        try {
            String res = response.body().string();
            System.out.println(res);
            return res;
        } catch (IOException e) {
            return null;
        }
    } else
        return null;
}

From source file:cn.edu.hfut.dmic.webcollector.example.DemoJsonCrawler.java

public DemoJsonCrawler(String crawlPath) {
    super(crawlPath);
    HttpRequesterImpl myRequester = new HttpRequesterImpl() {
        /*Override??http(HttpURLConnection)*/
        @Override/*from www .j  a  va2  s  . c  om*/
        public void configConnection(HttpURLConnection con) {

            try {
                con.setRequestMethod("POST");
            } catch (ProtocolException ex) {
                ex.printStackTrace();
            }

            /*http*/
            con.addRequestProperty("xxx", "xxxxxxx");
        }

    };
    myRequester.setCookie("cookie");
    this.setHttpRequester(myRequester);

}

From source file:de.zib.scalaris.examples.wikipedia.plugin.fourcaast.FourCaastAccounting.java

protected void pushSdrToServer(final WikiPageBeanBase page) {
    if (!page.getServiceUser().isEmpty()) {
        final String sdr = createSdr(page);
        //            System.out.println("Sending sdr...\n" + sdr);
        try {// w  ww  . ja  v a 2s.  c o m
            HttpURLConnection urlConn = (HttpURLConnection) accountingServer.openConnection();
            urlConn.setDoOutput(true);
            urlConn.setRequestMethod("PUT");
            OutputStreamWriter out = new OutputStreamWriter(urlConn.getOutputStream());
            out.write(sdr);
            out.close();

            //read the result from the server (necessary for the request to be send!)
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = in.readLine()) != null) {
                sb.append(line + '\n');
            }
            //                System.out.println(sb.toString());
            in.close();
        } catch (ProtocolException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}