/** * Fetch content from MACM instance by doing a HTTP GET request and set Cookies * * @param url {@link String} MACM content url * @return {@link InputStream} */ public InputStream openStream(String url) throws IOException { urlConnection = (HttpURLConnection) new URL(url).openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setUseCaches(false); HttpURLConnection.setFollowRedirects(true); if (COOKIES != null) { for (String cookie : CaaSResource.COOKIES) { urlConnection.addRequestProperty("Cookie", cookie.split(";", 1)[0]); } } if (urlConnection.getResponseCode() == 401 || urlConnection.getResponseCode() == 400) { try { connect(); } catch (URISyntaxException e) { e.printStackTrace(); } finally { openStream(url); } } return urlConnection.getInputStream(); }
/** * removed header (only used for authorization for PP) 2015.08 * * @param url the url * @return the JSON object * @throws MalformedURLException the malformed url exception * @throws IOException Signals that an I/O exception has occurred. * @throws JSONException the JSON exception */ static JSONObject readJsonFromUrlWithCmsHeader(String url) throws MalformedURLException, IOException, JSONException { InputStream is = null; JSONObject jObj = new JSONObject(); try { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection(); con.setRequestMethod("GET"); con.setConnectTimeout(ParallecGlobalConfig.urlConnectionConnectTimeoutMillis); con.setReadTimeout(ParallecGlobalConfig.urlConnectionReadTimeoutMillis); is = con.getInputStream(); BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String jsonText = PcFileNetworkIoUtils.readAll(rd); jObj = new JSONObject(jsonText); rd.close(); } catch (Exception t) { logger.error( "readJsonFromUrl() exception: " + t.getLocalizedMessage() + PcDateUtils.getNowDateTimeStrStandard()); } finally { if (is != null) { is.close(); } } return jObj; }
/** * Login with user name and password sent as String parameter to url using POST Interrogation * * @param username * @param userpass * @throws IOException */ @And("^I make post message with user: \"([^\"]*)\" and password: \"([^\"]*)\"$") public void HttpPostForm(String username, String userpass) throws IOException { URL url = new URL("http://dippy.trei.ro"); HttpURLConnection hConnection = (HttpURLConnection) url.openConnection(); HttpURLConnection.setFollowRedirects(true); hConnection.setDoOutput(true); hConnection.setRequestMethod("POST"); PrintStream ps = new PrintStream(hConnection.getOutputStream()); ps.print("user="******"&pass="******";do_login=Login"); ps.close(); hConnection.connect(); if (HttpURLConnection.HTTP_OK == hConnection.getResponseCode()) { InputStream is = hConnection.getInputStream(); System.out.println("!!! encoding: " + hConnection.getContentEncoding()); System.out.println("!!! message: " + hConnection.getResponseMessage()); is.close(); hConnection.disconnect(); } }
public static String getPage(String URLName) { StringBuilder buffer = new StringBuilder(); try { HttpURLConnection.setFollowRedirects(false); HttpURLConnection connection = (HttpURLConnection) new URL(URLName).openConnection(); // connection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT // 5.1; SV1; .NET CLR 1.1.4322)"); connection.connect(); BufferedReader dataInput = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line; while ((line = dataInput.readLine()) != null) { buffer.append(line); // buffer.append(cleanLine(line.toLowerCase())); buffer.append('\n'); } dataInput.close(); } catch (MalformedURLException e) { // e.printStackTrace(); return null; } catch (IOException e) { // e.printStackTrace(); return null; } return buffer.toString(); }
// 抓取页面内容 public static String getContentByUrl(String url, String codeKind) { StringBuffer document = null; URL targetUrl; try { targetUrl = new URL(url); System.setProperty("sun.net.client.defaultConnectTimeout", MaxTime); System.setProperty("sun.net.client.defaultReadTimeout", MaxTime); HttpURLConnection con = (HttpURLConnection) targetUrl.openConnection(); con.setFollowRedirects(true); con.setInstanceFollowRedirects(false); con.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), codeKind)); String s = ""; document = new StringBuffer(); while ((s = br.readLine()) != null) { document.append(s + "/r/n"); } s = null; br.close(); return document.toString(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
/** Initialize the http defaults and the authenticator. */ static { try { HttpURLConnection.setFollowRedirects(true); } catch (SecurityException e) { e.printStackTrace(); } }
public static String getHtmlSourceByGet(String spec, String charsetName, StringBuffer cookie) throws Exception { byte[] bytes = null; for (int i = 3; i > 0; --i) { try { URL url = new URL(spec); HttpURLConnection.setFollowRedirects(false); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); if (cookie != null) { connection.setRequestProperty("Cookie", cookie.toString()); } InputStream is = connection.getInputStream(); bytes = readToEnd(is); is.close(); if (cookie != null) { List<String> cookieList = connection.getHeaderFields().get("Set-Cookie"); if (cookieList != null) { for (String c : cookieList) { cookie.append(c); cookie.append("; "); } } } connection.disconnect(); break; } catch (Exception e) { if (i == 1) { throw e; } Thread.sleep((4 - i) * 1024); } } return encodeHtmlSource(bytes, charsetName); }
public static String visitWeb(String urlStr) { URL url = null; HttpURLConnection httpConn = null; InputStream in = null; try { url = new URL(urlStr); httpConn = (HttpURLConnection) url.openConnection(); HttpURLConnection.setFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.setRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE 6.0;Windows 2000)"); in = httpConn.getInputStream(); return convertStreamToString(in); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { in.close(); httpConn.disconnect(); } catch (Exception ex) { ex.printStackTrace(); } } return null; }
static { if (Integer.parseInt(Build.VERSION.SDK) < 8) { System.setProperty("http.keepAlive", "false"); } else { System.setProperty("http.keepAlive", "true"); } HttpURLConnection.setFollowRedirects(true); }
/** * Download the URL and return as a String. Gzip handling from http://goo.gl/J88WG * * @param url the URL to download * @return String of the URL contents * @throws IOException when there is an error connecting or reading the URL */ public String downloadUrl(URL url) throws TVRenamerIOException { InputStream inputStream = null; StringBuilder contents = new StringBuilder(); try { if (url != null) { logger.log(Level.INFO, "Downloading URL {0}", url.toString()); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection.setFollowRedirects(true); // allow both GZip and Deflate (ZLib) encodings conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); conn.setConnectTimeout(CONNECT_TIMEOUT_MS); conn.setReadTimeout(READ_TIMEOUT_MS); // create the appropriate stream wrapper based on the encoding type String encoding = conn.getContentEncoding(); if (encoding != null && encoding.equalsIgnoreCase("gzip")) { inputStream = new GZIPInputStream(conn.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { inputStream = new InflaterInputStream(conn.getInputStream(), new Inflater(true)); } else { inputStream = conn.getInputStream(); } // always specify encoding while reading streams BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8")); logger.finer("Before reading url stream"); String s; while ((s = reader.readLine()) != null) { contents.append(s); } if (logger.isLoggable(Level.FINEST)) { // no need to encode for logger output logger.log(Level.FINEST, "Url stream:\n{0}", contents.toString()); } } } catch (Exception e) { String message = "Exception when attempting to download and parse URL " + url; logger.log(Level.SEVERE, message, e); throw new TVRenamerIOException(message, e); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { logger.log(Level.SEVERE, "Exception when attempting to close input stream", e); } } return StringUtils.encodeSpecialCharacters(contents.toString()); }
@Override public void parseUrl(List<WeixinData> list, Node dom, Component component, String... args) { if (args[0] == null || args[0] == "" || args[1] == null || args[1] == "") return; String cookie = args[1]; // String referer = args[1]; List<String> results = StringUtil.regMatches(args[0], "<url>", "/url", true); for (int i = 0; i < results.size(); i++) { String tmpUrl = results.get(i); tmpUrl = "http://weixin.sogou.com" + tmpUrl.substring(tmpUrl.indexOf("CDATA[") + 6, tmpUrl.lastIndexOf("]]>")); String loc = null; try { HttpURLConnection conn = (HttpURLConnection) new URL(tmpUrl).openConnection(); conn.addRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:38.0) Gecko/20100101 Firefox/38.0"); conn.setRequestProperty("Cookie", cookie); // conn.setRequestProperty("Referer", referer); HttpURLConnection.setFollowRedirects(false); conn.setFollowRedirects(false); conn.connect(); loc = conn.getHeaderField("Location"); if (loc != null) Systemconfig.sysLog.log(conn.getResponseMessage()); Systemconfig.sysLog.log("real url: " + loc); int sleepTime = 30 + (int) (Math.random() * 20); Systemconfig.sysLog.log("sleep..." + sleepTime); TimeUtil.rest(sleepTime); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } list.get(i).setUrl(loc == null ? "err." : loc); } }
/** * checks a given URL for availbility * * @param urlN given URL * @return true if URL exists otherwise false */ public static boolean urlExist(URL urlN) { try { HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) urlN.openConnection(); con.setRequestMethod("HEAD"); return (con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (Exception e) { return false; } }
@Override public Statement apply(Statement base, FrameworkMethod method, Object target) { // Check at the beginning, so this can be used as a static field if (assumeOnline) { Assume.assumeTrue(serverOnline.get(port)); } else { Assume.assumeTrue(serverOffline.get(port)); } RestTemplate client = new RestTemplate(); boolean followRedirects = HttpURLConnection.getFollowRedirects(); HttpURLConnection.setFollowRedirects(false); boolean online = false; try { client.getForEntity(new UriTemplate(getUrl("/sparklr/login.jsp")).toString(), String.class); online = true; logger.info("Basic connectivity test passed"); } catch (RestClientException e) { logger.warn( String.format( "Not executing tests because basic connectivity test failed for hostName=%s, port=%d", hostName, port), e); if (assumeOnline) { Assume.assumeNoException(e); } } finally { HttpURLConnection.setFollowRedirects(followRedirects); if (online) { serverOffline.put(port, false); if (!assumeOnline) { Assume.assumeTrue(serverOffline.get(port)); } } else { serverOnline.put(port, false); } } return super.apply(base, method, target); }
/** * Establishes an HttpURLConnection from a URL, with the correct configuration to receive content from the given URL. * * @param url The URL to set up and receive content from * @return A valid HttpURLConnection * * @throws IOException The openConnection() method throws an IOException and the calling method is responsible for handling it. */ public static HttpURLConnection openHttpConnection(URL url) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.setDoOutput(false); System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19"); conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.162 Safari/535.19"); HttpURLConnection.setFollowRedirects(true); conn.setUseCaches(false); conn.setInstanceFollowRedirects(true); return conn; }
public void setAddMusic(Region r, String region, String message, RegiosPlayer p) { try { URL u = new URL(message); HttpURLConnection huc = (HttpURLConnection) u.openConnection(); HttpURLConnection.setFollowRedirects(false); huc.setRequestMethod("HEAD"); huc.connect(); if (huc.getResponseCode() != HttpURLConnection.HTTP_OK) { p.sendMessage(ChatColor.RED + "[Regios] URL does not exist!"); return; } } catch (MalformedURLException murlex) { p.sendMessage(ChatColor.RED + "[Regios] Invalid URL Format!"); return; } catch (IOException e) { e.printStackTrace(); return; } if (r == null) { p.sendMessage( ChatColor.RED + "[Regios] The region " + ChatColor.BLUE + region + ChatColor.RED + " doesn't exist!"); return; } else { if (!r.canModify(p)) { p.sendMessage(ChatColor.RED + "[Regios] You are not permitted to modify this region!"); return; } boolean match = false; for (String s : r.getCustomSoundUrl()) { if (s.trim().equalsIgnoreCase(message.trim())) { match = true; } } if (match) { p.sendMessage( ChatColor.RED + "[Regios] The URL " + ChatColor.BLUE + message + ChatColor.RED + " already exists!"); return; } p.sendMessage( ChatColor.GREEN + "[Regios] Spout Music URL added to region " + ChatColor.BLUE + region); } mutable.editAddToMusicList(r, message); }
public void run() { try { long startTime = System.nanoTime(); URL url = new URL( servletAddress + "?" + "sequenceSize=" + sequenceSize + "&" + "packagesAmount=" + packagesAmount); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); HttpURLConnection.setFollowRedirects(true); connection.setDoOutput(true); connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/octet-stream"); long connectionEstablishTime = System.nanoTime() - startTime; // System.out.println(connection.); startTime = System.nanoTime(); InputStream stream = new BufferedInputStream(connection.getInputStream()); byte[] buf = new byte[sequenceSize]; while (stream.read(buf, 0, sequenceSize) != -1) { // print(buf); } long dataTransferTime = System.nanoTime() - startTime; startTime = System.nanoTime(); stream.close(); connection.disconnect(); long connectionCloseTime = System.nanoTime() - startTime; System.out.printf( "%d %4.6f %4.6f %4.6f\n", sequenceSize * packagesAmount, convertToSeconds(connectionEstablishTime), convertToSeconds(dataTransferTime), convertToSeconds(connectionCloseTime)); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
public static boolean exists(String URLName) { try { HttpURLConnection.setFollowRedirects(false); // note : you may also need // HttpURLConnection.setInstanceFollowRedirects(false) HttpURLConnection con = (HttpURLConnection) new URL(URLName).openConnection(); con.setRequestMethod("HEAD"); return (con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (Exception e) { e.printStackTrace(); return false; } }
static { try { // Default new HTTP connections to follow redirects HttpURLConnection.setFollowRedirects(true); } catch (Exception ignored) { } try { // could be other algorithms (prob need to calculate this another way. final SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, NAIVE_TRUST_MANAGER, new SecureRandom()); naiveSSLSocketFactory = sslContext.getSocketFactory(); } catch (Exception e) { LOG.warn("Failed to build Naive SSLSocketFactory", e); } }
public void handleAction(ActionParameters params) throws ActionException { final HttpServletResponse response = params.getResponse(); final HttpServletRequest httpRequest = params.getRequest(); // default print format is application/pdf final String pformat = params.getHttpParam(PARM_FORMAT, "application/pdf"); final JSONObject jsonprint = getPrintJSON(params); String file_save = params.getHttpParam(PARM_SAVE, ""); boolean geojsCase = false; if (!params.getHttpParam(PARM_GEOJSON, "").isEmpty()) geojsCase = true; final HttpURLConnection con = getConnection(pformat, geojsCase); for (Enumeration<String> e = httpRequest.getHeaderNames(); e.hasMoreElements(); ) { final String key = e.nextElement(); final String value = httpRequest.getHeader(key); con.setRequestProperty(key, value); } try { con.setRequestMethod("POST"); con.setDoOutput(true); con.setDoInput(true); HttpURLConnection.setFollowRedirects(false); con.setUseCaches(false); con.setRequestProperty(HEADER_CONTENT_TYPE, "application/json"); con.connect(); if (log.isDebugEnabled()) { log.debug(jsonprint.toString(2)); } IOHelper.writeToConnection(con, jsonprint.toString()); final byte[] presponse = IOHelper.readBytes(con.getInputStream()); // Save plot for future use if (!file_save.isEmpty()) savePdfPng(presponse, file_save, pformat); final String contentType = con.getHeaderField(HEADER_CONTENT_TYPE); response.addHeader(HEADER_CONTENT_TYPE, contentType); response.getOutputStream().write(presponse, 0, presponse.length); response.getOutputStream().flush(); response.getOutputStream().close(); } catch (Exception e) { throw new ActionException("Couldn't proxy request to print server", e); } finally { con.disconnect(); } }
private void createPage( HttpServletRequest req, HttpServletResponse res, HttpSession sess, String url, String sessID) { try { String contextToken = "$contextToken$" + _reqCount++; URL u = new URL(url); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.setDoInput(true); conn.setUseCaches(false); HttpURLConnection.setFollowRedirects(false); Enumeration e = req.getHeaderNames(); while (e.hasMoreElements()) { String name = (String) e.nextElement(); String value = req.getHeader(name); conn.setRequestProperty(name, value); } conn.setRequestProperty( "Cookie", "sesessionid=" + sessID + ";session=" + sessID + ";sessionid=" + sessID + ";JSESSIONID=" + sessID); conn.setRequestProperty(SALMON_CREATE_PAGE_HEADER, "true"); conn.setRequestProperty(SALMON_CONTEXT_TOKEN, contextToken); InputStream in = conn.getInputStream(); while (in.read() > -1) ; in.close(); conn.disconnect(); Cookie cookie = new Cookie("JSESSIONID", sessID); cookie.setMaxAge(-1); res.addCookie(cookie); TagContext tc = (TagContext) sess.getAttribute(contextToken); if (tc != null) { sess.removeAttribute(contextToken); req.setAttribute(TagContext.TAG_CONTEXT_REQ_KEY, tc); } } catch (Exception e) { MessageLog.writeErrorMessage("Error creating page", e, this); } }
/** * Checks if the given website is available. * * @param url * the URL * @return true, if available */ public static boolean checkWebsiteAvailable(String url) { try { URL url1 = new URL(url); HttpURLConnection.setFollowRedirects(false); HttpURLConnection con = (HttpURLConnection) url1.openConnection(); con.setRequestMethod("HEAD"); int response = con.getResponseCode(); return response == HttpURLConnection.HTTP_OK; } catch (Exception ex) { return false; } }
private void handleExternalBody(String url) throws JspException, ServletException, IOException { URL netURL = new URL(url); URLConnection conn = netURL.openConnection(); if (conn instanceof HttpURLConnection) ((HttpURLConnection) conn).setFollowRedirects(true); InputStream is = conn.getInputStream(); try { ReadStream in = Vfs.openRead(is); String encoding = conn.getContentEncoding(); String contentType = conn.getContentType(); if (_charEncoding != null) { encoding = _charEncoding; if (encoding != null && !encoding.equals("")) in.setEncoding(encoding); } else if (encoding != null) in.setEncoding(encoding); else if (contentType != null) { int p = contentType.indexOf("charset="); if (p > 0) { CharBuffer cb = new CharBuffer(); for (int i = p + 8; i < contentType.length(); i++) { int ch = contentType.charAt(i); if (ch == '"' || ch == '\'') { } else if (ch >= 'a' && ch <= 'z') cb.append((char) ch); else if (ch >= 'A' && ch <= 'Z') cb.append((char) ch); else if (ch >= '0' && ch <= '9') cb.append((char) ch); else if (ch == '-' || ch == '_') cb.append((char) ch); else break; } encoding = cb.toString(); in.setEncoding(encoding); } } JspWriter out = pageContext.getOut(); int ch; while ((ch = in.readChar()) >= 0) out.print((char) ch); } finally { is.close(); } }
@Override protected File[] createTarget(Reference reference) throws Exception { File[] files = new File[] {null, null}; int pos = reference.toString().lastIndexOf("/dataset/"); dataset_service = reference.toString().substring(0, pos + 9); // todo fallback to RDF if arff is not available files[0] = File.createTempFile("wflinput_", ".arff"); files[0].deleteOnExit(); HttpURLConnection uc = null; try { uc = ClientResourceWrapper.getHttpURLConnection( reference.toString(), "GET", ChemicalMediaType.WEKA_ARFF.toString()); HttpURLConnection.setFollowRedirects(true); DownloadTool.download(uc.getInputStream(), files[0]); logger.fine(files[0].getAbsolutePath()); } catch (Exception x) { throw x; } finally { try { if (uc != null) uc.getInputStream().close(); } catch (Exception x) { } try { if (uc != null) uc.disconnect(); } catch (Exception x) { } } // now predict try { // TODO set labels files[1] = (File) predictor.predict(files[0]); logger.fine(files[1].getAbsolutePath()); // TODO handle errors } catch (Exception x) { throw x; } return files; }
public static String getHtmlSourceByPost( String spec, String charsetName, byte[] postData, StringBuffer cookie) throws Exception { byte[] bytes = null; for (int i = 3; i > 0; --i) { try { URL url = new URL(spec); HttpURLConnection.setFollowRedirects(false); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Connection", "keep-alive"); connection.setRequestProperty("Content-Length", Integer.toString(postData.length)); if (cookie != null) { connection.setRequestProperty("Cookie", cookie.toString()); } OutputStream os = connection.getOutputStream(); os.write(postData); os.flush(); os.close(); InputStream is = connection.getInputStream(); bytes = readToEnd(is); is.close(); if (cookie != null) { List<String> cookieList = connection.getHeaderFields().get("Set-Cookie"); if (cookieList != null) { for (String c : cookieList) { cookie.append(c); cookie.append("; "); } } } connection.disconnect(); break; } catch (Exception e) { if (i == 1) { throw e; } Thread.sleep((4 - i) * 1024); } } return encodeHtmlSource(bytes, charsetName); }
/** * Gets a connection from a url. All getConnection calls should go through this code. * * @param inCookies Supply cookie Map (received from prior setCookies calls from server) * @param input boolean indicating whether this connection will be used for input * @param output boolean indicating whether this connection will be used for output * @param cache boolean allow caching (be careful setting this to true for non-static retrievals). * @return URLConnection established URL connection. */ public static URLConnection getConnection( URL url, Map inCookies, boolean input, boolean output, boolean cache, boolean allowAllCerts) throws IOException { URLConnection c = url.openConnection(); c.setRequestProperty("Accept-Encoding", "gzip, deflate"); c.setAllowUserInteraction(false); c.setDoOutput(output); c.setDoInput(input); c.setUseCaches(cache); c.setReadTimeout(220000); c.setConnectTimeout(45000); String ref = getReferrer(); if (StringUtilities.hasContent(ref)) { c.setRequestProperty("Referer", ref); } String agent = getUserAgent(); if (StringUtilities.hasContent(agent)) { c.setRequestProperty("User-Agent", agent); } if (c instanceof HttpURLConnection) { // setFollowRedirects is a static (global) method / setting - resetting // it in case other code changed it? HttpURLConnection.setFollowRedirects(true); } if (c instanceof HttpsURLConnection && allowAllCerts) { try { setNaiveSSLSocketFactory((HttpsURLConnection) c); } catch (Exception e) { LOG.warn("Could not access '" + url.toString() + "'", e); } } // Set cookies in the HTTP header if (inCookies != null) { // [optional] place cookies (JSESSIONID) into HTTP headers setCookies(c, inCookies); } return c; }
/** * 利用googlemap api 通过 HTTP 进行地址解析 * * @param address 地址 * @return HTTP状态代码,精确度(请参见精确度常数),纬度,经度 */ private static String getLatlng(String address) { String ret = ""; if (address != null && !address.equals("")) { try { address = URLEncoder.encode(address, "UTF-8"); // 进行这一步是为了避免乱码 } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); // logger.error("转码失败", e1); } String[] arr = new String[4]; arr[0] = address; arr[1] = OUTPUT; arr[2] = SENSOR; arr[3] = KEY; String url = MessageFormat.format( "http://maps.google.com/maps/geo?q={0}&output={1}&sensor={2}&key={3}", arr); URL urlmy = null; try { urlmy = new URL(url); HttpURLConnection con = (HttpURLConnection) urlmy.openConnection(); con.setFollowRedirects(true); con.setInstanceFollowRedirects(false); con.connect(); BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "UTF-8")); String s = ""; StringBuffer sb = new StringBuffer(""); while ((s = br.readLine()) != null) { sb.append(s + "\r\n"); } ret = "" + sb; } catch (MalformedURLException e) { e.printStackTrace(); // logger.error("通过http方式获取地址信息失败", e); } catch (IOException e) { e.printStackTrace(); // logger.error("文件读取失败", e); } } return ret; }
public void run() { HttpURLConnection var1 = null; try { // Spout Start HttpURLConnection.setFollowRedirects(true); // Spout End URL var2 = new URL(this.location); var1 = (HttpURLConnection) var2.openConnection(); // Spout Start System.setProperty("http.agent", ""); var1.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30"); // Spout End var1.setDoInput(true); var1.setDoOutput(false); var1.connect(); if (var1.getResponseCode() / 100 == 4) { return; } if (this.buffer == null) { this.imageData.image = ImageIO.read(var1.getInputStream()); } else { // Spout Start BufferedImage image = ImageIO.read(var1.getInputStream()); if (image != null) { this.imageData.image = this.buffer.parseUserSkin(image); } else { // System.out.println("No image data found for " + location); } // Spout End } } catch (Exception var6) { // var6.printStackTrace(); // Spout } finally { var1.disconnect(); } }
public void setTexturePackURL(Region r, String region, String message, RegiosPlayer p) { try { URL u = new URL(message); HttpURLConnection huc = (HttpURLConnection) u.openConnection(); HttpURLConnection.setFollowRedirects(false); huc.setRequestMethod("HEAD"); huc.connect(); if (huc.getResponseCode() != HttpURLConnection.HTTP_OK) { p.sendMessage(ChatColor.RED + "[Regios] URL does not exist!"); return; } } catch (MalformedURLException murlex) { p.sendMessage(ChatColor.RED + "[Regios] Invalid URL Format!"); return; } catch (IOException e) { e.printStackTrace(); return; } if (r == null) { p.sendMessage( ChatColor.RED + "[Regios] The region " + ChatColor.BLUE + region + ChatColor.RED + " doesn't exist!"); return; } else { if (!r.canModify(p)) { p.sendMessage(ChatColor.RED + "[Regios] You are not permitted to modify this region!"); return; } p.sendMessage( ChatColor.GREEN + "[Regios] Spout Texture Pack updated for region " + ChatColor.BLUE + region); } mutable.editTexturePackURL(r, message); }
public void set_status() { String str1 = "Light1 " + r1.getLight1(); String str2 = "Light2 " + r1.getLight2(); String str3 = "Light3 " + r1.getLight3(); try { String str = str1 + "#" + str2 + "#" + str3; URL url = new URL( "http://localhost:8080/WebApplication/MyHomeStatus?Light=" + URLEncoder.encode(str, "UTF-8")); HttpURLConnection hConnection = (HttpURLConnection) url.openConnection(); HttpURLConnection.setFollowRedirects(true); hConnection.setDoOutput(true); hConnection.setRequestMethod("GET"); hConnection.connect(); if (HttpURLConnection.HTTP_OK == hConnection.getResponseCode()) { jLabel1.setText("Connected to Web Server! Sending Data...."); jLabel1.setForeground(Color.GREEN); jLabel1.setOpaque(true); jLabel2.setText("Sent Data: "); jLabel3.setText(str1 + " " + str2 + " " + str3); jLabel3.setForeground(Color.RED); jLabel3.setOpaque(true); hConnection.disconnect(); } else { jLabel1.setText("Waiting for Web Server Connection..."); jLabel1.setForeground(Color.RED); jLabel1.setOpaque(true); jLabel2.setText(""); jLabel3.setText(""); } } catch (Exception ex) { System.out.println(ex.toString()); } }
/** * Retrieves a user's 64-bit Steam ID from their Community Name * * @param communityName the user's community name * @return the 64-bit Steam ID * @throws IOException if there is an error querying the API * @throws DOMException if an error occurs in the DOM * @throws ParserConfigurationException if there is an error configuring the XML parser * @throws ParseException if there is an error parsing the returned XML * @throws SAXException if there is an error preparing the XML to be parsed */ private static long getSteamId64FromName(String communityName) throws IOException, DOMException, ParserConfigurationException, ParseException, SAXException { /* * First attempt to return the 64-bit ID from cache. If the user does not exist in the cache, then query * the API for the ID and store it in the cache for future use. */ if (idCache.containsKey(communityName)) { return idCache.get(communityName); } else { /* * Opens a connection to the API */ HttpURLConnection.setFollowRedirects(false); HttpURLConnection conn = (HttpURLConnection) (new URL("http://steamcommunity.com/id/" + communityName + "?xml=1")) .openConnection(); conn.setRequestProperty("User-Agent", Configuration.getUserAgent()); if (conn.getResponseCode() >= 400) { throw new IOException("Server returned response code: " + conn.getResponseCode()); } /* * Parses and returns the user's 64-bit Steam ID */ DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Element profile = parser.parse(conn.getInputStream()).getDocumentElement(); long steamID64 = Long.parseLong(profile.getElementsByTagName("steamID64").item(0).getTextContent()); synchronized (idCache) { idCache.put(communityName, steamID64); } return steamID64; } }