0

Could someone try my codes out? It was working a few days ago and now it's not. I did not modify anything, and so I suspect the webmaster of that side has block me. Could someone check it out for me? This is part of my school project.

public class Cost extends TimerTask{ public void run() { Calendar rightNow = Calendar.getInstance(); Integer hour = rightNow.get(Calendar.HOUR_OF_DAY); if (hour==1) { try { URL tariff = new URL("http://www.emcsg.com/MarketData/PriceInformation?downloadRealtime=true"); ReadableByteChannel tar = Channels.newChannel(tariff.openStream()); FileOutputStream fos = new FileOutputStream("test.csv"); fos.getChannel().transferFrom(tar, 0, 1<<24); } catch (IOException ex) { Logger.getLogger(Cost.class.getName()).log(Level.SEVERE, null, ex); } } else { } } } 
10
  • Yes I do, but I don't think it's because of this though. java.io.FileNotFoundException: D:\test.csv (The system cannot find the file specified) Commented Jan 16, 2012 at 17:17
  • I was supposed to use these code to download the file first before my other codes can process it..but damn, I super pissed off because it doesn't download anymore..................... Commented Jan 16, 2012 at 17:18
  • I can manually download using a browser, but my whole idea was to make it auto download.... Commented Jan 16, 2012 at 17:20
  • 1
    Well, if you get to line fos.getChannel().transferFrom(tar, 0, 1<<24); (I guess the FileNotFoundException is thrown here) you could already open a stream so the problem seems to be elsewhere. Please fix all exceptions, even if you think they're not related. If they really aren't they might still hide the actual problem. Commented Jan 16, 2012 at 17:21
  • if you can download in a browser then you're probably not blocked Commented Jan 16, 2012 at 17:21

2 Answers 2

4

First of all, clean up your IO exceptions as that might be obscuring the problem - check you can write to D:.

If you are being blocked by the site because of your user-agent header:

This will show you your user-agent header: http://pgl.yoyo.org/http/browser-headers.php. Then the answer to Setting user agent of a java URLConnection tells you how to set your header.

You will either need to add a step between instantiating URL and opening stream:

URL tariff = new URL("http://www.emcsg.com/MarketData/PriceInformation?downloadRealtime=true"); java.net.URLConnection c = tariff.openConnection(); c.setRequestProperty("User-Agent", " USER AGENT STRING HERE "); ReadableByteChannel tar = Channels.newChannel(c.getInputStream()); 

or you could try just doing this:

System.setProperty("http.agent", " USER AGENT STRING HERE "); 

sometime before you call openStream().

Edit: This works for me. Can you try running it and let us know the output:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.URL; import java.net.URLConnection; public class TestURL { public static void main(String[] args) { try { URL tariff = new URL("http://www.emcsg.com/MarketData/PriceInformation?downloadRealtime=true"); URLConnection c = tariff.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream())); System.out.println(br.readLine()); } catch (IOException ex) { ex.printStackTrace(); } } } 
Sign up to request clarification or add additional context in comments.

14 Comments

Thanks paul, I'm trying it now
sorry if I'm noob, the setting up of the user agent header, which part are u referring to?
Put System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7"); in a line or two before if (hour==1) {
oh no, it still didn't work...could I space my modified code here?
System.setProperty("http.agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7"); if (hour==2) { try { URL tariff = new URL("emcsg.com/MarketData/…); java.net.URLConnection c = tariff.openConnection(); c.setRequestProperty("User-Agent", " Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.75 Safari/535.7 ");
|
0

I checked your code and running it I had no problem, everything works fine. Are you working behind a proxy?

In that case you have to configure it:

System.setProperty("http.proxyHost", "my.proxy.name"); System.setProperty("http.proxyPort", "8080"); 

9 Comments

Yes, you can set it there. It's important that it's set berfore you do 'ReadableByteChannel tar = Channels.newChannel(tariff.openStream());', so in main it's ok
I did try your System.setProperty("http.proxyHost", "my.proxy.name"); System.setProperty("http.proxyPort", "8080");, but to no avail
You have to change 'my.proxy.name' with the real name of the proxy and '8080' with the real port. Did you do that?
when u mean proxy name, do you mean proxy ip?
System.setProperty("http.proxyHost", "155.**.*.1*"); System.setProperty("http.proxyPort", "4****"); I tried with this, but still the same issue
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.