(转)向Internet发送xml数据

Code:
  1.  利用HttpURLConnection对象,我们可以向网络发送xml数据.  
  2. StringBuilder xml =  new StringBuilder();  
  3. xml.append("<?xml version=/"1.0/" encoding=/"utf-8/" ?>");  
  4. xml.append("<M1 V=10000>");  
  5. xml.append("<U I=1 D=/"N73/">中国</U>");  
  6. xml.append("</M1>");  
  7. byte[] xmlbyte = xml.toString().getBytes("UTF-8");  
  8. URL url = new URL("http://localhost:8080/itcast/contanctmanage.do?method=readxml");  
  9. HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  10. conn.setConnectTimeout(61000);  
  11. conn.setDoOutput(true);//允许输出  
  12. conn.setUseCaches(false);//不使用Cache  
  13. conn.setRequestMethod("POST");          
  14. conn.setRequestProperty("Connection""Keep-Alive");//维持长连接  
  15. conn.setRequestProperty("Charset""UTF-8");  
  16. conn.setRequestProperty("Content-Length", String.valueOf(xmlbyte.length));  
  17. conn.setRequestProperty("Content-Type""text/xml; charset=UTF-8");  
  18. DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());  
  19. outStream.write(xmlbyte);//发送xml数据  
  20. outStream.flush();  
  21. if (conn.getResponseCode() != 200throw new RuntimeException("请求url失败");  
  22. InputStream is = conn.getInputStream();//获取返回数据  
  23. String result = readAsString(is, "UTF-8");  
  24. outStream.close();  


l

你可能感兴趣的:(xml,cache,网络,url,byte,internet)