List of usage examples for org.apache.commons.httpclient HttpMethodBase getResponseCharSet
public String getResponseCharSet()
From source file:org.springfield.mojo.http.HttpHelper.java
/** * Sends a standard HTTP request to the specified URI using the determined method. * Attaches the content, uses the specified content type, sets cookies, timeout and * request headers/*from w w w.j av a2 s . c om*/ * * @param method - the request method * @param uri - the uri to request * @param body - the content * @param contentType - the content type * @param cookies - cookies * @param timeout - timeout in milliseconds * @param charSet - the character set * @param requestHeaders - extra user defined headers * @return response */ public static Response sendRequest(String method, String uri, String body, String contentType, String cookies, int timeout, String charSet, Map<String, String> requestHeaders) { // http client HttpClient client = new HttpClient(); // method HttpMethodBase reqMethod = null; if (method.equals(HttpMethods.PUT)) { reqMethod = new PutMethod(uri); } else if (method.equals(HttpMethods.POST)) { reqMethod = new PostMethod(uri); } else if (method.equals(HttpMethods.GET)) { if (body != null) { // hack to be able to send a request body with a get (only if required) reqMethod = new PostMethod(uri) { public String getName() { return "GET"; } }; } else { reqMethod = new GetMethod(uri); } } else if (method.equals(HttpMethods.DELETE)) { if (body != null) { // hack to be able to send a request body with a delete (only if required) reqMethod = new PostMethod(uri) { public String getName() { return "DELETE"; } }; } else { reqMethod = new DeleteMethod(uri); } } else if (method.equals(HttpMethods.HEAD)) { reqMethod = new HeadMethod(uri); } else if (method.equals(HttpMethods.TRACE)) { reqMethod = new TraceMethod(uri); } else if (method.equals(HttpMethods.OPTIONS)) { reqMethod = new OptionsMethod(uri); } // add request body if (body != null) { try { RequestEntity entity = new StringRequestEntity(body, contentType, charSet); ((EntityEnclosingMethod) reqMethod).setRequestEntity(entity); reqMethod.setRequestHeader("Content-type", contentType); } catch (Exception e) { e.printStackTrace(); } } // add cookies if (cookies != null) { reqMethod.addRequestHeader("Cookie", cookies); } // add custom headers if (requestHeaders != null) { for (Map.Entry<String, String> header : requestHeaders.entrySet()) { String name = header.getKey(); String value = header.getValue(); reqMethod.addRequestHeader(name, value); } } Response response = new Response(); // do request try { if (timeout != -1) { client.getParams().setSoTimeout(timeout); } int statusCode = client.executeMethod(reqMethod); response.setStatusCode(statusCode); } catch (Exception e) { e.printStackTrace(); } // read response try { InputStream instream = reqMethod.getResponseBodyAsStream(); ByteArrayOutputStream outstream = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int len; while ((len = instream.read(buffer)) > 0) { outstream.write(buffer, 0, len); } String resp = new String(outstream.toByteArray(), reqMethod.getResponseCharSet()); response.setResponse(resp); //set content length long contentLength = reqMethod.getResponseContentLength(); response.setContentLength(contentLength); //set character set String respCharSet = reqMethod.getResponseCharSet(); response.setCharSet(respCharSet); //set all headers Header[] headers = reqMethod.getResponseHeaders(); response.setHeaders(headers); } catch (Exception e) { e.printStackTrace(); } // release connection reqMethod.releaseConnection(); // return return response; }