List of usage examples for org.apache.http.client ClientProtocolException ClientProtocolException
public ClientProtocolException(final Throwable cause)
From source file:downloadwolkflow.httpTest.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from www . j av a 2s . c o m*/ HttpGet httpget = new HttpGet( "http://www.myexperiment.org/workflows/16/download/Pathways_and_Gene_annotations_for_QTL_region-v7.t2flow?version=7"); //System.out.println("Executing request " + httpget.getRequestLine()); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); } finally { httpclient.close(); } }
From source file:com.ny.apps.executor.httpclient.HttpRequestSender.java
public static void main(String[] args) throws ClientProtocolException, IOException { /*/*from w w w . j ava 2s . c o m*/ CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("localhost", 80), new UsernamePasswordCredentials("root", "123")); CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); */ CloseableHttpClient client = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("http://www.baidu.com/"); httpGet.setHeader("Authorization", ""); logger.info("executing request " + httpGet.getURI()); ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException { // TODO ? int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= 200 && statusCode < 300) { HttpEntity entity = response.getEntity(); return entity == null ? null : EntityUtils.toString(entity); } else { throw new ClientProtocolException("Unexpected response status: " + statusCode); } } }; try { String responseBody = client.execute(httpGet, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); System.out.println("----------------------------------------"); } finally { client.close(); } }
From source file:httpasync.ZeroCopyHttpExchange.java
public static void main(final String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try {/*from w ww .jav a 2 s . co m*/ httpclient.start(); File upload = new File(args[0]); File download = new File(args[1]); ZeroCopyPost httpost = new ZeroCopyPost("http://localhost:8080/", upload, ContentType.create("text/plain")); ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(download) { @Override protected File process(final HttpResponse response, final File file, final ContentType contentType) throws Exception { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new ClientProtocolException("Upload failed: " + response.getStatusLine()); } return file; } }; Future<File> future = httpclient.execute(httpost, consumer, null); File result = future.get(); System.out.println("Response file length: " + result.length()); System.out.println("Shutting down"); } finally { httpclient.close(); } System.out.println("Done"); }
From source file:com.boonya.http.async.examples.nio.client.ZeroCopyHttpExchange.java
public static void main(final String[] args) throws Exception { CloseableHttpAsyncClient httpclient = HttpAsyncClients.createDefault(); try {//from w ww .j a v a 2 s . co m httpclient.start(); File upload = new File(args[0]); File download = new File(args[1]); ZeroCopyPost httpost = new ZeroCopyPost("http://localhost:8080/", upload, ContentType.create("text/plain")); ZeroCopyConsumer<File> consumer = new ZeroCopyConsumer<File>(download) { @Override protected File process(final HttpResponse response, final File file, final ContentType contentType) throws Exception { if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new ClientProtocolException("Upload failed: " + response.getStatusLine()); } return file; } }; Future<File> future = httpclient.execute(httpost, consumer, null); File result = future.get(); System.out.println("Response file length: " + result.length()); System.out.println("Shutting down"); } finally { httpclient.close(); } System.out.println("Done"); }
From source file:com.lxf.spider.client.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {// w ww. java 2 s . c om HttpGet httpget = new HttpGet("http://localhost/"); System.out.println("Executing request " + httpget.getRequestLine()); // Create a custom response handler 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(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); } finally { httpclient.close(); } }
From source file:nl.knaw.dans.cmd2rdf.conversion.util.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {/*from w w w . j a v a 2 s . com*/ HttpGet httpget = new HttpGet("http://localhost/"); //System.out.println("Executing request " + httpget.getRequestLine()); // Create a custom response handler 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(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); //System.out.println("----------------------------------------"); //System.out.println(responseBody); } finally { httpclient.close(); } }
From source file:com.li.tools.httpclient.util.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {//from www . ja v a 2 s . c o m HttpGet httpget = new HttpGet("http://httpbin.org/"); System.out.println("Executing request " + httpget.getRequestLine()); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { @Override public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); } finally { httpclient.close(); } }
From source file:com.http.my.ClientFormLogin.java
public static void main(String[] args) throws Exception { ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); System.out.println(response.getStatusLine()); if (status >= 200 && status < 303) { HttpEntity entity = response.getEntity(); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); }//from w ww . ja va2 s . c o m } }; BasicCookieStore cookieStore = new BasicCookieStore(); // // BasicClientCookie cookieJSESSIONID = new BasicClientCookie("JSESSIONID", "3FD927DC6911B719E4492E7473897FBA"); // cookieJSESSIONID.setVersion(0); // cookieJSESSIONID.setDomain("218.75.79.230"); // cookieJSESSIONID.setPath("/"); // System.out.println("Initial set of cookies:"); // cookieStore.addCookie(cookieJSESSIONID); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build(); // HttpProtocolParams.setUserAgent(httpclient.getParams(), "Mozilla/5.0 (Windows; U; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)"); try { // HttpPost httpost = new HttpPost("http://localhost:8081/cring/jsp/user/userLogin.do?method=login"); HttpPost loginpost = new HttpPost("http://134.96.41.47/ecommunications_chs/start.swe"); //HttpGet loginGet = new HttpGet("http://134.96.41.47/ecommunications_chs/start.swe"); // HttpGet loginGet = new HttpGet("http://www.baidu.com"); List<NameValuePair> nvps = new ArrayList<NameValuePair>(); nvps.add(new BasicNameValuePair("SWEUserName", "nili")); nvps.add(new BasicNameValuePair("SWEPassword", "nili7788")); nvps.add(new BasicNameValuePair("SWEFo", "SWEEntryForm")); nvps.add(new BasicNameValuePair("SWENeedContext", "false")); nvps.add(new BasicNameValuePair("SWECmd", "ExecuteLogin")); nvps.add(new BasicNameValuePair("W", "t")); nvps.add(new BasicNameValuePair("SWEC", "0")); nvps.add(new BasicNameValuePair("SWEBID", "-1")); nvps.add(new BasicNameValuePair("SWETS", "1385712482625")); /** * SWEUserName:111111 SWEPassword:222222 SWEFo:SWEEntryForm SWENeedContext:false SWECmd:ExecuteLogin W:t SWESPNR: SWESPNH: SWEH: SWEC:0 SWEW: SWEBID:-1 SWETS:1385712482625 SWEWN: */ loginpost.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // CloseableHttpResponse response = httpclient.execute(loginpost); // HttpEntity httpEntity = response.getEntity(); CloseableHttpResponse responseGet = httpclient.execute(loginpost); HttpEntity httpEntityGet = responseGet.getEntity(); System.out.println("statusLine: " + responseGet.getStatusLine()); System.out.println("ContentType: " + httpEntityGet.getContentType()); System.out.println("ContentLength: " + httpEntityGet.getContentLength()); // httpEntityGet.get System.out.println("loginGet: " + EntityUtils.toString(httpEntityGet)); /* try { 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()); } } HttpPost modifypost = new HttpPost("http://localhost:8081/cring/jsp/user/corpUserController.do?method=modifyPwd"); List <NameValuePair> modifynvps = new ArrayList <NameValuePair>(); modifynvps.add(new BasicNameValuePair("newPwd","111111")); modifypost.setEntity(new UrlEncodedFormEntity(modifynvps, Consts.UTF_8)); CloseableHttpResponse modifyresponse = httpclient.execute(modifypost); System.out.println("modifyresponse : "+modifyresponse.getStatusLine()); } finally { //response2.close(); }*/ } finally { httpclient.close(); } }
From source file:interoperabilite.webservice.fluent.FluentResponseHandling.java
public static void main(String[] args) throws Exception { Document result = Request.Get("http://somehost/content").execute() .handleResponse(new ResponseHandler<Document>() { @Override//from ww w. j a v a 2s .c o m public Document handleResponse(final HttpResponse response) throws IOException { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { throw new HttpResponseException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } if (entity == null) { throw new ClientProtocolException("Response contains no content"); } DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); try { DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); ContentType contentType = ContentType.getOrDefault(entity); if (!contentType.equals(ContentType.APPLICATION_XML)) { throw new ClientProtocolException("Unexpected content type:" + contentType); } Charset charset = contentType.getCharset(); if (charset == null) { charset = Consts.ISO_8859_1; } return docBuilder.parse(entity.getContent(), charset.name()); } catch (ParserConfigurationException ex) { throw new IllegalStateException(ex); } catch (SAXException ex) { throw new ClientProtocolException("Malformed XML document", ex); } } }); // Do something useful with the result System.out.println(result); }
From source file:com.http.ClientWithResponseHandler.java
public final static void main(String[] args) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); try {//from w ww . j a v a 2s . c o m HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // Create a custom response handler ResponseHandler<String> responseHandler = new ResponseHandler<String>() { public String handleResponse(final HttpResponse response) throws ClientProtocolException, IOException { int status = response.getStatusLine().getStatusCode(); System.out.println(response.getStatusLine()); if (status >= 200 && status < 300) { HttpEntity entity = response.getEntity(); entity = new BufferedHttpEntity(entity); System.out.println("---1-----:" + EntityUtils.toString(entity)); System.out.println("---2-----:" + EntityUtils.toString(entity)); return entity != null ? EntityUtils.toString(entity) : null; } else { throw new ClientProtocolException("Unexpected response status: " + status); } } }; String responseBody = httpclient.execute(httpget, responseHandler); System.out.println("----------------------------------------"); System.out.println(responseBody); System.out.println("----------------------------------------"); } finally { httpclient.close(); } }