Java HTTP Request request(boolean quiet, String method, URL url, Map body)

Here you can find the source of request(boolean quiet, String method, URL url, Map body)

Description

request

License

Apache License

Declaration

private static void request(boolean quiet, String method, URL url, Map<String, String> body)
            throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;

import java.net.URL;

import java.util.Map;

public class Main {
    private static void request(boolean quiet, String method, URL url/*
                                                                     * , String
                                                                     * username,
                                                                     * String
                                                                     * password
                                                                     */, Map<String, String> body)
            throws IOException {
        // sigh. openConnection() doesn't actually open the connection,
        // just gives you a URLConnection. connect() will open the connection.
        if (!quiet) {
            System.out.println("[issuing request: " + method + " " + url + "]");
        }//from  w ww .j a  v a2 s. c o m
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod(method);

        // write auth header
        // BASE64Encoder encoder = new BASE64Encoder();
        // String encodedCredential = encoder.encode( (username + ":" +
        // password).getBytes() );
        // connection.setRequestProperty("Authorization", "BASIC " +
        // encodedCredential);

        // write body if we're doing POST or PUT
        byte buffer[] = new byte[8192];
        int read = 0;
        if (body != null) {
            connection.setDoOutput(true);
            OutputStream output = connection.getOutputStream();
            DataOutputStream out2 = new DataOutputStream(output);
            out2.writeBytes(convert(body));
        }

        // do request
        long time = System.currentTimeMillis();
        connection.connect();

        //      InputStream responseBodyStream = connection.getInputStream();
        //      StringBuffer responseBody = new StringBuffer();
        // while ((read = responseBodyStream.read(buffer)) != -1)
        // {
        // responseBody.append(new String(buffer, 0, read));
        // }
        connection.disconnect();
        time = System.currentTimeMillis() - time;

        // start printing output
        //      if (!quiet)
        //         System.out.println("[read " + responseBody.length() + " chars in "
        //               + time + "ms]");

        // look at headers
        // the 0th header has a null key, and the value is the response line
        // ("HTTP/1.1 200 OK" or whatever)
        if (!quiet) {
            String header = null;
            String headerValue = null;
            int index = 0;
            while ((headerValue = connection.getHeaderField(index)) != null) {
                header = connection.getHeaderFieldKey(index);

                if (header == null)
                    System.out.println(headerValue);
                else
                    System.out.println(header + ": " + headerValue);

                index++;
            }
            System.out.println("");
        }

        // dump body
        //      System.out.print(responseBody);
        System.out.flush();
    }

    private static String convert(Map<String, String> data) {
        StringBuilder sb = new StringBuilder();
        for (String key : data.keySet()) {
            sb.append(key + "=" + data);
            sb.append("&");
        }

        return sb.subSequence(0, sb.length() - 2).toString();
    }
}

Related

  1. getOutputFromUrlConnection(String stringUrl, String requestProperty)
  2. getRequest(String urlString)
  3. getRequest(URL url, int timeout)
  4. getRequestContent(String urlText)
  5. makeUrlRequest(String surl, String data, String method)
  6. request(String httpUrl, Map httpArgMap)
  7. request(String url, int timeout, String method, Map header)
  8. request(String url, Map cookies, Map parameters)
  9. requestData(String url)