List of usage examples for java.net HttpURLConnection setInstanceFollowRedirects
public void setInstanceFollowRedirects(boolean followRedirects)
From source file:forge.download.GuiDownloadService.java
@Override public void run() { Proxy p = getProxy();/* w w w . jav a2 s . c om*/ int bufferLength; int iCard = 0; byte[] buffer = new byte[1024]; for (Entry<String, String> kv : files.entrySet()) { if (cancel) { break; } String url = kv.getValue(); final File fileDest = new File(kv.getKey()); final File base = fileDest.getParentFile(); FileOutputStream fos = null; try { // test for folder existence if (!base.exists() && !base.mkdir()) { // create folder if not found System.out.println("Can't create folder" + base.getAbsolutePath()); } URL imageUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(p); // don't allow redirections here -- they indicate 'file not found' on the server conn.setInstanceFollowRedirects(false); conn.connect(); if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) { conn.disconnect(); System.out.println(url); System.out.println("Skipped Download for: " + fileDest.getPath()); update(++iCard, fileDest); skipped++; continue; } fos = new FileOutputStream(fileDest); InputStream inputStream = conn.getInputStream(); while ((bufferLength = inputStream.read(buffer)) > 0) { fos.write(buffer, 0, bufferLength); } } catch (final ConnectException ce) { System.out.println("Connection refused for url: " + url); } catch (final MalformedURLException mURLe) { System.out.println("Error - possibly missing URL for: " + fileDest.getName()); } catch (final FileNotFoundException fnfe) { String formatStr = "Error - the LQ picture %s could not be found on the server. [%s] - %s"; System.out.println(String.format(formatStr, fileDest.getName(), url, fnfe.getMessage())); } catch (final Exception ex) { Log.error("LQ Pictures", "Error downloading pictures", ex); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { System.out.println("error closing output stream"); } } } update(++iCard, fileDest); } }
From source file:com.amastigote.xdu.query.module.WaterAndElectricity.java
private Document getPage(String output_data, String host_suffix) throws IOException { URL url = new URL(HOST + host_suffix); HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.setDoOutput(true); httpURLConnection.setRequestMethod("POST"); httpURLConnection.setUseCaches(false); httpURLConnection.setInstanceFollowRedirects(false); httpURLConnection.setRequestProperty("Cookie", "ASP.NET_SessionId=" + ASP_dot_NET_SessionId); httpURLConnection.connect();/* www . java 2s . c o m*/ OutputStreamWriter outputStreamWriter = new OutputStreamWriter(httpURLConnection.getOutputStream(), "UTF-8"); outputStreamWriter.write(output_data); outputStreamWriter.flush(); outputStreamWriter.close(); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(httpURLConnection.getInputStream())); String temp; String htmlPage = ""; while ((temp = bufferedReader.readLine()) != null) htmlPage += temp; bufferedReader.close(); httpURLConnection.disconnect(); htmlPage = htmlPage.replaceAll(" ", " "); return Jsoup.parse(htmlPage); }
From source file:com.wyp.module.controller.LicenseController.java
/** * wslicense//from ww w. j a v a 2 s. c o m * * @param properties * @return */ private String getCitrixLicense(String requestJson) { String licenses = ""; OutputStreamWriter out = null; InputStream is = null; long start = System.currentTimeMillis(); try { // ws url URL url = new URL(map.get("address")); // ws connection HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); connection.setInstanceFollowRedirects(true); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", " application/json"); connection.setRequestMethod("POST"); connection.connect(); out = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); out.append(requestJson); out.flush(); // response string is = connection.getInputStream(); int length = is.available(); if (0 < length) { long end = System.currentTimeMillis(); System.out.println("?" + (end - start)); BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuilder tempStr = new StringBuilder(); String temp = ""; while ((temp = reader.readLine()) != null) { tempStr.append(temp); } licenses = tempStr.toString(); // utf-8? System.out.println(licenses); } } catch (IOException e) { String eMsg = e.getMessage(); if ("Read timed out".equals(eMsg) || "Connection timed out: connect".equals(eMsg) || "Software caused connection abort: recv failed".equals(eMsg)) { long end = System.currentTimeMillis(); System.out.println(eMsg + (end - start)); licenses = "TIMEOUT"; } e.printStackTrace(); } finally { try { if (out != null) { out.close(); } if (is != null) { is.close(); } } catch (IOException ex) { ex.printStackTrace(); } } System.out.println(licenses); return licenses; }
From source file:com.adaptris.core.http.JdkHttpProducer.java
@Override protected AdaptrisMessage doRequest(AdaptrisMessage msg, ProduceDestination destination, long timeout) throws ProduceException { AdaptrisMessage reply = defaultIfNull(getMessageFactory()).newMessage(); ThreadLocalCredentials threadLocalCreds = null; try {// ww w .j a v a 2 s . c o m URL url = new URL(destination.getDestination(msg)); if (getPasswordAuthentication() != null) { Authenticator.setDefault(AdapterResourceAuthenticator.getInstance()); threadLocalCreds = ThreadLocalCredentials.getInstance(url.toString()); threadLocalCreds.setThreadCredentials(getPasswordAuthentication()); AdapterResourceAuthenticator.getInstance().addAuthenticator(threadLocalCreds); } HttpURLConnection http = (HttpURLConnection) url.openConnection(); http.setRequestMethod(methodToUse(msg).name()); http.setInstanceFollowRedirects(handleRedirection()); http.setDoInput(true); http.setConnectTimeout(Long.valueOf(timeout).intValue()); http.setReadTimeout(Long.valueOf(timeout).intValue()); // ProxyUtil.applyBasicProxyAuthorisation(http); addHeaders(msg, http); if (getContentTypeKey() != null && msg.containsKey(getContentTypeKey())) { http.setRequestProperty(CONTENT_TYPE, msg.getMetadataValue(getContentTypeKey())); } // if (getAuthorisation() != null) { // http.setRequestProperty(AUTHORIZATION, getAuthorisation()); // } // logHeaders("Request Information", "Request Method : " + http.getRequestMethod(), http.getRequestProperties().entrySet()); sendMessage(msg, http); readResponse(http, reply); // logHeaders("Response Information", http.getResponseMessage(), http.getHeaderFields().entrySet()); } catch (IOException e) { throw new ProduceException(e); } catch (CoreException e) { if (e instanceof ProduceException) { throw (ProduceException) e; } else { throw new ProduceException(e); } } finally { if (threadLocalCreds != null) { threadLocalCreds.removeThreadCredentials(); AdapterResourceAuthenticator.getInstance().removeAuthenticator(threadLocalCreds); } } return reply; }
From source file:tk.barrydegraaff.ocs.OCS.java
private Element createShare(Element request, Element response, ZimbraSoapContext zsc) { try {//from w w w .j a va 2 s.c o m if (checkPermissionOnTarget(request.getAttribute("owncloud_zimlet_server_name"))) { final String urlParameters = "path=" + request.getAttribute("path") + "&shareType=" + request.getAttribute("shareType") + "&password=" + request.getAttribute("password"); byte[] credentials = Base64 .encodeBase64((uriDecode(request.getAttribute("owncloud_zimlet_username")) + ":" + uriDecode(request.getAttribute("owncloud_zimlet_password"))).getBytes()); byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); int postDataLength = postData.length; String requestUrl = request.getAttribute("owncloud_zimlet_server_name") + ":" + request.getAttribute("owncloud_zimlet_server_port") + request.getAttribute("owncloud_zimlet_oc_folder") + "/ocs/v1.php/apps/files_sharing/api/v1/shares"; URL url = new URL(requestUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setInstanceFollowRedirects(true); conn.setRequestMethod("POST"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("charset", "utf-8"); conn.setRequestProperty("Content-Length", Integer.toString(postDataLength)); conn.setRequestProperty("OCS-APIRequest", "true"); conn.setRequestProperty("X-Forwarded-For", zsc.getRequestIP()); conn.setRequestProperty("Authorization", "Basic " + new String(credentials)); conn.setUseCaches(false); try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { wr.write(postData); } InputStream _is; Boolean isError = false; if (conn.getResponseCode() < 400) { _is = conn.getInputStream(); isError = false; } else { _is = conn.getErrorStream(); isError = true; } BufferedReader in = new BufferedReader(new InputStreamReader(_is)); String inputLine; StringBuffer responseTxt = new StringBuffer(); while ((inputLine = in.readLine()) != null) { responseTxt.append(inputLine); } in.close(); Pattern pattern; if (isError) { pattern = Pattern.compile("<message>(.+?)</message>"); } else { pattern = Pattern.compile("<url>(.+?)</url>"); } Matcher matcher = pattern.matcher(responseTxt.toString()); matcher.find(); final String result = matcher.group(1); if (!isError) { pattern = Pattern.compile("<id>(.+?)</id>"); } matcher = pattern.matcher(responseTxt.toString()); matcher.find(); final String id = matcher.group(1); //Implement Expiry date and update Password //And empty password or expiryDate will remove the property from the share //https://docs.nextcloud.com/server/12/developer_manual/core/ocs-share-api.html#update-share String errorMessage = ""; try { final String[] updateArguments = { "expireDate=" + request.getAttribute("expiryDate"), "password=" + request.getAttribute("password") }; for (String urlParameter : updateArguments) { postData = urlParameter.getBytes(StandardCharsets.UTF_8); postDataLength = postData.length; requestUrl = request.getAttribute("owncloud_zimlet_server_name") + ":" + request.getAttribute("owncloud_zimlet_server_port") + request.getAttribute("owncloud_zimlet_oc_folder") + "/ocs/v1.php/apps/files_sharing/api/v1/shares" + "/" + id; url = new URL(requestUrl); conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setInstanceFollowRedirects(true); conn.setRequestMethod("PUT"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("charset", "utf-8"); conn.setRequestProperty("Content-Length", Integer.toString(postDataLength)); conn.setRequestProperty("OCS-APIRequest", "true"); conn.setRequestProperty("X-Forwarded-For", zsc.getRequestIP()); conn.setRequestProperty("Authorization", "Basic " + new String(credentials)); conn.setUseCaches(false); try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { wr.write(postData); } isError = false; if (conn.getResponseCode() < 400) { _is = conn.getInputStream(); isError = false; } else { _is = conn.getErrorStream(); isError = true; } in = new BufferedReader(new InputStreamReader(_is)); responseTxt = new StringBuffer(); while ((inputLine = in.readLine()) != null) { responseTxt.append(inputLine); } in.close(); if (!isError) { try { pattern = Pattern.compile("<message>(.+?)</message>"); matcher = pattern.matcher(responseTxt.toString()); matcher.find(); if (!"OK".equals(matcher.group(1))) { errorMessage += matcher.group(1) + ". "; } } catch (Exception e) { //ignore https://github.com/Zimbra-Community/owncloud-zimlet/issues/148 } } } } catch (Exception e) { errorMessage += e.toString(); } /*The result variable holds the result from the `Create Share` request. If it starts with http it means the request was OK, otherwise it will hold the error message. Not all share properties can be set with `Create Share` and creating a share on an object that is already shared, will not give an error, but does not do anything. Therefore we always do 2 `Update Share` requests as well, to set/remove the password and expiry date. The errorMessage variable holds the result from the Update Share` requests, in case it is empty, it means all went OK and the result from `Create Share` can be trusted. Otherwise it holds concatenated error messages, that should be displayed to the user. The link share url would not be reliable in this case. */ if ("".equals(errorMessage)) { response.addAttribute("createShare", "{\"statuscode\":100,\"id\":\"" + id + "\",\"message\":\"\",\"url\":\"" + result + "\",\"status\":\"ok\",\"token\":\"\"}"); } else { //result holds the url of the created share, if all went well, or also an error message if the sharing failed response.addAttribute("createShare", "{\"statuscode\":100,\"id\":\"" + id + "\",\"message\":\"\",\"url\":\"" + errorMessage + "\",\"status\":\"ok\",\"token\":\"\"}"); } } else { response.addAttribute("createShare", "{\"statuscode\":100,\"id\":0,\"message\":\"\",\"url\":\"" + "Host not allowed: " + request.getAttribute("owncloud_zimlet_server_name") + "\",\"status\":\"ok\",\"token\":\"\"}"); } return response; } catch (Exception ex) { response.addAttribute("createShare", "{\"statuscode\":100,\"id\":0,\"message\":\"\",\"url\":\"" + "Could not create share. " + "\",\"status\":\"ok\",\"token\":\"\"}"); return response; } }
From source file:socialtrade1.ChildThread.java
void Get() { try {/*from w ww .ja v a 2 s . c o m*/ URL url1 = new URL(url); HttpURLConnection con = (HttpURLConnection) url1.openConnection(); con.setDoOutput(true); con.setInstanceFollowRedirects(true); con.setRequestMethod("GET"); con.setRequestProperty("Content-Type", " application/json; charset=UTF-8"); con.setRequestProperty("charset", "utf-8"); con.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); con.setRequestProperty("Host", "www.frenzzup.com"); con.setRequestProperty("Cookie", Cookie); con.setUseCaches(false); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response1 = new StringBuffer(); Engine.ThreadStatus[PositionNumber][state] = con.getResponseCode(); while ((inputLine = in.readLine()) != null) { response1.append(inputLine); } String res = response1.toString(); new Save("get11", res).start(); if (printResponse) System.out.println(res); Utilities.ThreadResponse[PositionNumber][state] = res; Engine.ThreadStatus[PositionNumber][state] = 200; new Save("get111", Utilities.ThreadResponse[PositionNumber][state]).start(); Utilities.getresp[PositionNumber][0] = res; in.close(); } catch (Exception m) { } }
From source file:socialtrade1.ChildThread.java
void Post() { try {//from w w w .j a v a 2 s . c o m byte[] postData = body.getBytes(StandardCharsets.UTF_8); int postDataLength = postData.length; URL url1 = new URL(url); HttpURLConnection con = (HttpURLConnection) url1.openConnection(); con.setDoOutput(true); con.setInstanceFollowRedirects(true); con.setRequestMethod("POST"); con.setRequestProperty("Content-Type", " application/json; charset=UTF-8"); con.setRequestProperty("charset", "utf-8"); con.setRequestProperty("Content-Length", Integer.toString(postDataLength)); con.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); con.setRequestProperty("Host", "www.frenzzup.com"); con.setRequestProperty("Cookie", Cookie); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.write(postData); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response1 = new StringBuffer(); Engine.ThreadStatus[PositionNumber][state] = con.getResponseCode(); while ((inputLine = in.readLine()) != null) { response1.append(inputLine); } String resp = response1.toString(); if (printResponse) System.out.println(resp); Utilities.ThreadResponse[PositionNumber][state] = resp; in.close(); } catch (Exception m) { } }
From source file:net.openid.appauthdemo.TokenActivity.java
@MainThread private void fetchUserInfo(String accessToken, String idToken, AuthorizationException ex) { if (ex != null) { Log.e(TAG, "Token refresh failed when fetching user info"); mUserInfoJson.set(null);/*from w ww . j av a2 s . com*/ runOnUiThread(this::displayAuthorized); return; } AuthorizationServiceDiscovery discovery = mStateManager.getCurrent() .getAuthorizationServiceConfiguration().discoveryDoc; URL userInfoEndpoint; try { userInfoEndpoint = new URL(discovery.getUserinfoEndpoint().toString()); } catch (MalformedURLException urlEx) { Log.e(TAG, "Failed to construct user info endpoint URL", urlEx); mUserInfoJson.set(null); runOnUiThread(this::displayAuthorized); return; } mExecutor.submit(() -> { try { HttpURLConnection conn = (HttpURLConnection) userInfoEndpoint.openConnection(); conn.setRequestProperty("Authorization", "Bearer " + accessToken); conn.setInstanceFollowRedirects(false); String response = Okio.buffer(Okio.source(conn.getInputStream())) .readString(Charset.forName("UTF-8")); mUserInfoJson.set(new JSONObject(response)); } catch (IOException ioEx) { Log.e(TAG, "Network error when querying userinfo endpoint", ioEx); showSnackbar("Fetching user info failed"); } catch (JSONException jsonEx) { Log.e(TAG, "Failed to parse userinfo response"); showSnackbar("Failed to parse user info"); } runOnUiThread(this::displayAuthorized); }); }
From source file:ar.com.aleatoria.ue.rest.SecureSimpleClientHttpRequestFactory.java
/** * Template method for preparing the given {@link HttpURLConnection}. * <p>//from www. j a v a2s . c o m * The default implementation prepares the connection for input and output, and sets the HTTP method. * * @param connection the connection to prepare * @param httpMethod the HTTP request method ({@code GET}, {@code POST}, etc.) * @throws IOException in case of I/O errors */ protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { if (this.connectTimeout >= 0) { connection.setConnectTimeout(this.connectTimeout); } if (this.readTimeout >= 0) { connection.setReadTimeout(this.readTimeout); } connection.setDoInput(true); if ("GET".equals(httpMethod)) { connection.setInstanceFollowRedirects(true); } else { connection.setInstanceFollowRedirects(false); } if ("PUT".equals(httpMethod) || "POST".equals(httpMethod)) { connection.setDoOutput(true); } else { connection.setDoOutput(false); } connection.setRequestMethod(httpMethod); }
From source file:com.github.thesmartenergy.sparql.generate.api.Fetch.java
@GET public Response doFetch(@Context Request r, @Context HttpServletRequest request, @DefaultValue("http://localhost:8080/sparql-generate/example/example1") @QueryParam("uri") String uri, @DefaultValue("*/*") @QueryParam("useaccept") String useaccept, @DefaultValue("") @QueryParam("accept") String accept) throws IOException { HttpURLConnection con; String message = null;/*w ww . ja v a 2s .co m*/ URL obj; try { obj = new URL(uri); con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("Accept", useaccept); con.setInstanceFollowRedirects(true); System.out.println("GET to " + uri + " returned " + con.getResponseCode()); InputStream in = con.getInputStream(); message = IOUtils.toString(in); } catch (IOException e) { return Response.status(Response.Status.BAD_REQUEST) .entity("Error while trying to access message at URI <" + uri + ">: " + e.getMessage()).build(); } Map<String, List<String>> headers = con.getHeaderFields(); if (!headers.containsKey("Link")) { return Response.status(Response.Status.BAD_REQUEST).entity( "Error while processing message at URI <" + uri + ">: there should be a Link header field") .build(); } String var = null; URL qobj = null; for (String linkstr : headers.get("Link")) { Link link = Link.valueOf(linkstr); if (link.getRels().contains("https://w3id.org/sparql-generate/voc#spargl-query") || link.getRels().contains("spargl-query")) { Map<String, String> params = link.getParams(); // check the context // check syntax for variable ? var = params.getOrDefault("var", "message"); // ok for relative and absolute URIs ? try { qobj = obj.toURI().resolve(link.getUri()).toURL(); System.out.println("found link " + qobj + " and var " + var); } catch (Exception e) { System.out.println(e.getMessage()); } break; } // what if multiple suitable links ? } if (var == null || qobj == null) { return Response.status(Response.Status.BAD_REQUEST).entity("Error while processing message at URI <" + uri + ">: did not find a suitable link with relation spargl-query, or https://w3id.org/sparql-generate/voc#spargl-query") .build(); } HttpURLConnection qcon; String query; try { qcon = (HttpURLConnection) qobj.openConnection(); qcon.setRequestMethod("GET"); qcon.setRequestProperty("Accept", "application/sparql-generate"); qcon.setInstanceFollowRedirects(true); System.out.println("GET to " + qobj + " returned " + qcon.getResponseCode()); InputStream in = qcon.getInputStream(); query = IOUtils.toString(in); } catch (IOException e) { return Response.status(Response.Status.BAD_REQUEST) .entity("Error while trying to access query at URI <" + qobj + ">: " + e.getMessage()).build(); } System.out.println("got query " + query); // parse the SPARQL-Generate query and create plan PlanFactory factory = new PlanFactory(); Syntax syntax = SPARQLGenerate.SYNTAX; SPARQLGenerateQuery q = (SPARQLGenerateQuery) QueryFactory.create(query, syntax); if (q.getBaseURI() == null) { q.setBaseURI("http://example.org/"); } RootPlan plan = factory.create(q); // create the initial model Model model = ModelFactory.createDefaultModel(); // if content encoding is not text-based, then use base64 encoding // use con.getContentEncoding() for this String dturi = "http://www.w3.org/2001/XMLSchema#string"; String ct = con.getContentType(); if (ct != null) { dturi = "urn:iana:mime:" + ct; } QuerySolutionMap initialBinding = new QuerySolutionMap(); TypeMapper typeMapper = TypeMapper.getInstance(); RDFDatatype dt = typeMapper.getSafeTypeByName(dturi); Node arqLiteral = NodeFactory.createLiteral(message, dt); RDFNode jenaLiteral = model.asRDFNode(arqLiteral); initialBinding.add(var, jenaLiteral); // execute the plan plan.exec(initialBinding, model); System.out.println(accept); if (!accept.equals("text/turtle") && !accept.equals("application/rdf+xml")) { List<Variant> vs = Variant .mediaTypes(new MediaType("application", "rdf+xml"), new MediaType("text", "turtle")).build(); Variant v = r.selectVariant(vs); accept = v.getMediaType().toString(); } System.out.println(accept); StringWriter sw = new StringWriter(); Response.ResponseBuilder res; if (accept.equals("application/rdf+xml")) { model.write(sw, "RDF/XML", "http://example.org/"); res = Response.ok(sw.toString(), "application/rdf+xml"); res.header("Content-Disposition", "filename= message.rdf;"); return res.build(); } else { model.write(sw, "TTL", "http://example.org/"); res = Response.ok(sw.toString(), "text/turtle"); res.header("Content-Disposition", "filename= message.ttl;"); return res.build(); } }