Example usage for java.net HttpURLConnection getRequestProperties

List of usage examples for java.net HttpURLConnection getRequestProperties

Introduction

In this page you can find the example usage for java.net HttpURLConnection getRequestProperties.

Prototype

public Map<String, List<String>> getRequestProperties() 

Source Link

Document

Returns an unmodifiable Map of general request properties for this connection.

Usage

From source file:com.wifiafterconnect.ParsedHttpInput.java

public static void showRequestProperties(Worker context, HttpURLConnection conn) {
    Map<String, List<String>> reqProps = conn.getRequestProperties();
    context.debug("RequestProperties for [" + conn.getURL() + "]");
    for (String key : reqProps.keySet()) {
        String propStr = "RequestPropery[" + key + "] = {";
        for (String val : reqProps.get(key)) {
            propStr += "[" + val + "]";
        }//  www. j  a  va2  s. c o  m
        context.debug(propStr + "}");
    }
}

From source file:org.rapidcontext.app.plugin.http.HttpPostProcedure.java

/**
 * Logs the HTTP request to the procedure call context.
 *
 * @param cx             the procedure call context
 * @param con            the HTTP connection
 * @param data           the HTTP request data
 *///  w ww  .  j  a  v a2 s  . c o  m
private static void logRequest(CallContext cx, HttpURLConnection con, String data) {

    cx.log("HTTP " + con.getRequestMethod() + " " + con.getURL());
    Iterator iter = con.getRequestProperties().keySet().iterator();
    while (iter.hasNext()) {
        String key = (String) iter.next();
        cx.log("  " + key + ": " + con.getRequestProperty(key));
    }
    if (data != null) {
        cx.log(data);
    }
}

From source file:test.ShopThreadSrc.java

private boolean doJSShop() throws Exception {
    LogUtil.infoPrintf("?----------->");
    //loadCookie();
    boolean result = true;
    String postParams = addJSDynamicParams();
    HttpURLConnection loginConn = getHttpPostConn(this.jsshopurl);
    loginConn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
    LogUtil.debugPrintf("?HEADER===" + loginConn.getRequestProperties());
    DataOutputStream wr = new DataOutputStream(loginConn.getOutputStream());
    wr.writeBytes(postParams);//w  ww  .  java 2 s.c om
    wr.flush();
    wr.close();
    int responseCode = loginConn.getResponseCode();
    LogUtil.debugPrintf("\nSending 'POST' request to URL : " + this.jsshopurl);
    LogUtil.debugPrintf("Post parameters : " + postParams);
    LogUtil.debugPrintf("Response Code : " + responseCode);
    Map<String, List<String>> header = loginConn.getHeaderFields();
    LogUtil.debugPrintf("??HEADER===" + header);
    List<String> cookie = header.get("Set-Cookie");
    if (cookie == null || cookie.size() == 0) {
        result = false;
        LogUtil.infoPrintf("?----------->");
    } else {
        LogUtil.infoPrintf("??----------->");
        LogUtil.debugPrintf("cookie====" + cookie);
        setCookies(cookie);
    }
    LogUtil.infoPrintf("??----------->");
    //System.out.println(list.toHtml());
    return result;
}

From source file:test.ShopThreadSrc.java

private boolean doPTShop() throws Exception {
    LogUtil.infoPrintf("?----------->");
    //loadCookie();
    boolean result = true;
    String postParams = addPTDynamicParams();
    HttpURLConnection loginConn = getHttpPostConn(this.ptshopurl);
    loginConn.setRequestProperty("Host", "danbao.5173.com");
    loginConn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
    LogUtil.debugPrintf("?HEADER===" + loginConn.getRequestProperties());
    DataOutputStream wr = new DataOutputStream(loginConn.getOutputStream());
    wr.writeBytes(postParams);/*from w  w w  .ja  v a 2 s. c  o m*/
    wr.flush();
    wr.close();
    int responseCode = loginConn.getResponseCode();
    LogUtil.debugPrintf("\nSending 'POST' request to URL : " + this.ptshopurl);
    LogUtil.debugPrintf("Post parameters : " + postParams);
    LogUtil.debugPrintf("Response Code : " + responseCode);
    Map<String, List<String>> header = loginConn.getHeaderFields();
    LogUtil.debugPrintf("??HEADER===" + header);
    List<String> cookie = header.get("Set-Cookie");
    if (cookie == null || cookie.size() == 0) {
        result = false;
        LogUtil.infoPrintf("?----------->");
    } else {
        LogUtil.infoPrintf("??----------->");
        LogUtil.debugPrintf("cookie====" + cookie);
        setCookies(cookie);
    }
    LogUtil.infoPrintf("??----------->");
    //System.out.println(list.toHtml());
    return result;
}

From source file:org.apache.jmeter.protocol.http.sampler.HTTPJavaImpl.java

/**
 * Get all the headers for the <code>HttpURLConnection</code> passed in
 *
 * @param conn/*w  w  w. j  av  a  2s.  c  o m*/
 *            <code>HttpUrlConnection</code> which represents the URL
 *            request
 * @return the headers as a string
 */
private String getConnectionHeaders(HttpURLConnection conn) {
    // Get all the request properties, which are the headers set on the connection
    StringBuilder hdrs = new StringBuilder(100);
    Map<String, List<String>> requestHeaders = conn.getRequestProperties();
    for (Map.Entry<String, List<String>> entry : requestHeaders.entrySet()) {
        String headerKey = entry.getKey();
        // Exclude the COOKIE header, since cookie is reported separately in the sample
        if (!HTTPConstants.HEADER_COOKIE.equalsIgnoreCase(headerKey)) {
            // value is a List of Strings
            for (String value : entry.getValue()) {
                hdrs.append(headerKey);
                hdrs.append(": "); // $NON-NLS-1$
                hdrs.append(value);
                hdrs.append("\n"); // $NON-NLS-1$
            }
        }
    }
    return hdrs.toString();
}

From source file:prototypes.ws.proxy.soap.proxy.ProxyServlet.java

/**
 * Recept all request./*from  w ww .  j a v  a 2 s .  co  m*/
 *
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
@Override
protected void doRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpURLConnection httpConn = null;
    LOGGER.debug("doRequest");
    BackendExchange backendExchange = RequestContext.getBackendExchange(request);
    LOGGER.trace("BackendExchange Hashcode : {}", Integer.toHexString(backendExchange.hashCode()));
    try {
        URL targetUrl = Requests.resolveTargetUrl(request, backendExchange.getUri());
        httpConn = prepareBackendConnection(targetUrl, request, backendExchange.getRequestHeaders());

        // save final state of request headers
        backendExchange.setRequestHeaders(httpConn.getRequestProperties());

        // Send request
        byte[] body = backendExchange.getRequestBody();

        backendExchange.start();
        boolean gzipped = false;
        try {
            if (body.length > 0) {
                httpConn.getOutputStream().write(body);
            } else {
                LOGGER.warn("Body Empty");
            }

            gzipped = "gzip".equals(httpConn.getContentEncoding());
            // Get response. If response is gzipped, uncompress it

            backendExchange.setResponseBody(Streams.getBytes(httpConn.getInputStream(), gzipped));
        } catch (java.net.SocketTimeoutException ex) {
            throw new IOException("Time out : " + ex.getMessage(), ex);
        } catch (IOException ex) {
            LOGGER.warn("Failed to read target response body {}", ex);
            backendExchange.setResponseBody(Streams.getBytes(httpConn.getErrorStream(), gzipped));
        } finally {
            backendExchange.stop();
        }

        // Stores infos
        backendExchange.setResponseCode(httpConn.getResponseCode());
        backendExchange.setResponseHeaders(httpConn.getHeaderFields());

        // Specific error code treatment
        switch (backendExchange.getResponseCode()) {
        case 0:
            // No response
            LOGGER.debug("ResponseCode =  0 !!!");
            Requests.sendErrorServer(request, response,
                    String.format(ProxyErrorConstants.EMPTY_RESPONSE, targetUrl.toString()));
            return;
        case 404:
            LOGGER.debug("404 returned");
            Requests.sendErrorServer(request, response,
                    String.format(ProxyErrorConstants.NOT_FOUND, targetUrl.toString()), 404);
            return;
        default:
            break;
        }

        // return response with filtered headers
        List<String> respHeadersToIgnore = new ArrayList<String>(RESP_HEADERS_TO_IGNORE);
        addResponseHeaders(response, backendExchange, respHeadersToIgnore);
        response.setStatus(backendExchange.getResponseCode());

        response.getOutputStream().write(backendExchange.getResponseBody());
    } catch (IllegalStateException e1) {
        // bad url
        Requests.sendErrorClient(request, response, e1.getMessage());
    } catch (ClassCastException ex) {
        // bad url
        Requests.sendErrorClient(request, response, ex.getMessage());
    } catch (IOException ex) {
        LOGGER.error("Backend call in ERROR");
        // bad call
        Requests.sendErrorServer(request, response, ex.getMessage());
    } catch (Exception ex) {
        LOGGER.error("Error during proxying : {}", ex);
        // protect from all exceptions
        Requests.sendInternalErrorServer(request, response, ex.getMessage());
    } finally {
        LOGGER.trace("BackendExchange Hashcode : {}", Integer.toHexString(backendExchange.hashCode()));
        LOGGER.debug("BackendExchange : {}", backendExchange);
        if (httpConn != null) {
            try {
                httpConn.disconnect();
            } catch (Exception ex) {
                LOGGER.warn("Error on disconnect {}", ex);
            }
        }
    }
}

From source file:org.apache.jmeter.protocol.http.sampler.HTTPJavaImplClassifier.java

/**
 * Get all the headers for the <code>HttpURLConnection</code> passed in
 * /*w ww. j ava2  s.  c o  m*/
 * @param conn
 *            <code>HttpUrlConnection</code> which represents the URL
 *            request
 * @return the headers as a string
 */
private String getConnectionHeaders(HttpURLConnection conn) {
    // Get all the request properties, which are the headers set on the
    // connection
    StringBuilder hdrs = new StringBuilder(100);
    Map<String, List<String>> requestHeaders = conn.getRequestProperties();
    for (Map.Entry<String, List<String>> entry : requestHeaders.entrySet()) {
        String headerKey = entry.getKey();
        // Exclude the COOKIE header, since cookie is reported separately in
        // the sample
        if (!HTTPConstants.HEADER_COOKIE.equalsIgnoreCase(headerKey)) {
            // value is a List of Strings
            for (String value : entry.getValue()) {
                hdrs.append(headerKey);
                hdrs.append(": "); // $NON-NLS-1$
                hdrs.append(value);
                hdrs.append("\n"); // $NON-NLS-1$
            }
        }
    }
    return hdrs.toString();
}

From source file:com.dao.ShopThread.java

private boolean doJSShop() throws Exception {
    LogUtil.debugPrintf("?----------->");
    // loadCookie();
    boolean result = true;
    LogUtil.webPrintf("??...");
    String postParams = addJSDynamicParams();
    LogUtil.webPrintf("???");
    LogUtil.webPrintf("?...");
    HttpURLConnection loginConn = getHttpPostConn(this.jsshopurl);
    loginConn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
    LogUtil.debugPrintf("?HEADER===" + loginConn.getRequestProperties());
    DataOutputStream wr = new DataOutputStream(loginConn.getOutputStream());
    wr.writeBytes(postParams);/*from  ww  w.  j a v a  2 s .c  o  m*/
    wr.flush();
    wr.close();
    int responseCode = loginConn.getResponseCode();
    LogUtil.debugPrintf("\nSending 'POST' request to URL : " + this.jsshopurl);
    LogUtil.debugPrintf("Post parameters : " + postParams);
    LogUtil.debugPrintf("Response Code : " + responseCode);
    Map<String, List<String>> header = loginConn.getHeaderFields();
    LogUtil.debugPrintf("??HEADER===" + header);
    List<String> cookie = header.get("Set-Cookie");
    if (cookie == null || cookie.size() == 0) {
        result = false;
        LogUtil.webPrintf("?!");
    } else {
        LogUtil.webPrintf("??!");
        LogUtil.debugPrintf("cookie====" + cookie);
        LogUtil.debugPrintf("Location :" + loginConn.getURL().toString());
        setCookies(cookie);
        LogUtil.webPrintf("?...");
        String payurl = getPayKey(loginConn, "js");
        LogUtil.webPrintf("??");
        if (payurl != null && !payurl.equals("")) {
            LogUtil.debugPrintf("?url:" + payurl);
            LogUtil.webPrintf("...");
            pay(payurl);
            LogUtil.webPrintf("?");
        } else {
            LogUtil.webPrintf("?");
            LogUtil.debugPrintf("?url");
        }
    }
    LogUtil.debugPrintf("??----------->");
    // System.out.println(list.toHtml());
    return result;
}

From source file:com.dao.ShopThread.java

private boolean doPTShop() throws Exception {
    long startTime = System.currentTimeMillis();
    // loadCookie();
    boolean result = true;
    LogUtil.webPrintf("??...");
    String postParams = addPTDynamicParams();
    LogUtil.webPrintf("???");
    LogUtil.webPrintf("?...");
    HttpURLConnection loginConn = getHttpPostConn(this.ptshopurl);
    loginConn.setRequestProperty("Host", "danbao.5173.com");
    loginConn.setRequestProperty("Content-Length", Integer.toString(postParams.length()));
    LogUtil.debugPrintf("?HEADER===" + loginConn.getRequestProperties());
    DataOutputStream wr = new DataOutputStream(loginConn.getOutputStream());
    wr.writeBytes(postParams);//from ww  w .j  a v  a 2  s.  c  o m
    wr.flush();
    wr.close();
    int responseCode = loginConn.getResponseCode();
    LogUtil.debugPrintf("\nSending 'POST' request to URL : " + this.ptshopurl);
    LogUtil.debugPrintf("Post parameters : " + postParams);
    LogUtil.debugPrintf("Response Code : " + responseCode);
    Map<String, List<String>> header = loginConn.getHeaderFields();
    LogUtil.debugPrintf("??HEADER===" + header);
    List<String> cookie = header.get("Set-Cookie");
    if (cookie == null || cookie.size() == 0) {
        result = false;
        LogUtil.webPrintf("?!");
    } else {
        LogUtil.webPrintf("??!");
        LogUtil.debugPrintf("cookie====" + cookie);
        LogUtil.debugPrintf("Location :" + loginConn.getURL().toString());
        setCookies(cookie);
        LogUtil.webPrintf("?...");
        String payurl = getPayKey(loginConn, "db");
        LogUtil.webPrintf("??");
        if (payurl != null && !payurl.equals("")) {
            LogUtil.debugPrintf("?url:" + payurl);
            LogUtil.webPrintf("...");
            pay(payurl);
            LogUtil.webPrintf("?");
        } else {
            LogUtil.webPrintf("?");
            LogUtil.debugPrintf("?url");
        }
    }
    return result;
}

From source file:com.microsoft.azure.storage.core.Utility.java

/**
 * Logs the HttpURLConnection request. If an exception is encountered, logs nothing.
 * //from   w w w  .j a  v  a  2  s . c  o m
 * @param conn
 *            The HttpURLConnection to serialize.
 * @param opContext 
 *            The operation context which provides the logger.
 */
public static void logHttpRequest(HttpURLConnection conn, OperationContext opContext) throws IOException {
    if (Logger.shouldLog(opContext)) {
        try {
            StringBuilder bld = new StringBuilder();

            bld.append(conn.getRequestMethod());
            bld.append(" ");
            bld.append(conn.getURL());
            bld.append("\n");

            // The Authorization header will not appear due to a security feature in HttpURLConnection
            for (Map.Entry<String, List<String>> header : conn.getRequestProperties().entrySet()) {
                if (header.getKey() != null) {
                    bld.append(header.getKey());
                    bld.append(": ");
                }

                for (int i = 0; i < header.getValue().size(); i++) {
                    bld.append(header.getValue().get(i));
                    if (i < header.getValue().size() - 1) {
                        bld.append(",");
                    }
                }
                bld.append('\n');
            }

            Logger.trace(opContext, bld.toString());
        } catch (Exception e) {
            // Do nothing
        }
    }
}