urlconnection

Reading from a URL Connection Java

有些话、适合烂在心里 提交于 2019-11-29 15:12:12
I'm trying to read html code from a URL Connection. In one case the html file I'm trying to read includes 5 line breaks before the actual doc type declaration. In this case the input reader throws an exception for EOF. URL pageUrl = new URL( "http://www.nytimes.com/2011/03/15/sports/basketball/15nbaround.html" ); URLConnection getConn = pageUrl.openConnection(); getConn.connect(); DataInputStream dis = new DataInputStream(getConn.getInputStream()); //some read method here Has anyone ran into a problem like this? URL pageUrl = new URL("http://www.nytimes.com/2011/03/15/sports/basketball

Why would URLConnection timeout after 6+ minutes instead of 5 seconds?

白昼怎懂夜的黑 提交于 2019-11-29 10:23:03
I am copying this method verbatim from my application, which isn't complete yet but it does attempt to provide me with a timeout stack trace if things don't go smoothly: protected boolean isHttpAlive() { boolean isHttpOk = false; HttpURLConnection httpConnection = null; try { URL gurl = new URL("http://www.amazon.com/"); URLConnection connection = gurl.openConnection(); connection.setConnectTimeout(5 * 1000); // 5 seconds! httpConnection = (HttpURLConnection) connection; int responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) isHttpOk = true; } catch

Java HTTPUrlConnection timeout does not work

百般思念 提交于 2019-11-29 09:41:24
I've written a programm that opens a httpurlconnection to a website through random proxies. My httpurlconnection is called conn . Now I know, that some of those proxies might be too slow, so i've set the timeout of the connection to 40000 milliseconds with conn.setConnectTimeout(40000) and conn.setReadTimeout(40000) . After doing so, i got this code: long diff = 0; long starttime = 0; long endtime = 0; try { starttime = System.currentTimeMillis(); conn.connect(); endtime = System.currentTimeMillis(); diff = endtime - starttime; if (endtime <= starttime + conn.getConnectTimeout()) { //Trying to

can't get response header location using Java's URLConnection

那年仲夏 提交于 2019-11-29 09:26:17
can someone kindly suggest what I'm doing wrong here? I'm trying to get the header location for a certain URL using Java here is my code: URLConnection conn = url.openConnection(); String location = conn.getHeaderField("Location"); it's strange since I know for sure the URL i'm refering to return a Location header and using methods like getContentType() or getContentLength() works perfectly Perhaps Location header is returned as a part of redirect response. If so, URLConnection handles redirect automatically by issuing the second request to the pointed resource, so you need to disable it: (

Custom JavaFX WebView Protocol Handler

人走茶凉 提交于 2019-11-29 07:47:44
I am trying to write my own protocol handler for a JavaFX application that uses webview to access a single website. What I have done so far My custom URLStreamHandlerFactory public class MyURLStreamHandlerFactory implements URLStreamHandlerFactory { public URLStreamHandler createURLStreamHandler(String protocol) { System.out.println("Protocol: " + protocol); if (protocol.equalsIgnoreCase("http") || protocol.equalsIgnoreCase("https")) { return new MyURLStreamHandler(); } else { return new URLStreamHandler() { @Override protected URLConnection openConnection(URL u) throws IOException { return

Getting a reference to Java's default http(s) URLStreamHandler

柔情痞子 提交于 2019-11-29 04:33:29
I have a library that I need to use in one of my projects, which unfortunately registers its own URLStreamHandler to handle http- URLs . Is there a way to get a reference to Java's default http- and https- URLStreamHandlers , so I can specify one of them in the URL 's constructor to open a standard http connection without using the protocol overridden by the library? Found it: sun.net.www.protocol.http.Handler With that, I can now do: URL url = new URL(null, "http://...", new sun.net.www.protocol.http.Handler()); HttpURLConnection cxn = (HttpURLConnection) url.openConnection(); And I get a

understanding URLConnection.setReadTimeout()

百般思念 提交于 2019-11-29 03:29:31
Consider the following snippet: URLConnection connection = target.openConnection(); connection.setConnectTimeout(5000); // 5 sec connection.setReadTimeout(10000); // 10 sec Does the connection.setReadTimeout sets the maximum time available for STARTING read data or is it the maximum time available for COMPLETING read data? My understaning is that with that, java has 10 seconds to start reading the next byte of data from the connection. There is no timeout for finishing read all data from the connection as we do not know how big the strean may be. Is it correct? According to oracle docs, if no

Android: Trying to get data from youtube api

穿精又带淫゛_ 提交于 2019-11-29 02:40:13
I'm attempting to get data from http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured?&start-index=1&max-results=15&v=2 and print the titles of the videos, but I get an exception in the line int responseCode = httpConnection.getResponseCode(); what's the problem? Any other way to get this data? My code: URL url; try { String featuredFeed = "http://gdata.youtube.com/feeds/api/standardfeeds/recently_featured?&start-index=1&max-results=15&v=2"; url = new URL(featuredFeed); URLConnection connection; connection = url.openConnection(); HttpURLConnection httpConnection =

URLConnection does not get the charset

一曲冷凌霜 提交于 2019-11-28 23:14:35
I'm using URL.openConnection() to download something from a server. The server says Content-Type: text/plain; charset=utf-8 But connection.getContentEncoding() returns null . What up? Waldheinz This is documented behaviour as the getContentEncoding() method is specified to return the contents of the Content-Encoding HTTP header, which is not set in your example. You could use the getContentType() method and parse the resulting String on your own, or possibly go for a more advanced HTTP client library like the one from Apache . Buhake Sindi The value returned from URLConnection

Android getContentLength always return -1 when downloading apk file

有些话、适合烂在心里 提交于 2019-11-28 12:34:12
问题 I'm using the following code for downloading file in my Android project: URL url = new URL(fileUrl); URLConnection conection= url.openConnection(); conection.setDoOutput(true); conection.connect(); int lenghtOfFile = conection.getContentLength(); If fileUrl is apk, lenghtOfFile always return -1. But if it is image, video type,... lenghtOfFile return is exactly. Why ? I'm using eclipse, Android SDK revision 23. 回答1: The content length is not always available because by default Android request