List of usage examples for org.apache.commons.httpclient.methods GetMethod GetMethod
public GetMethod(String uri)
From source file:InteractiveAuthenticationExample.java
private void doDemo() throws IOException { HttpClient client = new HttpClient(); client.getParams().setParameter(CredentialsProvider.PROVIDER, new ConsoleAuthPrompter()); GetMethod httpget = new GetMethod("http://target-host/requires-auth.html"); httpget.setDoAuthentication(true);//from w w w . j ava2 s .c o m try { // execute the GET int status = client.executeMethod(httpget); // print the status and response System.out.println(httpget.getStatusLine().toString()); System.out.println(httpget.getResponseBodyAsString()); } finally { // release any connection resources used by the method httpget.releaseConnection(); } }
From source file:com.gagein.crawler.FileDownLoader.java
public String downloadLink(String url, HtmlFileBean hfb) { /* 1.? HttpClinet ? */ HttpClient httpClient = new HttpClient(); // Http 5s// w w w . j a v a2 s . c o m httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000); /* 2.? GetMethod ? */ GetMethod getMethod = new GetMethod(url); // get 5s getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000); // ?? getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler()); /* 3. HTTP GET */ try { int statusCode = httpClient.executeMethod(getMethod); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + getMethod.getStatusLine()); } /* 4.? HTTP ? */ byte[] responseBody = getMethod.getResponseBody();// ? // ? url ???? url = FileUtils.getFileNameByUrl(url); saveToLocal(responseBody, hfb.getDirPath() + File.separator + url); } catch (HttpException e) { // ????? logger.debug("Please check your provided http " + "address!"); e.printStackTrace(); } catch (IOException e) { // ? logger.debug("?"); e.printStackTrace(); } finally { // logger.debug("=======================" + url + "=============?"); getMethod.releaseConnection(); } return hfb.getDirPath() + url; }
From source file:ch.algotrader.starter.GoogleDailyDownloader.java
private void retrieve(HttpClient httpclient, String symbol, String startDate, String endDate, String exchange) throws IOException, HttpException, FileNotFoundException, ParseException { GetMethod fileGet = new GetMethod("https://www.google.com/finance/historical?q=" + exchange + ":" + symbol + "&output=csv&startdate=" + startDate + (endDate == null ? "" : "&endDate=" + endDate)); fileGet.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); try {/*from ww w. j a va 2 s .c om*/ int status = httpclient.executeMethod(fileGet); if (status == HttpStatus.SC_OK) { BufferedReader reader = new BufferedReader( new InputStreamReader(fileGet.getResponseBodyAsStream())); File parent = new File("files" + File.separator + "google"); if (!parent.exists()) { FileUtils.forceMkdir(parent); } Writer writer = new OutputStreamWriter(new FileOutputStream(new File(parent, symbol + ".csv"))); try { reader.readLine(); String line; List<String> lines = new ArrayList<String>(); while ((line = reader.readLine()) != null) { String tokens[] = line.split(","); Date dateTime = fileFormat.parse(tokens[0]); StringBuffer buffer = new StringBuffer(); buffer.append( DateTimePatterns.LOCAL_DATE_TIME.format(DateTimeLegacy.toLocalDateTime(dateTime))); buffer.append(","); buffer.append(tokens[1].equals("-") ? "" : tokens[1]); buffer.append(","); buffer.append(tokens[2].equals("-") ? "" : tokens[2]); buffer.append(","); buffer.append(tokens[3].equals("-") ? "" : tokens[3]); buffer.append(","); buffer.append(tokens[4].equals("-") ? "" : tokens[4]); buffer.append(","); buffer.append(tokens[5]); buffer.append("\n"); lines.add(buffer.toString()); } writer.write("dateTime,open,high,low,close,vol\n"); // write in reverse order for (int i = lines.size() - 1; i > 0; i--) { writer.append(lines.get(i)); } } finally { reader.close(); writer.close(); } } } finally { fileGet.releaseConnection(); } }
From source file:com.bbva.arq.devops.ae.mirrorgate.jenkins.plugin.utils.RestCall.java
public MirrorGateResponse makeRestCallGet(String url, String user, String password) { MirrorGateResponse response;/* w w w. ja va 2 s. c om*/ GetMethod get = new GetMethod(url); try { HttpClient client = getHttpClient(); if (user != null && password != null) { client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password)); get.setDoAuthentication(true); } get.getParams().setContentCharset("UTF-8"); int responseCode = client.executeMethod(get); String responseString = get.getResponseBodyAsStream() != null ? getResponseString(get.getResponseBodyAsStream()) : ""; response = new MirrorGateResponse(responseCode, responseString); } catch (HttpException e) { LOGGER.log(Level.WARNING, "Error connecting to MirrorGate", e); response = new MirrorGateResponse(HttpStatus.SC_BAD_REQUEST, ""); } catch (IOException e) { LOGGER.log(Level.WARNING, "Error connecting to MirrorGate", e); response = new MirrorGateResponse(HttpStatus.SC_BAD_REQUEST, ""); } finally { get.releaseConnection(); } return response; }
From source file:com.sittinglittleduck.DirBuster.GenBaseCase.java
/** * Generates the base case//from w w w .j a v a2 s.co m * * @param manager the manager class * @param url The directy or file we need a base case for * @param isDir true if it's dir, else false if it's a file * @param fileExtention File extention to be scanned, set to null if it's a dir that is to be * tested * @return A BaseCase Object */ public static BaseCase genBaseCase(Manager manager, String url, boolean isDir, String fileExtention) throws MalformedURLException, IOException { String type; if (isDir) { type = "Dir"; } else { type = "File"; } /* * markers for using regex instead */ boolean useRegexInstead = false; String regex = null; BaseCase tempBaseCase = manager.getBaseCase(url, isDir, fileExtention); if (tempBaseCase != null) { // System.out.println("Using prestored basecase"); return tempBaseCase; } // System.out.println("DEBUG GenBaseCase: URL to get baseCase for: " + url + " (" + type + // ")"); if (Config.debug) { System.out.println("DEBUG GenBaseCase: URL to get baseCase for:" + url); } BaseCase baseCase = null; int failcode = 0; String failString = Config.failCaseString; String baseResponce = ""; URL failurl = null; if (isDir) { failurl = new URL(url + failString + "/"); } else { if (manager.isBlankExt()) { fileExtention = ""; failurl = new URL(url + failString + fileExtention); } else { if (!fileExtention.startsWith(".")) { fileExtention = "." + fileExtention; } failurl = new URL(url + failString + fileExtention); } } // System.out.println("DEBUG GenBaseCase: Getting :" + failurl + " (" + type + ")"); if (Config.debug) { System.out.println("DEBUG GenBaseCase: Getting:" + failurl); } GetMethod httpget = new GetMethod(failurl.toString()); // set the custom HTTP headers Vector HTTPheaders = manager.getHTTPHeaders(); for (int a = 0; a < HTTPheaders.size(); a++) { HTTPHeader httpHeader = (HTTPHeader) HTTPheaders.elementAt(a); /* * Host header has to be set in a different way! */ if (httpHeader.getHeader().startsWith("Host")) { httpget.getParams().setVirtualHost(httpHeader.getValue()); } else { httpget.setRequestHeader(httpHeader.getHeader(), httpHeader.getValue()); } } httpget.setFollowRedirects(Config.followRedirects); // save the http responce code for the base case failcode = manager.getHttpclient().executeMethod(httpget); manager.workDone(); // we now need to get the content as we need a base case! if (failcode == 200) { if (Config.debug) { System.out.println("DEBUG GenBaseCase: base case for " + failurl.toString() + "came back as 200!"); } BufferedReader input = new BufferedReader(new InputStreamReader(httpget.getResponseBodyAsStream())); String tempLine; StringBuffer buf = new StringBuffer(); while ((tempLine = input.readLine()) != null) { buf.append("\r\n" + tempLine); } baseResponce = buf.toString(); input.close(); // HTMLparse.parseHTML(); // HTMLparse htmlParse = new HTMLparse(baseResponce, null); // Thread parse = new Thread(htmlParse); // parse.start(); // clean up the base case, based on the basecase URL baseResponce = FilterResponce.CleanResponce(baseResponce, failurl, failString); httpget.releaseConnection(); /* * get the base case twice more, for consisitency checking */ String baseResponce1 = baseResponce; String baseResponce2 = getBaseCaseAgain(manager, failurl, failString); String baseResponce3 = getBaseCaseAgain(manager, failurl, failString); if (baseResponce1 != null && baseResponce2 != null && baseResponce3 != null) { /* * check that all the responces are same, if they are do nothing if not enter the if statement */ if (!baseResponce1.equalsIgnoreCase(baseResponce2) || !baseResponce1.equalsIgnoreCase(baseResponce3) || !baseResponce2.equalsIgnoreCase(baseResponce3)) { if (manager.getFailCaseRegexes().size() != 0) { /* * for each saved regex see if it will work, if it does then use that one * if not then give the uses the dialog */ Vector<String> failCaseRegexes = manager.getFailCaseRegexes(); for (int a = 0; a < failCaseRegexes.size(); a++) { Pattern regexFindFile = Pattern.compile(failCaseRegexes.elementAt(a)); Matcher m1 = regexFindFile.matcher(baseResponce1); Matcher m2 = regexFindFile.matcher(baseResponce2); Matcher m3 = regexFindFile.matcher(baseResponce3); boolean test1 = m1.find(); boolean test2 = m2.find(); boolean test3 = m3.find(); if (test1 && test2 && test3) { regex = failCaseRegexes.elementAt(a); useRegexInstead = true; break; } } } } else { /* * We have a big problem as now we have different responce codes for the same request * //TODO think of a way to deal with is */ } if (Config.debug) { System.out.println("DEBUG GenBaseCase: base case was set to :" + baseResponce); } } } httpget.releaseConnection(); baseCase = new BaseCase(new URL(url), failcode, isDir, failurl, baseResponce, fileExtention, useRegexInstead, regex); // add the new base case to the manager list manager.addBaseCase(baseCase); manager.addNumberOfBaseCasesProduced(); return baseCase; }
From source file:com.unicauca.braim.http.HttpBraimClient.java
public Song[] GET_Songs(int page, String access_token, JButton bt_next_list, JButton bt_previous_list) throws IOException { if (page == 1) { bt_previous_list.setEnabled(false); } else {/* www .j a v a2s .c o m*/ bt_previous_list.setEnabled(true); } String token = "braim_token=" + access_token; String data = "page=" + page + "&per_page=10"; GetMethod method = new GetMethod(api_url + "/api/v1/songs?" + token + "&" + data); Song[] songList = null; Gson gson = new Gson(); try { method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); } byte[] responseBody = method.getResponseBody(); Integer total_pages = Integer.parseInt(method.getResponseHeader("total_pages").getValue()); System.out.println("TOTAL SONG PAGES= " + method.getResponseHeader("total_pages")); String response = new String(responseBody, "UTF-8"); songList = gson.fromJson(response, Song[].class); if (page == total_pages) { bt_next_list.setEnabled(false); } else { bt_next_list.setEnabled(true); } } catch (UnsupportedEncodingException ex) { Logger.getLogger(HttpBraimClient.class.getName()).log(Level.SEVERE, null, ex); } return songList; }
From source file:net.adamcin.httpsig.http.apache3.Http3UtilTest.java
@Test public void testLogin() { TestBody.test(new HttpServerTestBody() { @Override// www . j a va 2s. c o m protected void execute() throws Exception { List<String> headers = Arrays.asList(Constants.HEADER_REQUEST_TARGET, Constants.HEADER_DATE); setServlet(new AdminServlet(headers)); KeyPair keyPair = KeyTestUtil.getKeyPairFromProperties("b2048", "id_rsa"); DefaultKeychain provider = new DefaultKeychain(); provider.add(new SSHKey(KeyFormat.SSH_RSA, keyPair)); HttpClient client = new HttpClient(); Http3Util.enableAuth(client, provider, getKeyId()); HttpMethod request = new GetMethod(getAbsoluteUrl(TEST_URL)); try { int status = client.executeMethod(request); assertEquals("should return 200", 200, status); } finally { request.releaseConnection(); } } }); }
From source file:fr.msch.wissl.server.TestLogin.java
@Test public void test() throws IOException, JSONException { HttpClient client = new HttpClient(); // good username, bad password: error 401 PostMethod post = new PostMethod(TServer.URL + "login"); post.addParameter("username", admin_username); post.addParameter("password", "bad_password"); client.executeMethod(post);// www . j a v a 2 s.c o m post.getResponseBodyAsString(); Assert.assertEquals(401, post.getStatusCode()); // empty password and username: error 401 post = new PostMethod(TServer.URL + "login"); post.addParameter("username", ""); post.addParameter("password", ""); client.executeMethod(post); post.getResponseBodyAsString(); Assert.assertEquals(401, post.getStatusCode()); // log in as admin post = new PostMethod(TServer.URL + "login"); post.addParameter("username", admin_username); post.addParameter("password", admin_password); client.executeMethod(post); String ret = post.getResponseBodyAsString(); JSONObject obj = new JSONObject(ret); int uid_admin = obj.getInt("userId"); String sid_admin = obj.getString("sessionId"); int auth = obj.getInt("auth"); Assert.assertEquals(200, post.getStatusCode()); Assert.assertEquals(uid_admin, this.admin_userId); Assert.assertNotNull(UUID.fromString(sid_admin)); Assert.assertEquals(auth, 1); // call 'stats' with this session, should succeed GetMethod get = new GetMethod(TServer.URL + "stats"); get.addRequestHeader("sessionId", sid_admin); client.executeMethod(get); Assert.assertEquals(200, get.getStatusCode()); // the previous session that was setup by the test case // for this user should NOT have been destroyed // both sessions are kept for the same user get = new GetMethod(TServer.URL + "stats"); get.addRequestHeader("sessionId", this.admin_sessionId); client.executeMethod(get); Assert.assertEquals(200, get.getStatusCode()); // the other user set up by the test case should still be logged in get = new GetMethod(TServer.URL + "stats"); get.addRequestHeader("sessionId", this.user_sessionId); client.executeMethod(get); Assert.assertEquals(200, get.getStatusCode()); // logout all users post = new PostMethod(TServer.URL + "logout"); post.addRequestHeader("sessionId", sid_admin); client.executeMethod(post); Assert.assertEquals(204, post.getStatusCode()); post = new PostMethod(TServer.URL + "logout"); post.addRequestHeader("sessionId", this.user_sessionId); client.executeMethod(post); Assert.assertEquals(204, post.getStatusCode()); post = new PostMethod(TServer.URL + "logout"); post.addRequestHeader("sessionId", this.admin_sessionId); client.executeMethod(post); Assert.assertEquals(204, post.getStatusCode()); // check that neither client can call 'stats' get = new GetMethod(TServer.URL + "stats"); get.addRequestHeader("sessionId", sid_admin); client.executeMethod(get); Assert.assertEquals(401, get.getStatusCode()); get = new GetMethod(TServer.URL + "stats"); get.addRequestHeader("sessionId", this.user_sessionId); client.executeMethod(get); Assert.assertEquals(401, get.getStatusCode()); }
From source file:at.ait.dme.yuma.suite.apps.map.server.tileset.TilesetGenerator.java
private String downloadImage(String dir, String url) throws TilingException { InputStream is = null;/*w w w . java 2 s .c om*/ OutputStream os = null; try { GetMethod getImageMethod = new GetMethod(url); int statusCode = new HttpClient().executeMethod(getImageMethod); if (statusCode != HttpStatus.SC_OK) throw new TilingException("GET " + url + " returned status code:" + statusCode); is = getImageMethod.getResponseBodyAsStream(); File imageFile = new File(dir + "/" + url.substring(url.lastIndexOf("/")).replace("%", "-")); os = new FileOutputStream(imageFile); byte buf[] = new byte[1024]; int len; while ((len = is.read(buf)) > 0) os.write(buf, 0, len); return imageFile.getAbsolutePath(); } catch (Throwable t) { logger.error("failed to store image from url:" + url, t); throw new TilingException(t); } finally { try { if (os != null) os.close(); if (is != null) is.close(); } catch (IOException e) { logger.error("failed to close streams"); } } }
From source file:jshm.sh.client.HttpForm.java
/** * // w w w. j av a 2s.com * @param method Is an {@link Object} for the purpose of overloading the constructor * @param url * @param data An array of Strings with alternating keys and values */ public HttpForm(final String method, final Object url, final String... data) { if ((data.length & 1) != 0) throw new IllegalArgumentException("data must have an even number of values"); if (!(url instanceof String)) throw new IllegalArgumentException("url must be a String"); this.url = (String) url; this.data = new NameValuePair[data.length / 2]; // this.data[0] = data[0], data[1] // this.data[1] = data[2], data[3] // this.data[2] = data[4], data[5] for (int i = 0; i < data.length; i += 2) { this.data[i / 2] = new NameValuePair(data[i], data[i + 1]); } this.methodName = method; if ("POST".equalsIgnoreCase(method.toString())) { PostMethod postMethod = new PostMethod(this.url); postMethod.setRequestBody(this.data); this.method = postMethod; } else if ("GET".equalsIgnoreCase(method.toString())) { GetMethod getMethod = new GetMethod(this.url); getMethod.setQueryString(this.data); this.method = getMethod; } else { throw new IllegalArgumentException("method must be POST or GET, given: " + method); } }