Example usage for org.apache.commons.httpclient Header toString

List of usage examples for org.apache.commons.httpclient Header toString

Introduction

In this page you can find the example usage for org.apache.commons.httpclient Header toString.

Prototype

public String toString() 

Source Link

Usage

From source file:com.leosys.core.utils.SendMessage.java

public static String postMessage(String phoneNo, String sendText) throws IOException {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod("http://sms.webchinese.cn/web_api/");
    post.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=gbk");// ?    
    NameValuePair[] data = { new NameValuePair("Uid", "fanyy"), // ??    
            new NameValuePair("Key", "694de3e5ca9f7015eaef"), // ??,    
            new NameValuePair("smsMob", phoneNo), // ??    
            new NameValuePair("smsText", sendText + "??") };//  
    post.setRequestBody(data);/*from  www .j a v a2 s . com*/

    client.executeMethod(post);
    Header[] headers = post.getResponseHeaders();
    int statusCode = post.getStatusCode();
    System.out.println("statusCode:" + statusCode);
    for (Header h : headers) {
        System.out.println(h.toString());
    }
    String result = new String(post.getResponseBodyAsString().getBytes("gbk"));
    System.out.println(result);
    post.releaseConnection();
    return result;
}

From source file:com.touch6.sm.gateway.webchinese.Webchinese.java

public static String batchSend(String url, String uid, String key, String phone, String msg, String contentType,
        String charset) throws CoreException {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod(url);
    post.addRequestHeader("Content-Type", contentType);//?
    NameValuePair[] data = { new NameValuePair("Uid", uid), new NameValuePair("Key", key),
            new NameValuePair("smsMob", phone), new NameValuePair("smsText", msg) };
    post.setRequestBody(data);/*from   w w  w. j ava 2 s  . co  m*/
    try {
        client.executeMethod(post);
    } catch (Exception e) {
        logger.info("??:", e);
        throw new CoreException(ECodeUtil.getCommError(SystemErrorConstant.SYSTEM_EXCEPTION));
    }
    Header[] headers = post.getResponseHeaders();
    int statusCode = post.getStatusCode();
    System.out.println("statusCode:" + statusCode);
    for (Header h : headers) {
        System.out.println(h.toString());
    }
    String result;
    try {
        result = new String(post.getResponseBodyAsString().getBytes(charset));
        System.out.println(result); //???
    } catch (Exception e) {
        logger.info("??:", e);
        throw new CoreException(ECodeUtil.getCommError(SystemErrorConstant.SYSTEM_EXCEPTION));
    }

    post.releaseConnection();
    return result;
}

From source file:cz.muni.fi.pa165.creatures.rest.client.services.utils.CRUDServiceHelper.java

/**
 * Helper methods which executes an HTTP {@code method} and writes the
 * output to the standard output (e.g. console). Return codes of the HTTP
 * responses are present so we can verify what happened at the server side.
 *
 * @param method method to send to the server side
 *//*from  w  w w .j a va 2s . c o m*/
public static void send(HttpMethodBase method) {
    HttpClient httpClient = new HttpClient();
    try {

        int result = httpClient.executeMethod(method);
        System.out.println(RESPONSE_STATUS_CODE + result);
        ResponseCode response = ResponseCode.fromInt(result);
        if (response != ResponseCode.NOT_FOUND && response != ResponseCode.SERVER_ERROR
                && response != ResponseCode.FORBIDDEN) {
            System.out.println(RESPONSE_HEADER);

            Header[] headers = method.getResponseHeaders();
            for (Header h : headers) {
                System.out.println(h.toString());
            }

            InputStreamReader isr = new InputStreamReader(method.getResponseBodyAsStream());
            BufferedReader br = new BufferedReader(isr);

            String line;

            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        }
    } catch (ConnectException ex) {
        logger.log(Level.WARNING, CONNECTION_EXCEPTION_MSG);
    } catch (IOException ex) {
        logger.log(Level.INFO, ex.getMessage());
    } finally {
        method.releaseConnection();
    }
}

From source file:com.zimbra.qa.unittest.prov.soap.SoapDebugListener.java

@Override
public void receiveSoapMessage(PostMethod postMethod, Element envelope) {
    if (level == Level.OFF) {
        return;//from   w  w  w . j av  a2  s .c o  m
    }

    System.out.println();
    System.out.println("=== Response ===");

    if (Level.needsHeader(level)) {
        Header[] headers = postMethod.getResponseHeaders();
        for (Header header : headers) {
            System.out.println(header.toString().trim()); // trim the ending crlf
        }
        System.out.println();
    }

    if (Level.needsBody(level)) {
        System.out.println(envelope.prettyPrint());
    }
}

From source file:com.zimbra.qa.unittest.prov.soap.SoapDebugListener.java

@Override
public void sendSoapMessage(PostMethod postMethod, Element envelope, HttpState httpState) {
    if (level == Level.OFF) {
        return;//from  ww  w.ja  v  a  2  s  . c om
    }

    System.out.println();
    System.out.println("=== Request ===");

    if (Level.needsHeader(level)) {
        try {
            URI uri = postMethod.getURI();
            System.out.println(uri.toString());
        } catch (URIException e) {
            e.printStackTrace();
        }

        // headers
        Header[] headers = postMethod.getRequestHeaders();
        for (Header header : headers) {
            System.out.println(header.toString().trim()); // trim the ending crlf
        }
        System.out.println();

        //cookies
        if (httpState != null) {
            Cookie[] cookies = httpState.getCookies();
            for (Cookie cookie : cookies) {
                System.out.println("Cookie: " + cookie.toString());
            }
        }
        System.out.println();
    }

    if (Level.needsBody(level)) {
        System.out.println(envelope.prettyPrint());
    }
}

From source file:com.zimbra.common.util.SoapDebugListener.java

@Override
public void receiveSoapMessage(PostMethod postMethod, Element envelope) {
    if (level == Level.OFF) {
        return;//from  w w  w.  ja v a  2 s . c o m
    }

    printer.println();
    printer.println("=== Response ===");

    if (Level.needsHeader(level)) {
        Header[] headers = postMethod.getResponseHeaders();
        for (Header header : headers) {
            printer.println(header.toString().trim()); // trim the ending crlf
        }
        printer.println();
    }

    if (Level.needsBody(level)) {
        printer.println(envelope.prettyPrint());
    }
}

From source file:com.zimbra.common.util.SoapDebugListener.java

@Override
public void sendSoapMessage(PostMethod postMethod, Element envelope, HttpState httpState) {
    if (level == Level.OFF) {
        return;/*from  w  w w  . j a v a  2  s  .c  om*/
    }

    printer.println();
    printer.println("=== Request ===");

    if (Level.needsHeader(level)) {
        try {
            URI uri = postMethod.getURI();
            printer.println(uri.toString());
        } catch (URIException e) {
            e.printStackTrace();
        }

        // headers
        Header[] headers = postMethod.getRequestHeaders();
        for (Header header : headers) {
            printer.println(header.toString().trim()); // trim the ending crlf
        }
        printer.println();

        //cookies
        if (httpState != null) {
            Cookie[] cookies = httpState.getCookies();
            for (Cookie cookie : cookies) {
                printer.println("Cookie: " + cookie.toString());
            }
        }
        printer.println();
    }

    if (Level.needsBody(level)) {
        printer.println(envelope.prettyPrint());
    }
}

From source file:com.eviware.soapui.impl.wsdl.monitor.jettyproxy.TunnelServlet.java

private byte[] getResponseToBytes(String footer, ExtendedHttpMethod postMethod, byte[] res) {
    String response = footer;//w ww  . j a va2s  .  c  o m

    Header[] headers = postMethod.getResponseHeaders();
    for (Header header : headers) {
        response += header.toString();
    }
    response += "\n";
    response += XmlUtils.prettyPrintXml(new String(res));

    return response.getBytes();
}

From source file:de.pdark.dsmp.ProxyDownload.java

/**
 * Do the download./* w  ww .  j  a  v  a 2 s  .com*/
 * 
 * @throws IOException
 * @throws DownloadFailed
 */
public void download() throws IOException, DownloadFailed {
    if (!config.isAllowed(url)) {
        throw new DownloadFailed(
                "HTTP/1.1 " + HttpStatus.SC_FORBIDDEN + " Download denied by rule in DSMP config");
    }

    // If there is a status file in the cache, return it instead of trying it again
    // As usual with caches, this one is a key area which will always cause
    // trouble.
    // TODO There should be a simple way to get rid of the cached statuses
    // TODO Maybe retry a download after a certain time?
    File statusFile = new File(dest.getAbsolutePath() + ".status");
    if (statusFile.exists()) {
        try {
            FileReader r = new FileReader(statusFile);
            char[] buffer = new char[(int) statusFile.length()];
            int len = r.read(buffer);
            r.close();
            String status = new String(buffer, 0, len);
            throw new DownloadFailed(status);
        } catch (IOException e) {
            log.warn("Error writing 'File not found'-Status to " + statusFile.getAbsolutePath(), e);
        }
    }

    mkdirs();

    HttpClient client = new HttpClient();

    String msg = "";
    if (config.useProxy(url)) {
        Credentials defaultcreds = new UsernamePasswordCredentials(config.getProxyUsername(),
                config.getProxyPassword());
        AuthScope scope = new AuthScope(config.getProxyHost(), config.getProxyPort(), AuthScope.ANY_REALM);
        HostConfiguration hc = new HostConfiguration();
        hc.setProxy(config.getProxyHost(), config.getProxyPort());
        client.setHostConfiguration(hc);
        client.getState().setProxyCredentials(scope, defaultcreds);
        msg = "via proxy ";
    }
    log.info("Downloading " + msg + "to " + dest.getAbsolutePath());

    GetMethod get = new GetMethod(url.toString());
    get.setFollowRedirects(true);
    try {
        int status = client.executeMethod(get);

        log.info("Download status: " + status);
        if (0 == 1 && log.isDebugEnabled()) {
            Header[] header = get.getResponseHeaders();
            for (Header aHeader : header)
                log.debug(aHeader.toString().trim());
        }

        log.info("Content: " + valueOf(get.getResponseHeader("Content-Length")) + " bytes; "
                + valueOf(get.getResponseHeader("Content-Type")));

        if (status != HttpStatus.SC_OK) {
            // Remember "File not found"
            if (status == HttpStatus.SC_NOT_FOUND) {
                try {
                    FileWriter w = new FileWriter(statusFile);
                    w.write(get.getStatusLine().toString());
                    w.close();
                } catch (IOException e) {
                    log.warn("Error writing 'File not found'-Status to " + statusFile.getAbsolutePath(), e);
                }
            }
            throw new DownloadFailed(get);
        }

        File dl = new File(dest.getAbsolutePath() + ".new");
        OutputStream out = new BufferedOutputStream(new FileOutputStream(dl));
        IOUtils.copy(get.getResponseBodyAsStream(), out);
        out.close();

        File bak = new File(dest.getAbsolutePath() + ".bak");
        if (bak.exists())
            bak.delete();
        if (dest.exists())
            dest.renameTo(bak);
        dl.renameTo(dest);
    } finally {
        get.releaseConnection();
    }
}

From source file:com.eviware.soapui.impl.wsdl.monitor.jettyproxy.ProxyServlet.java

private byte[] getResponseToBytes(String status, ExtendedHttpMethod postMethod, byte[] res) {
    String response = status.trim() + "\r\n";

    Header[] headers = postMethod.getResponseHeaders();
    for (Header header : headers) {
        response += header.toString().trim() + "\r\n";
    }//from   w w  w .  jav  a 2  s .c om
    response += "\r\n";

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        out.write(response.getBytes());
        out.write(res);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out.toByteArray();
}