Android 开发工具类 30_sendXML

时间:2023-03-09 15:35:57
Android 开发工具类 30_sendXML

String xml = "<?xml version=\"1.0" encoding=\"UTF-8"?>

<persons><person id=\"23\"><name>hello</name><age>30

</age></person></persons>";

 private static boolean sendXML(String path, String xml) throws Exception{
//
byte[] data = xml.getBytes(); HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);//允许对外传输数据
conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length)); OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close(); if(conn.getResponseCode() == 200){
return true;
}
return false;
}