urlconnection

URLConnection FTP list files

百般思念 提交于 2019-11-28 12:13:27
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 issue is, it's copying the files but there's no content.They are all 0KB and empty! Can anyone see

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

∥☆過路亽.° 提交于 2019-11-28 09:44:36
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 ) ? Ken Gentle The headers must be set prior to getting the InputStream to have any affect - an IllegalStateException will be thrown if the connection is already open. As far

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

北战南征 提交于 2019-11-28 09:37:20
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); } Amanda HttpURLConnection conn = (HttpURLConnection) u.openConnection(); only creates an Object connect()

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

早过忘川 提交于 2019-11-28 06:46:43
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.maxRedirects", "100"); But when I crank it up to, say, 100 it definitely takes longer to throw the error so I

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

谁都会走 提交于 2019-11-28 05:49:52
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.out.println(httpCon.getResponseCode()); System.out.println(httpCon.getResponseMessage()); out.close();

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

半世苍凉 提交于 2019-11-28 03:45:40
问题 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 =

Java HTTPUrlConnection timeout does not work

a 夏天 提交于 2019-11-28 03:09:41
问题 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

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

两盒软妹~` 提交于 2019-11-28 02:51:12
问题 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 回答1: Perhaps Location header is returned as a part of redirect response. If so, URLConnection handles

How to upload a WAV file using URLConnection

家住魔仙堡 提交于 2019-11-28 02:15:30
问题 Curretly, I can upload .wav files by using this command in Terminal (MAC): curl -H 'X-Arg: AccessKey=3f55abc5-2830-027ce-e328-4e74df5a3f8f' --data-binary @audio.wav -H 'Content-Type: audio/wav' http://generic-15327.2-3-11.com/bbb.aaa.eval/abc I want to do the same from Android. So far I have implemented an AsyncTask with the URLConnection inside, I send the headers and everything, except the wave file. Get a json response telling that no file was uploaded, obviously, but so far so good, I

Custom JavaFX WebView Protocol Handler

不羁的心 提交于 2019-11-28 01:29:33
问题 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