System.err.println("Method failed: " + method.getStatusLine());
System.err.println("Method failed: " + method.getStatusLine());
LOG.error("Method failed: " + getMethod.getStatusLine());
LOG.error("Method failed: " + getMethod.getStatusLine());
// Create an instance of HttpClient. HttpClient client = new HttpClient(); // Create a method instance. GetMethod method = new GetMethod(url); // Provide custom retry handler is necessary method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); try { // Execute the method. int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } // Read the response body. byte[] responseBody = method.getResponseBody(); // Deal with the response. // Use caution: ensure correct character encoding and is not binary data System.out.println(new String(responseBody)); } catch (HttpException e) { System.err.println("Fatal protocol violation: " + e.getMessage()); e.printStackTrace(); } catch (IOException e) { System.err.println("Fatal transport error: " + e.getMessage()); e.printStackTrace(); } finally { // Release the connection. method.releaseConnection(); }
// create SSL Context which trusts your self-signed certificate TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType()); keystore.load(ClassLoader.getSystemResourceAsStream("myKeystoreFile"), "password".toCharArray()); trustManagerFactory.init(keystore); TrustManager[] trustManagers = trustManagerFactory.getTrustManagers(); SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustManagers, null); // register your trusting SSL context Protocol.registerProtocol("https", new Protocol("https", (ProtocolSocketFactory) new SocketFactoryWrapper(sslContext.getSocketFactory()), 443)); // make the https call HttpClient httpclient = new HttpClient(); GetMethod httpget = new GetMethod("https://myendpoint.com"); httpclient.executeMethod(httpget); System.out.println(httpget.getStatusLine());
public BintrayPackage getPackageMetadata(String owner, String repo, String name){ String url = String.format("https://api.bintray.com/packages/%s/%s/%s", owner, repo, name); GetMethod get = new GetMethod(url); try { new HttpClient().executeMethod(get); if (get.getStatusLine() == null || get.getStatusCode() != 200) { throw new IOException(get.getStatusLine().toString()); } JsonParser parser = new JsonFactory().createParser(get.getResponseBodyAsStream()); parser.setCodec(new ObjectMapper()); return parser.readValueAs(BintrayPackage.class); } catch (IOException e) { if(get.getStatusLine() != null && get.getStatusCode() == 404){ return null; } else throw new RuntimeException(e); } }
GetMethod method = new GetMethod(resource_url); try { int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { logger.error("Get method failed: " + method.getStatusLine()); } org.apache.commons.io.FileUtils.copyInputStreamToFile( method.getResponseBodyAsStream(), new File(resource_file)); } catch (HttpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { method.releaseConnection(); }
public List<BintrayFile> getPackageFiles(String owner, String repo, String name, String version){ String url = String.format("https://api.bintray.com/packages/%s/%s/%s/versions/%s/files", owner, repo, name, version); GetMethod get = new GetMethod(url); try { new HttpClient().executeMethod(get); if (get.getStatusLine() == null || get.getStatusCode() != 200) { throw new IOException(get.getStatusLine().toString()); } ObjectMapper mapper = new ObjectMapper(); CollectionType fileListType = mapper.getTypeFactory().constructCollectionType(List.class, BintrayFile.class); List<BintrayFile> bintrayFiles = mapper.readValue(get.getResponseBodyAsStream(), fileListType); return bintrayFiles; } catch (IOException e) { if(get.getStatusLine() != null && get.getStatusCode() == 404){ throw new RuntimeException("Bintray package not found!", e); } else throw new RuntimeException(e); } }
public List<BintrayId> getAvailablePackages(String owner, String repo){ String url = String.format("https://api.bintray.com/repos/%s/%s/packages", owner, repo); GetMethod get = new GetMethod(url); try { new HttpClient().executeMethod(get); if (get.getStatusLine() == null || get.getStatusCode() != 200) { throw new IOException(get.getStatusLine().toString()); } ObjectMapper mapper = new ObjectMapper(); CollectionType idListType = mapper.getTypeFactory().constructCollectionType(List.class, BintrayId.class); List<BintrayId> owaIds = mapper.readValue(get.getResponseBodyAsStream(), idListType); return owaIds; } catch (IOException e) { //avoid NPE if(get.getStatusLine() != null && get.getStatusCode() == 404){ throw new RuntimeException("Bintray repository not found!", e); } else throw new RuntimeException(e); } }
HttpClient client = new HttpClient(); GetMethod method = new GetMethod("http://www.abc.com/file.xml"); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } byte[] responseBody = method.getResponseBody();
Protocol.registerProtocol("https", new Protocol("https", new MySSLSocketFactory(), 443)); HttpClient httpclient = new HttpClient(); GetMethod httpget = new GetMethod("https://www.whatever.com/"); try { httpclient.executeMethod(httpget); System.out.println(httpget.getStatusLine()); } finally { httpget.releaseConnection(); }
final HttpClient client = new HttpClient(); final GetMethod method = new GetMethod("http://structureddata.wikispaces.com/Test"); try { if (HttpStatus.SC_OK == client.executeMethod(method)) { System.out.println(IOUtils.toString(method.getResponseBodyAsStream())); } else { throw new IOException("Unable to load page, error " + method.getStatusLine()); } } finally { method.releaseConnection(); }
Protocol.registerProtocol("https", new Protocol("https", new AuthSSLProtocolSocketFactory(keystoreUrl, keystorePassword, truststoreUrl, truststorePassword), 443)); HttpClient httpclient = new HttpClient(); GetMethod httpget = new GetMethod("https://www.whatever.com/"); try { httpclient.executeMethod(httpget); System.out.println(httpget.getStatusLine()); } finally { httpget.releaseConnection(); }
Protocol myhttps = new Protocol("https", new MySSLSocketFactory(), 443); Protocol.registerProtocol("https", new Protocol("https", new MySSLSocketFactory(), 443)); HttpClient httpclient = new HttpClient(); GetMethod httpget = new GetMethod("https://www.whatever.com/"); try { httpclient.executeMethod(httpget); System.out.println(httpget.getStatusLine()); } finally { httpget.releaseConnection(); }
HttpClient httpclient = new HttpClient(); httpclient.getHostConfiguration().setProxy("myproxyhost", 8080); httpclient.getState().setProxyCredentials("my-proxy-realm", " myproxyhost", new UsernamePasswordCredentials("my-proxy-username", "my-proxy-password")); GetMethod httpget = new GetMethod("https://www.verisign.com/"); try { httpclient.executeMethod(httpget); System.out.println(httpget.getStatusLine()); } finally { httpget.releaseConnection(); }
HttpClient httpclient = new HttpClient(); httpclient.getHostConfiguration().setProxy("myproxyhost", 8080); httpclient.getState().setProxyCredentials("my-proxy-realm", " myproxyhost", new UsernamePasswordCredentials("my-proxy-username", "my-proxy-password")); GetMethod httpget = new GetMethod("https://www.verisign.com/"); try { httpclient.executeMethod(httpget); System.out.println(httpget.getStatusLine()); } finally { httpget.releaseConnection(); }
public String doGet(String url, String charset) { String resStr = null; HttpClient htpClient = new HttpClient(); GetMethod getMethod = new GetMethod(url); getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); try { int statusCode = htpClient.executeMethod(getMethod); // log.info(statusCode); if (statusCode != HttpStatus.SC_OK) { log.error("Method failed: " + getMethod.getStatusLine()); return resStr; } byte[] responseBody = getMethod.getResponseBody(); resStr = new String(responseBody, charset); } catch (HttpException e) { log.error("Please check your provided http address!"); // 发生致命的异常,可能是协议不对或者返回的内容有问题 } catch (IOException e) { log.error("Network anomaly"); // 发生网络异常 } finally { getMethod.releaseConnection(); // 释放连接 } return resStr; }
/** * Get InputStream for the GET method for the given path. * * @param path the server relative path of the resource to get * @return InputStream * @exception HttpException * @exception IOException */ public InputStream getMethodData(String path) throws HttpException, IOException { setClient(); GetMethod method = new GetMethod(URIUtil.encodePathQuery(path)); generateTransactionHeader(method); client.executeMethod(method); int statusCode = method.getStatusLine().getStatusCode(); setStatusCode(statusCode); if(statusCode >= 200 && statusCode < 300) return method.getResponseBodyAsStream(); else throw new IOException("Couldn't get file"); }
LOGGER.error("Method failed: " + get.getStatusLine());