java技术学习

向指定URL发送XML数据

 

 1 package  com.test.mainterface;
 2
 3 import  java.io. * ;
 4 import  java.net.HttpURLConnection;
 5 import  java.net.URL;
 6
 7 public   class  Httptest1  {
 8
 9      public   static   void  main(String[] args)  {
10
11         URL url  =   null ;
12         HttpURLConnection conn  =   null ;
13         OutputStream outStream  =   null ;
14         InputStream inStream  =   null ;
15         
16          try   {
17             url  =   new  URL( " http://219.233.89.7:8090/cgi-bin/mailinterface " );
18             conn  =  (HttpURLConnection) url.openConnection();
19             conn.setDoOutput( true );
20             conn.setDoInput( true );
21
22             String sendXml  =   " <?xml version=\ " 1.0 \ "  encoding=\ " UTF - 16 \ " ?><!DOCTYPE SigMailCommand SYSTEM \ " SigMail.dtd\ " > " // XML数据    
23             sendXml  +=   " <SigMailCommand Name=\ " checkuser\ " ><Param Name=\ " domainname\ " >test.com.cn</Param> " ;
24             sendXml  +=   " <Param Name=\ " username\ " >admin</Param></SigMailCommand> " ;
25
26             outStream  =  conn.getOutputStream();
27
28              // 准备通过CONN对象写入XML数据
29             BufferedWriter bw  =   new  BufferedWriter( new  java.io.OutputStreamWriter(outStream,
30              " UTF-16 " ));
31             bw.write(sendXml);
32             bw.flush();
33             bw.close();
34             
35              // DataOutputStream dataOutStream = new DataOutputStream(outStream);
36              // dataOutStream.writeChars(xml);
37              // dataOutStream.flush();
38              // dataOutStream.close();
39             
40
41              // 准备通过CONN对象读取返回的XML数据
42
43             inStream  =  conn.getInputStream();
44             StringBuffer returnXml  =   new  StringBuffer( "" );
45             BufferedReader rd  =   new  BufferedReader( new  InputStreamReader(inStream,
46                      " UTF-16 " ));
47              for  (String line  =   null ; (line  =  rd.readLine())  !=   null ;)  {
48                 returnXml.append(line);
49             }

50             System.out.println(returnXml.toString());
51             rd.close();
52         }
  catch  (IOException ex)  {
53             ex.printStackTrace();
54         }
finally {
55              try   {
56                  if  (outStream  !=   null )
57                     outStream.close();
58                  if  (inStream  !=   null )
59                     inStream.close();
60                  if  (conn  !=   null )
61                     conn.disconnect();
62             }
  catch  (IOException e)  {
63                  //  TODO 自动生成 catch 块
64                 e.printStackTrace();
65             }

66         }

67
68     }

69
70 }

71

posted on 2007-01-12 15:52 denny 阅读(1270) 评论(0)  编辑  收藏 所属分类: JAVA技术


只有注册用户登录后才能发表评论。


网站导航:
 

My Links

Blog Stats

留言簿

文章分类

文章档案

搜索

最新评论