Httpq请求xml并接收
1 package com.jf.common 2 3 import java.io.BufferedReader; 4 import java.io.IOException; 5 import java.io.InputStreamReader; 6 import java.io.OutputStreamWriter; 7 import java.net.MalformedURLException; 8 import java.net.URL; 9 import java.net.URLConnection; 10 11 public class HttpPostTest { 12 void testPost(String urlStr) { 13 try { 14 URL url = new URL(urlStr); 15 URLConnection con = url.openConnection(); 16 con.setDoOutput(true); 17 con.setRequestProperty("Pragma:", "no-cache"); 18 con.setRequestProperty("Cache-Control", "no-cache"); 19 con.setRequestProperty("Content-Type", "text/xml"); 20 21 OutputStreamWriter out = new OutputStreamWriter(con 22 .getOutputStream()); 23 String xmlInfo = getXmlInfo(); 24 System.out.println("urlStr=" + urlStr); 25 System.out.println("xmlInfo=" + xmlInfo); 26 out.write(new String(xmlInfo.getBytes("ISO-8859-1"))); 27 out.flush(); 28 out.close(); 29 BufferedReader br = new BufferedReader(new InputStreamReader(con 30 .getInputStream())); 31 String line = ""; 32 for (line = br.readLine(); line != null; line = br.readLine()) { 33 System.out.println(line); 34 } 35 } catch (MalformedURLException e) { 36 e.printStackTrace(); 37 } catch (IOException e) { 38 e.printStackTrace(); 39 } 40 } 41 42 private String getXmlInfo() { 43 StringBuilder sb = new StringBuilder(); 44 sb.append("<videoSend>"); 45 sb.append(" <header>"); 46 sb.append(" <sid>1</sid>"); 47 sb.append(" <type>service</type>"); 48 sb.append(" </header>"); 49 sb.append(" <service name=\"videoSend\">"); 50 sb.append(" <fromNum>xxxx</fromNum>"); 51 sb.append(" <toNum>xxxx</toNum>"); 52 sb.append(" <videoPath>mnt/xxx.xxx.xxx.xxx/resources/xxx.mov</videoPath>"); 53 sb.append(" <chargeNumber>xxx</chargeNumber>"); 54 sb.append(" </service>"); 55 sb.append("</videoSend>"); 56 return sb.toString(); 57 } 58 59 public static void main(String[] args) { 60 String url = "http://xxx.xxx.xxx.xxx:xxxx/VideoSend"; 61 new HttpPostTest().testPost(url); 62 } 63 }