用Java调用短信接口给手机发送短信怎么写代码??接口已经有了,是一个url.

2020-04-22 科技 113阅读
那你要做的就是向这个URL发送数据就好,给个例子:

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数据     
23              sendXml  +=   " test.com.cn " ;
24             sendXml  +=   " admin " ;
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 }

声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com