title: FileNotFoundException while getting the InputStream object from HttpURLConnection
body: <p>I am trying to send a post request to a url using HttpURLConnection (for using cUrl in java). 
  The content of the request is xml and at the end point, the application processes the xml and stores a record to the database and then sends back a response in form of xml string. The app is hosted on apache-tomcat locally. </p>
  
  <p>When I execute this code from the terminal, a row gets added to the db as expected. But an exception is thrown as follows while getting the InputStream from the connection</p>
  
  <pre><code>java.io.FileNotFoundException: http://localhost:8080/myapp/service/generate
      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1401)
      at org.kodeplay.helloworld.HttpCurl.main(HttpCurl.java:30)
  </code></pre>
  
  <p>Here is the code</p>
  
  <pre><code>public class HttpCurl {
      public static void main(String [] args) {
  
          HttpURLConnection con;
  
          try {
              con = (HttpURLConnection) new URL("http://localhost:8080/myapp/service/generate").openConnection();
              con.setRequestMethod("POST");
              con.setDoOutput(true);
              con.setDoInput(true);
  
              File xmlFile = new File("test.xml");
  
              String xml = ReadWriteTextFile.getContents(xmlFile);                
  
              con.getOutputStream().write(xml.getBytes("UTF-8"));
              InputStream response = con.getInputStream();
  
              BufferedReader reader = new BufferedReader(new InputStreamReader(response));
              for (String line ; (line = reader.readLine()) != null;) {
                  System.out.println(line);
              }
              reader.close();
  
          } catch (FileNotFoundException e) {
              e.printStackTrace();
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
    }
  </code></pre>
  
  <p>Its confusing because the exception is traced to the line <code>InputStream response = con.getInputStream();</code> and there doesn't seem to be any file involved for a FileNotFoundException. </p>
  
  <p>When I try to open a connection to an xml file directly, it doesn't throw this exception.</p>
  
  <p>The service app uses spring framework and Jaxb2Marshaller to create the response xml.</p>
  
  <p>The class ReadWriteTextFile is taken from <a href="http://www.javapractices.com/topic/TopicAction.do?Id=42" rel="noreferrer">here</a></p>
  
  <p>Thanks.</p>
  
  <p><strong>Edit:</strong>
  Well it saves the data in the DB and sends back a 404 response status code at the same time.</p>
  
  <p>I also tried doing a curl using php and print out the <code>CURLINFO_HTTP_CODE</code> which turns out to be 200. </p>
  
  <p>Any ideas on how do I go about debugging this ? Both service and client are on the local server. </p>
  
  <p><strong>Resolved:</strong>
  I could solve the problem after referring to an <a href="https://stackoverflow.com/questions/941628/urlconnection-filenotfoundexception-for-non-standard-http-port-sources/2274535#2274535">answer</a> on SO itself. </p>
  
  <p>It seems HttpURLConnection always returns 404 response when connecting to a url with a non standard port. </p>
  
  <p>Adding these lines solved it</p>
  
  <pre><code>con.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
  con.setRequestProperty("Accept","*/*");
  </code></pre>