如何在java中解码通过Url发送的xml

时间:2014-09-11 12:59:57

标签: java xml

我在java中使用以下方法启动了一个在线xml:

 public String WriteToServer(String xml) {
     StringBuilder answer = new StringBuilder();
     try {
         String myurl="example.com";

         URL url = new URL(myurl); 
         URLConnection conn = url.openConnection(Proxy.NO_PROXY);

         conn.setDoOutput(true);
         OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

         writer.write(xml);
         writer.flush();

         BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
         String line;
         while ((line = reader.readLine()) != null) {
             answer.append(line);
         }

         writer.close();
         reader.close();

     } catch (Exception e) {
         System.out.println(e);
     }
     return answer.toString();
 }

我的问题是服务器接收到编码的xml,因此它无法理解并向客户端返回500响应。如何将xml解码为服务器可以读取的纯文本?

1 个答案:

答案 0 :(得分:0)

在创建OutputStreamWriter实例

之前尝试以下代码
String myurl="example.com";
URL url = new URL(myurl); 
URLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Language", "en-US");
conn.setUseCaches (false);
conn.setDoInput(true);
conn.setDoOutput(true);