List of usage examples for org.apache.commons.httpclient HttpMethod getStatusCode
public abstract int getStatusCode();
From source file:Correct.java
public static void main(String[] args) { String URLL = ""; HttpClient client = new HttpClient(); HttpMethod method = new PostMethod(URLL); method.setDoAuthentication(true);//w ww . j a v a 2s . co m HostConfiguration hostConfig = client.getHostConfiguration(); hostConfig.setHost("172.29.38.8"); hostConfig.setProxy("172.29.90.4", 8); // NTCredentials proxyCredentials = new NTCredentials("", "", "", ""); client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", "")); //// try { //// URL url = new URL(""); //// HttpURLConnection urls = (HttpURLConnection) url.openConnection(); //// //// //// } catch (MalformedURLException ex) { //// Logger.getLogger(Correct.class.getName()).log(Level.SEVERE, null, ex); //// } catch (IOException ex) { //// Logger.getLogger(Correct.class.getName()).log(Level.SEVERE, null, ex); //// } try { // send the transaction client.executeMethod(hostConfig, method); StatusLine status = method.getStatusLine(); if (status != null && method.getStatusCode() == 200) { System.out.println(method.getResponseBodyAsString() + "\n Status code : " + status); } else { System.err.println(method.getStatusLine() + "\n: Posting Failed !"); } } catch (IOException ioe) { ioe.printStackTrace(); } method.releaseConnection(); }
From source file:com.amazonaws.sqs.util.HttpResponse.java
public static HttpResponse parseMethod(HttpMethod method) throws IOException { int httpStatusCode = method.getStatusCode(); InputStream in = method.getResponseBodyAsStream(); int numRead = -1; byte[] buf = new byte[4 * 1024]; String responseBody = new String(""); while ((numRead = in.read(buf)) != -1) { String str = new String(buf, 0, numRead); responseBody = responseBody + str; }//from w w w. j a va 2 s. com method.releaseConnection(); return new HttpResponse(responseBody, httpStatusCode); }
From source file:com.discursive.jccook.httpclient.RedirectExample.java
private static void executeMethod(HttpClient client, HttpMethod method) throws IOException, HttpException { client.executeMethod(method);/*from www .ja v a 2 s . com*/ System.out.println("Response Code: " + method.getStatusCode()); String response = method.getResponseBodyAsString(); System.out.println(response); method.releaseConnection(); method.recycle(); }
From source file:com.renlg.util.NetAssist.java
public static String delegateGet(String url) { StringBuffer response = new StringBuffer(); HttpClient client = new HttpClient(); HttpMethod method = null; try {//w ww.java 2 s . c o m method = new GetMethod(url); client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream(), "utf8")); String line; while ((line = reader.readLine()) != null) { response.append(line); } reader.close(); } } catch (Exception e) { log.error("HTTP Get" + url + "??", e); } finally { if (method != null) method.releaseConnection(); } return response.toString(); }
From source file:com.netflix.genie.server.util.NetUtil.java
/** * Returns the response from an HTTP GET call if it succeeds, null * otherwise.// www . ja v a 2 s. c o m * * @param uri The URI to execute the HTTP GET on * @return response from an HTTP GET call if it succeeds, null otherwise * @throws IOException if there was an error with the HTTP request */ private static String httpGet(final String uri) throws IOException { String response = null; //TODO: Use one of other http clients to remove dependency final HttpClient client = new HttpClient(); final HttpMethod method = new GetMethod(uri); client.executeMethod(method); final int status = method.getStatusCode(); if (status == HttpURLConnection.HTTP_OK) { response = method.getResponseBodyAsString(); } return response; }
From source file:com.urswolfer.intellij.plugin.gerrit.rest.GerritApiUtil.java
@Nullable private static JsonElement request(@NotNull String host, @Nullable String login, @Nullable String password, @NotNull String path, @Nullable String requestBody, boolean post) { HttpMethod method = null; try {//from w w w . j av a2 s .co m method = doREST(host, login, password, path, requestBody, post); String resp = method.getResponseBodyAsString(); if (method.getStatusCode() != 200) { String message = String.format("Request not successful. Message: %s. Status-Code: %s.", method.getStatusText(), method.getStatusCode()); LOG.warn(message); throw new HttpStatusException(method.getStatusCode(), method.getStatusText(), message); } if (resp == null) { String message = String.format("Unexpectedly empty response: %s.", resp); LOG.warn(message); throw new RuntimeException(message); } return parseResponse(resp); } catch (IOException e) { LOG.warn(String.format("Request failed: %s", e.getMessage()), e); throw Throwables.propagate(e); } finally { if (method != null) { method.releaseConnection(); } } }
From source file:apm.common.utils.HttpTookit.java
/** * HTTP GET?HTML//from w w w . ja v a 2 s .c om * * @param url * URL? * @param queryString * ?,?null * @param charset * * @param pretty * ? * @return ?HTML */ public static String doGet(String url, String queryString, String charset, boolean pretty) { StringBuffer response = new StringBuffer(); HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(url); try { if (StringUtils.isNotBlank(queryString)) // get??http?????%? method.setQueryString(URIUtil.encodeQuery(queryString)); client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream(), charset)); String line; while ((line = reader.readLine()) != null) { if (pretty) { response.append(line).append(System.getProperty("line.separator")); } else { response.append(line); } } reader.close(); } } catch (URIException e) { log.error("HTTP Get?" + queryString + "???", e); } catch (IOException e) { log.error("HTTP Get" + url + "??", e); } finally { method.releaseConnection(); } return response.toString(); }
From source file:edu.du.penrose.systems.fedoraProxy.web.bus.ProxyController.java
/** * Call the HttpMethod and write all results and status to the HTTP response * object.// w w w . java 2s.c o m * * THIS IS THE REQUEST WE NEED TO DUPLICATE GET * /fedora/get/codu:72/ECTD_test_1_access.pdf HTTP/1.1 Host: localhost:8080 * Connection: keep-alive Accept: * application/xml,application/xhtml+xml,text/ * html;q=0.9,text/plain;q=0.8,image/png,*;q=0.5 User-Agent: Mozilla/5.0 * (X11; U; Linux x86_64; en-US) AppleWebKit/534.10 (KHTML, like Gecko) * Chrome/8.0.552.215 Safari/534.10 Accept-Encoding: gzip,deflate,sdch * Accept-Language: en-US,en;q=0.8 Accept-Charset: * ISO-8859-1,utf-8;q=0.7,*;q=0.3 Cookie: fez_list=YjowOw%3D%3D * * ACTUAL SENT GET /fedora/get/codu:72/ECTD_test_1_access.pdf HTTP/1.1 * Accept: * application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q * =0.8,image/png,*;q=0.5 Connection: keep-alive Accept-Encoding: * gzip,deflate,sdch Accept-Language: en-US,en;q=0.8 Accept-Charset: * ISO-8859-1,utf-8;q=0.7,*;q=0.3 : User-Agent: Jakarta * Commons-HttpClient/3.1 Host: localhost:8080 * * @param proxyCommand * @param response * @param authenicate true to set Preemptive authentication */ public static void executeMethod(String proxyCommand, HttpServletResponse response, boolean authenicate) { HttpClient theClient = new HttpClient(); HttpMethod method = new GetMethod(proxyCommand); setDefaultHeaders(method); setAdrCredentials(theClient, authenicate); try { theClient.executeMethod(method); response.setStatus(method.getStatusCode()); // Set the content type, as it comes from the server Header[] headers = method.getResponseHeaders(); for (Header header : headers) { if ("Content-Type".equalsIgnoreCase(header.getName())) { response.setContentType(header.getValue()); } /** * Copy all headers, except Transfer-Encoding which is getting set * set elsewhere. At this point response.containsHeader( * "Transfer-Encoding" ) is false, however it is getting set twice * according to wireshark, therefore we do not set it here. */ if (!header.getName().equalsIgnoreCase("Transfer-Encoding")) { response.setHeader(header.getName(), header.getValue()); } } // Write the body, flush and close InputStream is = method.getResponseBodyAsStream(); BufferedInputStream bis = new BufferedInputStream(is); StringBuffer sb = new StringBuffer(); byte[] bytes = new byte[8192]; // reading as chunk of 8192 bytes int count = bis.read(bytes); while (count != -1 && count <= 8192) { response.getOutputStream().write(bytes, 0, count); count = bis.read(bytes); } bis.close(); response.getOutputStream().flush(); response.getOutputStream().close(); } catch (Exception e) { logger.error(e.getMessage()); } finally { method.releaseConnection(); } }
From source file:apm.common.utils.HttpTookit.java
/** * HTTP POST?HTML/*from ww w . j a va2s . co m*/ * * @param url * URL? * @param params * ?,?null * @param charset * * @param pretty * ? * @return ?HTML */ public static String doPost(String url, Map<String, String> params, String charset, boolean pretty) { StringBuffer response = new StringBuffer(); HttpClient client = new HttpClient(); HttpMethod method = new PostMethod(url); // Http Post? if (params != null) { HttpMethodParams p = new HttpMethodParams(); for (Map.Entry<String, String> entry : params.entrySet()) { p.setParameter(entry.getKey(), entry.getValue()); } method.setParams(p); } try { client.executeMethod(method); if (method.getStatusCode() == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader( new InputStreamReader(method.getResponseBodyAsStream(), charset)); String line; while ((line = reader.readLine()) != null) { if (pretty) { response.append(line).append(System.getProperty("line.separator")); } else { response.append(line); } } reader.close(); } } catch (IOException e) { log.error("HTTP Post" + url + "??", e); } finally { method.releaseConnection(); } return response.toString(); }
From source file:com.feilong.tools.net.httpclient3.HttpClientUtil.java
/** * ?log./*from ww w . ja v a 2 s . c om*/ * * @param httpMethod * the http method * @param httpClientConfig * the http client config * @return the http method response attribute map for log */ private static Map<String, Object> getHttpMethodResponseAttributeMapForLog(HttpMethod httpMethod, HttpClientConfig httpClientConfig) { Map<String, Object> map = new LinkedHashMap<String, Object>(); Object statusCode = null; try { statusCode = httpMethod.getStatusCode(); } catch (Exception e) { statusCode = e.getClass().getName() + " " + e.getMessage(); } String statusText = null; try { statusText = httpMethod.getStatusText(); } catch (Exception e) { statusText = e.getClass().getName() + " " + e.getMessage(); } map.put("httpMethod.getRequestHeaders()-->map", NameValuePairUtil.toMap(httpMethod.getRequestHeaders())); map.put("httpMethod.getStatusCode()", statusCode); map.put("httpMethod.getStatusText()", statusText); map.put("httpMethod.getStatusLine()", "" + httpMethod.getStatusLine()); map.put("httpMethod.getResponseHeaders()-->map", NameValuePairUtil.toMap(httpMethod.getResponseHeaders())); map.put("httpMethod.getResponseFooters()", httpMethod.getResponseFooters()); map.put("httpClientConfig", httpClientConfig); return map; }