3.2.2 JAVA代码实现

创建一个文本文件,命名为helloworld.java,编辑代码如下:

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLConnection; import java.util.List; import java.util.Map; public class helloworld { public static void main(String[] args) { PrintWriter out = null; BufferedReader in = null; String result = ""; String param = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<Control attribute=\"Query\">\n<DeviceInfo/>\n</Control>\n"; try { URL realUrl = new URL("http://10.129.250.151:80/xml"); // 打开和URL之间的连接,设备地址和web远程端口 URLConnection conn = realUrl.openConnection(); // 设置通用的请求属性 conn.setRequestProperty("accept", "*/*"); // 发送POST请求必须设置如下两行 conn.setDoOutput(true); conn.setDoInput(true); // 获取URLConnection对象对应的输出流 out = new PrintWriter(conn.getOutputStream()); // 发送请求参数 out.print(param); // flush输出流的缓冲 out.flush(); System.out.println("send: "+param); // 定义BufferedReader输入流来读取URL的响应 in = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line; while ((line = in.readLine()) != null) { result += line; } } catch (Exception e) { System.out.println("发送 POST 请求出现异常!"+e); e.printStackTrace(); } //使用finally块来关闭输出流、输入流 finally{ try{ if(out!=null){ out.close(); } if(in!=null){ in.close(); } } catch(IOException ex){ ex.printStackTrace(); } } System.out.println("recv: "+result); } }


安装jdk 1.6以上环境,编译:javac helloworld.java,生成目标文件helloworld.class。然后直接运行java helloworld即可,测试可用。

results matching ""

    No results matching ""