urlconnection

Reading binary file from URLConnection

让人想犯罪 __ 提交于 2019-11-27 07:38:47
I'm trying to read a binary file from a URLConnection. When I test it with a text file it seems to work fine but for binary files it doesn't. I'm using the following mime-type on the server when the file is send out: application/octet-stream But so far nothing seems to work. This is the code that I use to receive the file: file = File.createTempFile( "tempfile", ".bin"); file.deleteOnExit(); URL url = new URL( "http://somedomain.com/image.gif" ); URLConnection connection = url.openConnection(); BufferedReader input = new BufferedReader( new InputStreamReader( connection.getInputStream() ) );

How can I specify the local address on a java.net.URLConnection?

耗尽温柔 提交于 2019-11-27 07:30:41
My Tomcat instance is listening to multiple IP addresses, but I want to control which source IP address is used when opening a URLConnection . How can I specify this? This should do the trick: URL url = new URL(yourUrlHere); Proxy proxy = new Proxy(Proxy.Type.DIRECT, new InetSocketAddress( InetAddress.getByAddress( new byte[]{your, ip, interface, here}), yourTcpPortHere)); URLConnection conn = url.openConnection(proxy); And you are done. Dont forget to handle exceptions nicely and off course change the values to suit your scenario. Ah and I omitted the import statements Using the Apache

URLConnection FTP list files

冷暖自知 提交于 2019-11-27 06:50:59
问题 URL url = new URL("ftp://user:pass@ftp.example.com/thefolder/"); URLConnection connection = url.openConnection(); ... // List files in folder... Using something like the above, I was wondering how I could grab a list of files within folder 'thefolder'? Hi guys, Following on from this original question, I have put together this simple FTP connection which is all working and looking good. It can see all files in the /live/conf/ location and copies them all to the local /conf/ location. The only

Java URLConnection Timeout

折月煮酒 提交于 2019-11-27 06:47:50
I am trying to parse an XML file from an HTTP URL. I want to configure a timeout of 15 seconds if the XML fetch takes longer than that, I want to report a timeout. For some reason, the setConnectTimeout and setReadTimeout do not work. Here's the code: URL url = new URL("http://www.myurl.com/sample.xml"); URLConnection urlConn = url.openConnection(); urlConn.setConnectTimeout(15000); urlConn.setReadTimeout(15000); urlConn.setAllowUserInteraction(false); urlConn.setDoOutput(true); InputStream inStream = urlConn.getInputStream(); InputSource input = new InputSource(inStream); And I am catching

Reading binary file from URLConnection

荒凉一梦 提交于 2019-11-27 03:59:43
问题 I'm trying to read a binary file from a URLConnection. When I test it with a text file it seems to work fine but for binary files it doesn't. I'm using the following mime-type on the server when the file is send out: application/octet-stream But so far nothing seems to work. This is the code that I use to receive the file: file = File.createTempFile( "tempfile", ".bin"); file.deleteOnExit(); URL url = new URL( "http://somedomain.com/image.gif" ); URLConnection connection = url.openConnection(

What is the proper way of setting headers in a URLConnection?

二次信任 提交于 2019-11-27 03:09:46
问题 My code is like the following: URLConnection cnx = address.openConnection(); cnx.setAllowUserInteraction(false); cnx.setDoOutput(true); cnx.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"); InputStream is = cnx.getInputStream(); Is it ok if I set the headers before I get the InputStream ? Will my header be sent, or will the server see the default URLConnection 's user-agent ( if any ) ? 回答1: The headers must be set prior to getting the InputStream to have

Java URLConnection - When do I need to use the connect() method?

别来无恙 提交于 2019-11-27 03:02:56
问题 I have a problem to understand the meaning of the connect() method in the URLConnection class. In the following code, if I use the connect() method, I get the same result if I don't use it. Why (or when) do I need to use it? URL u = new URL("http://example.com"); HttpURLConnection conn = (HttpURLConnection) u.openConnection(); conn.connect();//with or without it I have the same result InputStream in = conn.getInputStream(); int b; while ((b = in.read()) != -1) { System.out.write(b); } 回答1:

Why would a “java.net.ConnectException: Connection timed out” exception occur when URL is up?

99封情书 提交于 2019-11-27 02:59:26
I'm getting a ConnectException: Connection timed out with some frequency from my code. The URL I am trying to hit is up. The same code works for some users, but not others. It seems like once one user starts to get this exception they continue to get the exception. Here is the stack trace: java.net.ConnectException: Connection timed out Caused by: java.net.ConnectException: Connection timed out at java.net.PlainSocketImpl.socketConnect(Native Method) at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333) at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195) at java

Getting “java.net.ProtocolException: Server redirected too many times” Error

本秂侑毒 提交于 2019-11-27 01:35:42
问题 I'm making a simple URL request with code like this: URL url = new URL(webpage); URLConnection urlConnection = url.openConnection(); InputStream is = urlConnection.getInputStream(); But on that last line, I'm getting the "redirected too many times error". If my "webpage" var is, say, google.com then it works fine, but when I try to use my servlet's URL then it fails. It seems I can adjust the number of times it follows the redirects (default is 20) with this: System.setProperty("http

HttpURLConnection sends a POST request even though httpCon.setRequestMethod(“GET”); is set

一笑奈何 提交于 2019-11-27 01:04:06
问题 Here is my code: String addr = "http://172.26.41.18:8080/domain/list"; URL url = new URL(addr); HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); httpCon.setDoOutput(true); httpCon.setDoInput(true); httpCon.setUseCaches(false); httpCon.setAllowUserInteraction(false); httpCon.setRequestMethod("GET"); httpCon.addRequestProperty("Authorization", "Basic YWRtaW4fYFgjkl5463"); httpCon.connect(); OutputStreamWriter out = new OutputStreamWriter(httpCon.getOutputStream()); System