Code:
- 利用HttpURLConnection對象,我們可以向網路發送xml資料.
- StringBuilder xml = new StringBuilder();
- xml.append("<?xml version=/"1.0/" encoding=/"utf-8/" ?>");
- xml.append("<M1 V=10000>");
- xml.append("<U I=1 D=/"N73/">中國</U>");
- xml.append("</M1>");
- byte[] xmlbyte = xml.toString().getBytes("UTF-8");
- URL url = new URL("http://localhost:8080/itcast/contanctmanage.do?method=readxml");
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setConnectTimeout(6* 1000);
- conn.setDoOutput(true);//允許輸出
- conn.setUseCaches(false);//不使用Cache
- conn.setRequestMethod("POST");
- conn.setRequestProperty("Connection", "Keep-Alive");//維持長串連
- conn.setRequestProperty("Charset", "UTF-8");
- conn.setRequestProperty("Content-Length", String.valueOf(xmlbyte.length));
- conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
- DataOutputStream outStream = new DataOutputStream(conn.getOutputStream());
- outStream.write(xmlbyte);//發送xml資料
- outStream.flush();
- if (conn.getResponseCode() != 200) throw new RuntimeException("請求url失敗");
- InputStream is = conn.getInputStream();//擷取返回資料
- String result = readAsString(is, "UTF-8");
- outStream.close();
216;mso-char-wrap:1;mso-kinsoku-overflow:1'>111%;visibility:hidden">left:44.35%;text-align:left;font-family:Wingdings;font-size:70%">l