title: Problem sending XML via HTTP
body: <p>I want to have an application which parses various RSS feeds and send the information to a remote server. The information is sent in xml format via http. At first I tried to deploy this application on my own server, so I send the xml using the method shown in <a href="http://www.java-tips.org/other-api-tips/httpclient/how-to-send-an-xml-document-to-a-remote-web-server-using-http-5.html" rel="nofollow" title="tutorialquot;">this tutorial</a> by Java Tips. Here is my code which is replicated from the example:</p>
  
  <p><strong>First Method</strong></p>
  
  <pre><code>    String strURL = "http://localhost/readme/readme_xml";
      String strXMLFilename = "output.xml";
      File input = new File(strXMLFilename);
  
  
      PostMethod post = new PostMethod(strURL);
      post.setRequestEntity(new InputStreamRequestEntity(
              new FileInputStream(input), input.length()));
      post.setRequestHeader(
              "Content-type", "text/xml; charset=ISO-8859-1");
      HttpClient httpclient = new HttpClient();
      try {
  
          int result = httpclient.executeMethod(post);            
          System.out.println("Response status code: " + result);            
          System.out.println("Response body: ");
          System.out.println(post.getResponseBodyAsString());            
      } finally {
          post.releaseConnection();
      }
  </code></pre>
  
  <p>This works perfectly (I even tested using a remote server outside the localhost). Then, somehow I cant use my own server to deploy this application, so I decided to migrate to Google Apps Engine. One thing about it, as we know it, is that not all libraries are allowed in the environment. So I try another method shown in ExampleDepot.com (I can't find where the exact url though) as below:</p>
  
  <p><strong>Second Method</strong></p>
  
  <pre><code>try {
             /* fill up this url with the remote server url */
              URL url = new URL("http://localhost/readme/readme_xml");
              FileReader fr = new FileReader("output.xml");
              char[] buffer = new char[1024*10];
              int len = 0;
              if ((len = fr.read(buffer)) != -1){
              /* send http request to remote server */
                  URLConnection conn = url.openConnection();
                  conn.setRequestProperty("Content-Type","text/xml;charset=ISO-8859-1"); /* need to specify the content type */
                  conn.setDoOutput(true);
                  conn.setDoOutput(true);
                  PrintWriter pw = new PrintWriter(conn.getOutputStream());
                  pw.write(buffer, 0, len);
                  pw.flush();
                  /* receive response from remote server*/
                  BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                  String input = null;
                  while ((input = bf.readLine()) != null){
                      System.out.println(input);
                  }
              }
  
          } catch (MalformedURLException e) {
                  e.printStackTrace();
          } catch (FileNotFoundException e) {
                  e.printStackTrace();
          } catch (IOException e) {
                  e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>The second method though, doesn't work and gives the following error (I use SimpleXMLElement (php) object to parse xml in the remote hosting):</p>
  
  <p><a href="https://www.evernote.com/shard/s45/sh/b75d0705-b304-44ae-b4e2-cc0c69987910/82264eaf4edab33150ef90f922bf75bb" rel="nofollow" title="error messagequot;">Error message from remote server</a></p>
  
  <p>Here's the php code from the remote server (In here, I just want the SimpleXMLElement to parse the xml without doing anything else fancy for now)</p>
  
  <pre><code>$xml = new SimpleXMLElement('php://input', NULL, TRUE);
     foreach ($xml -&gt; attributes() as $name =&gt; $val){
     echo "[".$name."] = ".$val."\n";
     }
  </code></pre>
  
  <p>I thought the cause of this problem is the malfunction xml file (because the eclipse IDE indicates there's error of "invalid byte 1 of 1-byte utf-8 sequence"). Then I use the same exact input xml file to the first method, but it still works perfectly. </p>
  
  <p>So is there any adjustment that I need to make to the second method? Or is there any other method that I can use to send xml file to remote server? Let me know if I need to add some other details. Thanks for your help. </p>
  
  <hr>
  
  <p>NOTE: I actually solved this problem by using the solution given in the comments. I didn't use approaches suggested in the answers, even though those answers are pretty useful. So, I didn't select the best answer out of those answers given. Nonetheless, I still appreciate all of your helps, thus deserve my upvote. Cheers!</p>