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 {

  }
}
}
Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
Eugene
  • 263
  • 2
  • 5
  • 16
  • 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) – Eugene Jan 16 '12 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..................... – Eugene Jan 16 '12 at 17:18
  • I can manually download using a browser, but my whole idea was to make it auto download.... – Eugene Jan 16 '12 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. – Thomas Jan 16 '12 at 17:21
  • if you can download in a browser then you're probably not blocked – soulcheck Jan 16 '12 at 17:21
  • If you can hit it in a browser then you're probably not blocked, unless the website is checking your User-Agent header. You can always set a user-agent header to match your browser but it's a bit cheeky. – Paul Medcraft Jan 16 '12 at 17:23
  • oh god, this is irritating me, I just screen my codes, and even use my old working codes, but it's not working...could someone please help me test these codes out.... : – Eugene Jan 16 '12 at 17:27
  • try Paul's suggestion of setting `user-agent`, may be the site identified your program as a bot. – RanRag Jan 16 '12 at 17:30
  • I tried this java.net.URLConnection c = url.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"); but netbeans wasn't able to identify url.open connection – Eugene Jan 16 '12 at 17:48
  • Try setting it as "http.agent" system property instead - see my answer below. And it should probably be tariff.openConnection() – Paul Medcraft Jan 16 '12 at 17:56

2 Answers2

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();
            }
        }
    }
Community
  • 1
  • 1
Paul Medcraft
  • 1,386
  • 11
  • 23
  • Thanks paul, I'm trying it now – Eugene Jan 16 '12 at 18:01
  • sorry if I'm noob, the setting up of the user agent header, which part are u referring to? – Eugene Jan 16 '12 at 18:16
  • 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) {` – Paul Medcraft Jan 16 '12 at 18:19
  • oh no, it still didn't work...could I space my modified code here? – Eugene Jan 16 '12 at 18:25
  • 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("http://www.emcsg.com/MarketData/PriceInformation?downloadRealtime=true"); 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 "); – Eugene Jan 16 '12 at 18:28
  • 'ReadableByteChannel tar = Channels.newChannel(c.getInputStream());' 'FileOutputStream fos = new FileOutputStream("Tariff.csv");' 'fos.getChannel().transferFrom(tar, 0, 1<<13);' – Eugene Jan 16 '12 at 18:28
  • No, needs to be one approach or the other, not both. Just comment out `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 "); ` and try again. – Paul Medcraft Jan 16 '12 at 18:29
  • I did attempt to remove the exception earlier on, but it was still the same. I think it's definitely the webserver that is blocking me as a bot – Eugene Jan 16 '12 at 18:30
  • I comment out that line, still couldn't download – Eugene Jan 16 '12 at 18:31
  • sorry for the trouble, on my part, I'm also trying how to figure this out. – Eugene Jan 16 '12 at 18:48
  • You *are* running the java process on the same machine as the browser that *can* access the page? Is it the same machine and location / connection that worked OK earlier? – Paul Medcraft Jan 16 '12 at 18:51
  • yes, I'm running the java process on the same machine as the browser. It worked well 2 days ago, same machine and same location. – Eugene Jan 16 '12 at 18:54
  • Please try the example in my last edit and let us know the output. – Paul Medcraft Jan 16 '12 at 20:53
  • Hi paul, I seem to found the error after much attempt, apparently it was link to another .class file, but netbeans did not indicate that error. Thanks for the patience and help! I will give you my vote for all your helpful advices! – Eugene Jan 18 '12 at 15:07
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");
Marco Porta
  • 23
  • 1
  • 3
  • 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 – Marco Porta Jan 16 '12 at 17:31
  • I did try your System.setProperty("http.proxyHost", "my.proxy.name"); System.setProperty("http.proxyPort", "8080");, but to no avail – Eugene Jan 16 '12 at 17:32
  • You have to change 'my.proxy.name' with the real name of the proxy and '8080' with the real port. Did you do that? – Marco Porta Jan 16 '12 at 17:33
  • when u mean proxy name, do you mean proxy ip? – Eugene Jan 16 '12 at 17:36
  • System.setProperty("http.proxyHost", "155.**.*.1*"); System.setProperty("http.proxyPort", "4****"); I tried with this, but still the same issue – Eugene Jan 16 '12 at 17:43
  • Can you print the stacktrace of the exception and post it? which line gives the NPE? – Marco Porta Jan 16 '12 at 17:45
  • java.io.FileNotFoundException: D:\test.csv (The system cannot find the file specified) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:106) at java.io.FileInputStream.(FileInputStream.java:66) at java.io.FileReader.(FileReader.java:41) at evscheduler.Opencsv.Csvreader(Opencsv.java:29) at evscheduler.EVschedulerApp$1$1.run(EVschedulerApp.java:73) at java.lang.Thread.run(Thread.java:619) – Eugene Jan 16 '12 at 17:47
  • Are you sure that the exception is related to the code you posted? Seems you're trying to read the file you wrote, but you wrote "test.cvs", not "D:\test.csv". Try doing this: FileOutputStream fos = new FileOutputStream("d:\\test.csv"); – Marco Porta Jan 16 '12 at 17:51
  • I just did what you recommended, it couldn't work. The file exception are all due to the later code. By right, my codes should have download and then being processed by other codes. The only download code I have is these code... – Eugene Jan 16 '12 at 17:56