0

I am trying to use the code below to read from a valid url. I can copy and paste the url into my browser and it works perfectly (displays the xml) but when I try to access it programatically it returns nothing (no data and no error). I have already tried to set the user-agent via this post: Can't read in HTML content from valid URL but it didnt fix my problem. If it matters I am trying to do a single Eve API call. I believe the problem is that I do not have my headers formatted correctly, and the Eve site is rejecting the query. I can access the data fine using PHP, but I recently had to change languages.

    public static void readFileToXML(String urlString,String fName)
{
    try{
    java.net.URL url = new java.net.URL(urlString);
    System.out.println(url);
    URLConnection cnx = url.openConnection();
    cnx.setAllowUserInteraction(false);         
    cnx.setDoOutput(true);
    cnx.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/531.0 (KHTML, like Gecko) Chrome/3.0.183.1 Safari/531.0");
    System.out.println(cnx.getContentLengthLong());// a change suggested in the comments. returns -1
    InputStream is = cnx.getInputStream();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    File file=new File("C:\\Users\\xxx\\Desktop\\"+fName);
    BufferedWriter bw = new BufferedWriter(new FileWriter(file,false));
    String inputLine;
    while ((inputLine = br.readLine()) != null) {
        bw.write(inputLine);
        System.out.println(inputLine);
    }
    System.out.println("Finished read");
    bw.close();
    br.close();
    }
    catch(Exception e)
    {
        System.out.println("Exception: "+e.getMessage());
    }
}
Community
  • 1
  • 1
Rilcon42
  • 9,584
  • 18
  • 83
  • 167
  • Try printing out the value of `cnx.getContentLengthLong()` before reading from the InputStream. Also, it's a good idea to print out an entire exception instead of just its message: `System.out.println(e);` – VGR Jun 20 '14 at 21:11
  • @VGR, I tried to print out cnx.getContentLengthLong() and it returned -1. I do not think I have my headers formatted correctly, and Eve is rejecting them for some reason. – Rilcon42 Jun 22 '14 at 18:54
  • The server should give you some indication of the problem in the HTTP response. Try adding this before the getContentLengthLong line: `HttpURLConnection h = (HttpURLConnection) cnx; System.out.printf("%s %s%n", h.getResponseCode(), h.getResponseMessage());` – VGR Jun 22 '14 at 20:54
  • @VGR, After making the last change I am now seeing: 301 moved permanently, yet I can still see the data when I paste the url into my browser. Any idea what im missing? – Rilcon42 Jun 23 '14 at 16:22
  • You shouldn't be getting that, according to the [documentation for HttpURLConnection.setFollowRedirects](http://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html#setFollowRedirects-boolean-). Try adding `cnx.setInstanceFollowRedirects(true);` right after the call to url.openConnection. – VGR Jun 23 '14 at 20:42

0 Answers0