urlconnection

Reading a web page in Java IOException Premature EOF

╄→гoц情女王★ 提交于 2019-11-27 22:19:15
问题 I am frequently getting a 'Premature EOF' Exception when reading a web page. The following is the StackTrace java.io.IOException: Premature EOF at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:556) at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:600) at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:687) at java.io.FilterInputStream.read(FilterInputStream.java:133) at sun.net.www.protocol.http.HttpURLConnection

Android: Trying to get data from youtube api

纵饮孤独 提交于 2019-11-27 21:57:47
问题 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);

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

蹲街弑〆低调 提交于 2019-11-27 18:36:10
问题 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? 回答1: Found it: sun.net.www.protocol.http.Handler With that, I can now do: URL url = new URL(null, "http://...", new sun.net.www

understanding URLConnection.setReadTimeout()

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 17:36:04
问题 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

Android's HttpURLConnection throws EOFException on HEAD requests

一世执手 提交于 2019-11-27 15:01:10
This small code snippet runs fine on my Mac's JVM. Unfortunately it crashes when executed on Android 4.2. import java.net.HttpURLConnection; import java.net.URL; public class App { public static void main( String... arguments ) throws Exception { HttpURLConnection connection = (HttpURLConnection) new URL( "https://github.com" ).openConnection(); connection.setRequestMethod( "HEAD" ); System.out.println( connection.getResponseCode() + "" ); } } If I replace https://github.com with https://www.facebook.com it works fine but I'm failing to figure out why. The exception does not contain a message;

URLConnection does not get the charset

无人久伴 提交于 2019-11-27 14:37:24
问题 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? 回答1: 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

Unable to resolve host “<insert URL here>” No address associated with hostname

不打扰是莪最后的温柔 提交于 2019-11-27 10:05:16
问题 I tried following this tutorial: Getting Data from the Web I tried implementing it on Android 3.0, the latest platform for tablets, however, I get this error: " Unable to resolve host "www.anddev.org" No address associated with hostname. " You can checkout the URL that I used just to prove that the file exists. http://www.anddev.org/images/tut/basic/getdatafromtheweb/loadme.txt I created a private class and extended it with asynctask. Here is the code: private class Downloader extends

Calling a Servlet from a Java application

◇◆丶佛笑我妖孽 提交于 2019-11-27 09:45:27
I want to call a Servlet from a Java application. The problem is, that the call seems not to reach the Servlet. I do not get any error, but do not reach the first output "doPost" in the Servlet. If I open the URL in a web browser, I got - of course - the error that GET is not supported etc., but at least I see, that something happens. I use the following code (the ActionPackage class only holds a Vector of parameters and is Serializable): Java application: ActionPackage p = new ActionPackage(); p.addParameter("TEST", "VALUE"); System.out.println(p); URL gwtServlet = null; try { gwtServlet =

Can I override the Host header where using java's HttpUrlConnection class?

时光总嘲笑我的痴心妄想 提交于 2019-11-27 09:24:21
I'm using the following code to open a http connection in java: URL url = new URL("http://stackoverflow.com"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setRequestMethod("GET"); conn.setRequestProperty("Host", "Test:8080"); conn.getOutputStream(); However calling conn.setRequestProperty("Host", "Test:8080") appears to have no effect regardless of what order I call the methods and the Host is reset to the destination server. Is there any way to override the Host header without using a different library? TIA Matt Boris This used to work in

URLConnection with Cookies?

最后都变了- 提交于 2019-11-27 08:03:42
I'm trying to make a URLConnection that supports cookies. According to the documentation I can use: CookieManager cookieManager = new CookieManager(); CookieHandler.setDefault(cookieManager); I couldn't get this code to work, then I saw this only works for API 9 (2.3). However, I don't get an error using CookieManager in an older emulator, CookieManager exists, but can't be constructed. Is there any way to make this work for earlier versions? I tried: cookieManager.setAcceptCookie(true); URLConnection con = u.openConnection(); con.setRequestProperty("Cookie", cookieManager.getInstance()