List of usage examples for org.apache.http.client.methods HttpUriRequest getRequestLine
RequestLine getRequestLine();
From source file:cn.jumper.study.http.ClientFormLogin.java
public static void main(String[] args) throws Exception { BasicCookieStore cookieStore = new BasicCookieStore(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); HttpHost proxy = new HttpHost("192.168.10.3", 8080, "http"); RequestConfig config = RequestConfig.custom().setProxy(proxy).build(); try {/* ww w . ja v a2 s .c om*/ HttpGet httpget = new HttpGet("http://www.ksf-food.com/admin/Login.asp"); httpget.setConfig(config); CloseableHttpResponse response1 = httpclient.execute(httpget); try { HttpEntity entity = response1.getEntity(); System.out.println("Login form get: " + response1.getStatusLine()); EntityUtils.consume(entity); System.out.println("Initial set of cookies:"); List<Cookie> cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { response1.close(); } String code = ""; try { HttpUriRequest httpgetCode = RequestBuilder.get() .setUri("http://www.ksf-food.com/admin/inc/checkcode.asp").setConfig(config).build(); /* * HttpGet httpgetCode = new HttpGet( * "http://www.qufuev.com/admin/inc/checkcode.asp"); * httpgetCode.setConfig(config); */ System.out.println("Executing request " + httpgetCode.getRequestLine()); System.out.println("========================================================"); System.out.println("==httpget header =="); for (Header header : httpgetCode.getAllHeaders()) { System.out.println(header.getName() + ":" + header.getValue()); } System.out.println("==httpget header =="); ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); System.out.println("==respons header =="); for (Header header : response.getAllHeaders()) { System.out.println(header.getName() + ":" + header.getValue()); } System.out.println("==respons header =="); String fileName = System.currentTimeMillis() + ""; DataOutputStream dataOutputStream = new DataOutputStream( new FileOutputStream("d://test//e3//" + fileName + ".jpg")); dataOutputStream.write(EntityUtils.toByteArray(entity)); dataOutputStream.close(); return ImageTest.getAllOcr("d://test//e3//" + fileName + ".jpg"); } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; code = httpclient.execute(httpgetCode, responseHandler); System.out.println("ClientFormLogin.main()-CheckCode:" + code); System.out.println("----------------------------------------"); } catch (IOException e) { e.printStackTrace(); } HttpUriRequest login = RequestBuilder.post() .setUri(new URI("http://www.ksf-food.com/admin/Admin_ChkLogin.asp")) .addParameter("UserName", "username").addParameter("Password", "password") .addParameter("CheckCode", code).setConfig(config).build(); System.out.println("========================================================"); System.out.println("==httpget header =="); for (Header header : login.getAllHeaders()) { System.out.println(header.getName() + ":" + header.getValue()); } CloseableHttpResponse response2 = httpclient.execute(login); try { HttpEntity entity = response2.getEntity(); System.out.println("Login form post: " + response2.getStatusLine()); // EntityUtils.consume(entity); System.out.println("ClientFormLogin.main():\\n" + EntityUtils.toString(entity, "GBK")); System.out.println("Post logon cookies:"); List<Cookie> cookies = cookieStore.getCookies(); if (cookies.isEmpty()) { System.out.println("None"); } else { for (int i = 0; i < cookies.size(); i++) { System.out.println("- " + cookies.get(i).toString()); } } } finally { response2.close(); } } finally { httpclient.close(); } }
From source file:org.elasticsearch.client.RequestLogger.java
/** * Creates curl output for given request *//*w ww .ja v a 2 s . c o m*/ static String buildTraceRequest(HttpUriRequest request, HttpHost host) throws IOException { String requestLine = "curl -iX " + request.getMethod() + " '" + host + getUri(request.getRequestLine()) + "'"; if (request instanceof HttpEntityEnclosingRequest) { HttpEntityEnclosingRequest enclosingRequest = (HttpEntityEnclosingRequest) request; if (enclosingRequest.getEntity() != null) { requestLine += " -d '"; HttpEntity entity = enclosingRequest.getEntity(); if (entity.isRepeatable() == false) { entity = new BufferedHttpEntity(enclosingRequest.getEntity()); enclosingRequest.setEntity(entity); } requestLine += EntityUtils.toString(entity, StandardCharsets.UTF_8) + "'"; } } return requestLine; }
From source file:org.elasticsearch.client.RequestLogger.java
/** * Logs a request that failed/*from w w w .j a v a 2s. c o m*/ */ static void logFailedRequest(Log logger, HttpUriRequest request, HttpHost host, Exception e) { if (logger.isDebugEnabled()) { logger.debug( "request [" + request.getMethod() + " " + host + getUri(request.getRequestLine()) + "] failed", e); } if (tracer.isTraceEnabled()) { String traceRequest; try { traceRequest = buildTraceRequest(request, host); } catch (IOException e1) { tracer.trace("error while reading request for trace purposes", e); traceRequest = ""; } tracer.trace(traceRequest); } }
From source file:org.elasticsearch.client.RequestLogger.java
/** * Logs a request that yielded a response *///from ww w . j a v a2 s .c o m static void logResponse(Log logger, HttpUriRequest request, HttpHost host, HttpResponse httpResponse) { if (logger.isDebugEnabled()) { logger.debug("request [" + request.getMethod() + " " + host + getUri(request.getRequestLine()) + "] returned [" + httpResponse.getStatusLine() + "]"); } if (logger.isWarnEnabled()) { Header[] warnings = httpResponse.getHeaders("Warning"); if (warnings != null && warnings.length > 0) { logger.warn(buildWarningMessage(request, host, warnings)); } } if (tracer.isTraceEnabled()) { String requestLine; try { requestLine = buildTraceRequest(request, host); } catch (IOException e) { requestLine = ""; tracer.trace("error while reading request for trace purposes", e); } String responseLine; try { responseLine = buildTraceResponse(httpResponse); } catch (IOException e) { responseLine = ""; tracer.trace("error while reading response for trace purposes", e); } tracer.trace(requestLine + '\n' + responseLine); } }
From source file:org.elasticsearch.client.RequestLogger.java
static String buildWarningMessage(HttpUriRequest request, HttpHost host, Header[] warnings) { StringBuilder message = new StringBuilder("request [").append(request.getMethod()).append(" ").append(host) .append(getUri(request.getRequestLine())).append("] returned ").append(warnings.length) .append(" warnings: "); for (int i = 0; i < warnings.length; i++) { if (i > 0) { message.append(","); }//w w w. j a v a 2 s .co m message.append("[").append(warnings[i].getValue()).append("]"); } return message.toString(); }
From source file:spark.protocol.SparqlCall.java
/** Logs a request, executes it, and dumps the response to the logger. For development use only. */ @SuppressWarnings("unused") private static final void dump(HttpClient client, HttpUriRequest req) { if (logger.isTraceEnabled()) { StringBuilder sb = new StringBuilder("\n=== Request ==="); sb.append("\n").append(req.getRequestLine()); for (Header h : req.getAllHeaders()) { sb.append("\n").append(h.getName()).append(": ").append(h.getValue()); }/* w w w .j a v a 2s . c o m*/ logger.trace(sb.toString()); HttpResponse resp = null; try { resp = client.execute(req); } catch (Exception e) { logger.trace("Error executing request", e); return; } sb = new StringBuilder("\n=== Response ==="); sb.append("\n").append(resp.getStatusLine()); for (Header h : resp.getAllHeaders()) { sb.append("\n").append(h.getName()).append(": ").append(h.getValue()); } logger.trace(sb.toString()); HttpEntity entity = resp.getEntity(); if (entity != null) { sb = new StringBuilder("\n=== Content ==="); try { int len = (int) entity.getContentLength(); if (len < 0) len = 100; ByteArrayOutputStream baos = new ByteArrayOutputStream(len); entity.writeTo(baos); sb.append("\n").append(baos.toString("UTF-8")); logger.trace(sb.toString()); } catch (IOException e) { logger.trace("Error reading content", e); } } } }
From source file:org.jclouds.http.apachehc.ApacheHCHttpCommandExecutorService.java
private org.apache.http.HttpResponse executeRequest(HttpUriRequest nativeRequest) throws IOException, ClientProtocolException { URI endpoint = URI.create(nativeRequest.getRequestLine().getUri()); HttpHost host = new HttpHost(endpoint.getHost(), endpoint.getPort(), endpoint.getScheme()); org.apache.http.HttpResponse nativeResponse = client.execute(host, nativeRequest); return nativeResponse; }
From source file:org.esigate.http.RedirectStrategy.java
@Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { HttpUriRequest newRequest = super.getRedirect(request, response, context); context.setAttribute(LAST_REQUEST, newRequest); if (LOG.isInfoEnabled()) { LOG.info(request.getRequestLine() + " -> " + response.getStatusLine() + " -> automaticaly following redirect to " + newRequest.getRequestLine()); }/*from ww w. j a v a 2 s. c o m*/ return newRequest; }
From source file:io.ucoin.ucoinj.core.client.service.HttpServiceImpl.java
protected boolean executeRequest(HttpClient httpClient, HttpUriRequest request) { if (log.isDebugEnabled()) { log.debug("Executing request : " + request.getRequestLine()); }/* w w w. ja va 2s. c o m*/ try { HttpResponse response = httpClient.execute(request); switch (response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: { response.getEntity().consumeContent(); return true; } case HttpStatus.SC_UNAUTHORIZED: case HttpStatus.SC_FORBIDDEN: throw new TechnicalException("ucoin.client.authentication"); default: throw new TechnicalException("ucoin.client.status" + response.getStatusLine().toString()); } } catch (ConnectException e) { throw new TechnicalException("ucoin.client.core.connect", e); } catch (IOException e) { throw new TechnicalException(e.getMessage(), e); } }
From source file:com.infine.android.devoxx.io.json.RemoteJsonExecutor.java
private void execute(HttpUriRequest request, JsonHandler handler) throws HandlerException { try {/*from ww w . j a va 2 s .c om*/ final HttpResponse resp = mHttpClient.execute(request); final int status = resp.getStatusLine().getStatusCode(); if (status != HttpStatus.SC_OK) { throw new HandlerException( "Unexpected server response " + resp.getStatusLine() + " for " + request.getRequestLine()); } final InputStream input = resp.getEntity().getContent(); try { handler.parseAndApply(input, mResolver); } finally { if (input != null) input.close(); } } catch (HandlerException e) { throw e; } catch (IOException e) { throw new HandlerException("Problem reading remote response for " + request.getRequestLine(), e); } }